使用 fuzzywuzzy 进行意图识别

intent recognition using fuzzywuzzy

我有这个简单的 intent.json 文件

{
  "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)

现在我希望用户输入识别模式并输出响应。

问题是当我输入“hi”时,它没有遍历每个模式。它的输出是 ('Hi', 100) ('not interested', 45) ('Thanks', 45) ('What about you', 45)

我想要 80 到 100 范围内较高的模式,并打印来自该模式的响应

还有一个 Rhasspy 库可以用于意图识别,我如何为这个文件使用那个库

使用process.extractOnescore_cutoff参数:

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']