Issue
I have this simple intent.json file
{
"intents": [
{
"tag": "greeting",
"patterns": [
"Hi",
"How are you",
"Is anyone there?",
"Hello",
"Good day"
],
"responses": [
"Hello"
],
"context_set": ""
},
{
"tag": "goodbye",
"patterns": [
"Bye",
"not interested",
"Goodbye"
],
"responses": [
"ok bye"
]
},
{
"tag": "thanks",
"patterns": [
"Thanks",
"Thank you"
],
"responses": [
"My pleasure"
]
},
{
"tag": "greetiing_exchange",
"patterns": [
"What about you",
"you",
"how about your self"
],
"responses": [
"i am perfect, thanks for asking"
],
"context_set": ""
}
]
}
from fuzzywuzzy import process
for intent in intents['intents']:
Ratios = process.extract(message,intent['patterns'])
for ratio in Ratios:
highest_value = max(Ratios, key = lambda i : i[1])
print(highest_value)
Now i want input from user identify the pattern and output response.
The problem is it is not iterating through every pattern when i input "hi". Its output is ('Hi', 100) ('not interested', 45) ('Thanks', 45) ('What about you', 45)
I want the pattern which is higher in range of 80 to 100, and print response from that pattern
Another thing there is a library Rhasspy which can be used for intent recognition how can i use that library for this file
Solution
Use process.extractOne
and score_cutoff
parameter:
from fuzzywuzzy import process
import operator
ratios = []
for idx, intent in enumerate(intents['intents']):
# ratio = process.extractOne(message, intent['patterns'], score_cutoff=80)
# if ratio:
# --- New in python 3.8: walrus operator ---
if ratio := process.extractOne(message, intent['patterns'], score_cutoff=80):
ratios.append((idx, ratio[0], ratio[1]))
responses = intents['intents'] \
[max(ratios, key=operator.itemgetter(2))[0]] \
['responses'] if ratios else []
>>> responses
['Hello']
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.