首页 > 解决方案 > 如何在 Python 中使用 NLP 分析聊天对话中的问题

问题描述

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

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

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

这可能涉及几个步骤:

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

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

到目前为止我的想法:

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

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.  

标签: pythonnlpchatbots

解决方案


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

实体提取

实体提取是在句子中找到特定子字符串的任务。这可以是location, time, person_name, etc... 或在您的情况下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'
] 

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


推荐阅读