Python NLP 意图识别

新手上路,请多包涵

我是 Python 和 NLP 的新手,我的问题是如何找出给定问题的意图,例如我有这样的问题和答案集:

question:What is NLP; answer: NLP stands for Natural Language Processing

我做了一些基本的 POS tagger 上面问题中的给定问题我得到 entety [NLP] 我也做了 String Matching 使用这个算法

基本上我面临以下问题:

  1. 如果用户询问 what is NLP 那么它将返回准确的答案
  2. 如果用户询问 meaning of NLP 则失败
  3. 如果用户询问 Definition of NLP 则失败
  4. 如果用户询问 What is Natural Language Processing 则失败

那么我应该如何识别给定问题的用户意图,因为在我的情况下,字符串匹配或模式匹配不起作用。

原文由 Neo-coder 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答

您可以使用 spacy 为聊天意图语义训练自定义解析器。

spaCy 的解析器组件可用于训练以预测输入文本上的任何类型的树结构。您还可以预测整个文档或聊天记录的树,以及用于注释话语结构的句子根之间的连接。

例如:“告诉我柏林最好的酒店”

 ('show', 'ROOT', 'show')
('best', 'QUALITY', 'hotel') --> hotel with QUALITY best
('hotel', 'PLACE', 'show') --> show PLACE hotel
('berlin', 'LOCATION', 'hotel') --> hotel with LOCATION berlin

要训练模型,您需要以下格式的数据:

 # training data: texts, heads and dependency labels
# for no relation, we simply chose an arbitrary dependency label, e.g. '-'
TRAIN_DATA = [
    ("find a cafe with great wifi", {
        'heads': [0, 2, 0, 5, 5, 2],  # index of token head
        'deps': ['ROOT', '-', 'PLACE', '-', 'QUALITY', 'ATTRIBUTE']
    }),
    ("find a hotel near the beach", {
        'heads': [0, 2, 0, 5, 5, 2],
        'deps': ['ROOT', '-', 'PLACE', 'QUALITY', '-', 'ATTRIBUTE']
    })]

TEST_DATA:
input : show me the best hotel in berlin
output: [
      ('show', 'ROOT', 'show'),
      ('best', 'QUALITY', 'hotel'),
      ('hotel', 'PLACE', 'show'),
      ('berlin', 'LOCATION', 'hotel')
    ]

有关详细信息,请查看以下链接。 https://spacy.io/usage/examples#intent-parser

原文由 manish Prasad 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题