0

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

1 Answer 1

1

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']
11
  • Every thing is working fine here thank you for that but instead printing response from json it is just matching with patterns and prints closest pattern. How can i change that ?? like when it sees a pattern it shows relevant response, i tried adding loop and changing some stuff here and there but couldn't figure out
    – malic92
    Jun 28, 2021 at 7:15
  • You want the highest ratio of each tag?
    – Corralien
    Jun 28, 2021 at 7:22
  • No i want highest ratio from each pattern and which ever pattern have highest ratio, it shows response from that pattern like if i type "bye" its in second tag's pattern when it sees that it reaches to it response and print that response which is "ok bye"
    – malic92
    Jun 28, 2021 at 7:25
  • I hadn't seen the key responses :-P
    – Corralien
    Jun 28, 2021 at 7:28
  • "tag": "goodbye", "patterns": [ "Bye", "not interested", "Goodbye" ], "responses": [ "ok bye" ] here like this
    – malic92
    Jun 28, 2021 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.