如何在 python 中使用 NLP 来分析聊天对话中的问题

How to use NLP in python to analyze questions from a chat conversation

我在 python 中编写了一个聊天机器人,它连接到 discord,并且能够完成一些任务。其中一项任务是查询特定电脑游戏的资源列表,以及return查询资源的详细位置。 现在我想尽可能无缝地将功能集成到聊天中。所以我想我可以使用 NLP 技术。

举个例子: 用户 1 想知道 he/she 在哪里可以找到资源 "wood"。所以 he/she 在 discord 聊天中询问: "Where can I find wood?"

我的程序现在应该能够将此问题识别为对资源位置的有效查询,并以资源位置响应 "wood"。

这可能涉及几个步骤:

我不是编程新手,但我是 NLP 新手。我也是深度学习的初学者/已经使用 tensorflow/keras.

开发了 RNN 模型

对于这个项目,我找到了 nltkspaCy,它们都是用于 NLP 的 python 模块。我已经知道文本分析由几个不同的工作组成,并不是所有的工作都适合我的项目。但似乎标记化和 pos 标记都可能令人感兴趣。但不知何故,我正在努力寻找一种可行的方法来完成这项任务。它已经从如何识别一条短信是否实际上是一个问题开始。我的研究表明这不是 NLP 库提供的开箱即用的功能,预训练的深度学习模型通常用于对这样的句子进行分类。

到目前为止我的想法:

1) 逐句分析每条聊天消息
标记句子,使用词干提取,然后进行词性标记,然后迭代所有标记以找出是否:

2) 使用某种匹配,例如 spaCy 的基于规则的匹配

3) 使用非 NLP 技术 如果其他一切都应该 unviable/too 复杂,我仍然可以想出一个硬编码的方法,我会预先定义几个问题类型,并在聊天消息中字符串搜索它们的出现,并尝试手动提取使用字符串操作的资源名称。
这可能是最容易出错且最不灵活的解决方案,但我会保留它作为后备方案。

当然,我确实想实现一个尽可能灵活的解决方案,这样它就可以检测各种形式和类型的问题,而无需事先对所有可能的问题类型进行硬编码。它应该尽可能接近"the bot just understands the chat and answers the question"。

有人可以指导我找到一个好的解决方案吗? (不要求完整的代码,而是我将使用的 techniques/steps/libraries)

也许作为旁注:在以后的版本中我想扩展功能。然后,其他用户可能会在不和谐聊天中命名资源的位置,如果该位置尚未包含,则机器人会将此位置添加到其数据库中。所以聊天对话可能看起来像:

User 1: Where can I find cryptonite?
User 2: It can be found in lex luthors lab
Bot: Shall I add "lex luthors lab" as location for resource "cryptonite"?
User 2: @bot: yes
Bot: Done.  

tl:dr

看来你基本上有一个 intent/entity 问题。

1) 逐句分析每条聊天消息。 这可以通过意图分类来解决。

2) 使用某种匹配,比如 spaCy 的基于规则的匹配 这可以通过实体提取来解决。


意图

意图是对整个句子的分类。

例如,您可以有一个意图:find_resource。 然后,您将需要应分类为 find_resource 的例句。 例如:

X = [
  'Where can I find wood?',
  'What is the location of wood?',
  'Where do I find fire?',
  'Give me the coordinates of lemons.',
  'What is the best place to gather coal?',
  'Do you know where I can find tomatoes?',
  'Tell me a spot to collect wood.'
]

您可以训练神经网络来执行此分类任务,但您可以先尝试更简单的模型。一个好的机器学习库是 scikit-learn,它提供 out-of-the-box 传统的机器学习分类方法。它还有一个 feature_extraction.text sub-package 用于处理文本。

例子

# Training data

## X is the sample sentences
X = [
    'Where can I find wood?',
    'What is the location of wood?',
    'Where do I find fire?',
    'Give me the coordinates of lemons.',
    'What is the best place to gather coal?',
    'Do you know where I can find tomatoes?',
    'Tell me a spot to collect wood.',
    'How can I level up strength?',
    'How do I train woodcutting?',
    'Where can I practice my swimming skill?',
    'Can I become better in running?',
    'Where can I train my woodcutting skill?'
]

## y is the intent class corresponding to sentences in X
y = [
    'find_resource',
    'find_resource',
    'find_resource',
    'find_resource',
    'find_resource',
    'find_resource',
    'find_resource',
    'improve_skill',
    'improve_skill',
    'improve_skill',
    'improve_skill',
    'improve_skill'
]

# Define the classifier

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline

clf = Pipeline(
    [
        ('tfidf', TfidfVectorizer()),
        ('sgd', SGDClassifier())
    ]
)

## Train the classifier

clf.fit(X, y)

# Test your classifier

## New sentences (that weren't in X and your model never seen before)

new_sentences = [
    'What are the coordinates of wood?',
    'Where can I find paper?',
    'How can I improve woodcutting?',
    'Where can I improve my jumping skill?'
]

predicted_intents = clf.predict(new_sentences)

print(predicted_intents)

> ['find_resource' 'find_resource' 'improve_skill' 'improve_skill']

实体提取

实体提取是在句子中找到特定 sub-string 的任务。这可以是 locationtimeperson_name 等...或者在您的情况下 resource_type.

典型的训练数据如下:

X = [
    'Where can I find [wood](resource_type)?',
    'What is the location of [wood](resource_type)?',
    'Where do I find [fire](resource_type)?',
    'How can I level up [strength](skill_type)?',
    'Where can I train my [woodcutting](skill_type) skill?'
]

确实 spaCy 提供了最先进的模型。它具有 pre-trained 实体类型,但它还允许您使用自定义实体对其进行扩展(在您的情况下为 resource_type)。


旁注

User 1: Where can I find cryptonite?
User 2: It can be found in lex luthors lab
Bot: Shall I add "lex luthors lab" as location for resource "cryptonite"?
User 2: @bot: yes
Bot: Done. 

您可以将您的问题建模为:

意图:

X = [
  'Where can I find cryptonite?'
  'It can be found in lex luthors lab',
  'yes'
] 

y = [
  'find_resource',
  'provide_location',
  'affirm'
]

实体:

X = [
  'Where can I find [cryptonite](resource_type)?'
  'It can be found in [lex luthors lab](location)',
  'yes'
] 

诀窍是让您弄清楚 User 2 是否真的回复了 User 1。此外,您需要保持对话的状态,但这取决于您使用的机器人框架。然而,这不再是 NLP 问题。