首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用fuzzywuzzy进行意图识别

使用fuzzywuzzy进行意图识别
EN

Stack Overflow用户
提问于 2021-06-28 13:55:25
回答 1查看 53关注 0票数 0

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

代码语言:javascript
复制
{
  "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) (‘不感兴趣’,45) (‘谢谢’,45) (‘你呢’,45)

我想要在80到100的范围内较高的图案,并从该图案打印响应

另外,还有一个可用于意图识别的库Rhasspy,我如何将该库用于此文件

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-28 14:18:19

使用process.extractOnescore_cutoff参数:

代码语言:javascript
复制
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 []
代码语言:javascript
复制
>>> responses
['Hello']
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68158190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档