【发布时间】:2020-09-12 01:25:04
【问题描述】:
这里的第一个问题,如果我的礼仪很差,请多多包涵。我目前正在做一个项目,目标是使用 python 实现语音助手。我们被推荐使用自然语言处理来帮助助手更有效地解析问题,我已经为此成功安装了 nltk。我对自然语言处理完全陌生,所以我遇到了一些困惑。
现在我的代码将对麦克风进行口头输入,例如:
"what is the weather in Chicago?"
并成功对其进行标记,删除停用词并将其标记如下:
import nltk # importing the natural language toolkit
from nltk import word_tokenize # this allows us to tokenize a sentence
from nltk.corpus import stopwords # this allows us to filter out stopwords
# Tokenizes the sentence
tokens = word_tokenize(text)
print(tokens)
# Removes stopwords from the sentence
sWords = set(stopwords.words('english'))
cleanTokens = [w for w in tokens if not w in sWords]
print(cleanTokens)
# Tags the sentence
tagged = nltk.pos_tag(cleanTokens)
print(tagged)
# Prints fully processed sentence with tags attatched
print(nltk.ne_chunk(tagged))
输出:
['what', 'is', 'the', 'weather', 'in', 'Chicago']
['weather', 'Chicago']
[('weather', 'NN'), ('Chicago', 'NNP')]
(S weather/NN (GPE Chicago/NNP))
基本上我的问题是我不确定从这里去哪里。我还没有真正找到任何很好的例子来说明如何将这样的文本与 API 一起使用以实际返回芝加哥的天气。
在这个伪代码中简单地使用 if/else 语句是否正确?:
if tagged.contains("weather")
city = searchForCities(tagged)
return city.weatherReport
elif tagged.contains("time) ect...
总而言之,当您对 nltk 文本进行标记/标记时,您的代码确定下一步要做什么以便相关信息被正确的库使用的最佳方法是什么?
【问题讨论】:
-
您是否考虑过其他选项,例如在 AI bot 工具之上构建以进行对话,例如 Google's DialogFlow(其他可从 Amazon、Microsoft 等获得)API 可以识别句子中的指定关键字,这些关键字允许在句子变体中识别这些单词::1)芝加哥的天气怎么样?,2)请告诉我芝加哥的天气吗? 3) 等。可以用 Python 和其他语言进行定制。 Home Depot 在他们的购物应用中使用它。
-
您可能只想返回查询搜索引擎的结果,其中包含您认为重要的句子中的单词。如果您知道用户只会询问一部分问题,则可以执行您提到的 if -else 块。但是,如果他们可以就您想要量身定制的答案提出有关广泛主题的问题,那么您每次整理的场景数量将过于庞大且难以处理。
标签: python nlp nltk voice-recognition