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

标签 python nlp chat bots

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

举个例子: 用户 1 想知道他/她在哪里可以找到资源“木材”。所以他/她在不和谐的聊天中问道:“我在哪里可以找到木材?”

我的程序现在应该能够将此问题识别为对资源位置的有效查询,并以资源“木材”的位置进行响应。

这可能涉及几个步骤:

  • 确定实际上提出了问题
  • 确定所请求资源的名称
  • ???

我对编程并不陌生,但对 NLP 却很陌生。另外,我是深度学习的初学者/已经使用tensorflow/keras开发了RNN模型。

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

到目前为止我的想法:

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

  • 包含动词“find”(在哪里可以找到...)或“get”(在哪里可以得到...)”或“is”(在哪里...)
  • 检查是否包含名词,如果包含,该名词是否是有效的资源名称(更好的方法可能是找出名词实际上是与动词相关的宾语。这可能吗?)
  • 通过检查最后一个标记是否为 ? 来检查句子是否为问题。

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

  • 构建多种可以识别所需问题/问题类型的模式
  • 匹配每条聊天消息中的模式
  • 如果匹配,则提取资源名称

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

当然,我确实希望实现一个尽可能灵活的解决方案,以便它可以检测各种形式和类型的问题,而无需事先对所有可能类型的问题进行硬编码。它应该尽可能接近“机器人只是理解聊天并回答问题”。

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

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

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:博士

看来您基本上存在意图/实体问题。

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它提供了开箱即用的传统机器学习分类方法。它还有一个用于处理文本的 feature_extraction.text 子包。

示例

# 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']
<小时/>

实体提取

实体提取是在句子中查找特定子字符串的任务。这可以是位置时间person_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提供最先进的模型。它具有预先训练的实体类型,但它还允许您使用自定义实体(在您的情况下为 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'
] 

诀窍是让您弄清楚用户 2 是否确实回复了用户 1。此外,您需要保持对话的状态,但这取决于您正在使用的机器人框架。尽管如此,这不再是一个 NLP 问题。

关于python - 如何在Python中使用NLP来分析聊天对话中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56836865/

相关文章:

python - 如何使用 python 将波斯语文本保存在 csv 文件中?

audio - 如何避免 webrtc 的 javascript 中的回声和噪音

Facebook 聊天在打开时 Conceal Flash 应用程序

php - MySQL - WHERE、AND & OR 语法

python - 如何捕获 "kill"异常

python获取实例变量最佳实践

python - 如何使用 xlrd 将新列和行添加到 .xls 文件

python - 我应该如何使用 scikit learn 对以下列表列表进行矢量化?

python - 情感分析 Python 标记化

python - Unicode 该死的 : detwingle crashes on a website