python - 如何使用 nlp 从问题列表中生成答案?
问题描述
我有一个名为问题的列表:
>>> questions=['Where do you live?', 'What is my favourite color?', 'What is your age', 'Do you like coding?']
和一个名为答案的列表:
>>> answers=['I live in India','my favourite color is orange', 'my age is 16','I love coding']
我需要做的是创建一个函数,它将字符串作为不同的问题并返回适当的答案。例如 :
>>> def get_answer(question):
... # Compare the string with the questions list and then give the appropriate answer accordingly
>>> ans=get_answer('Do you live in india or america?')
>>> print(ans)
I live in India
到目前为止我尝试过的
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
def simi(X,Y):
# tokenization
X_list = word_tokenize(X)
Y_list = word_tokenize(Y)
# sw contains the list of stopwords
sw = stopwords.words('english')
l1 =[];l2 =[]
# remove stop words from the string
X_set = {w for w in X_list if not w in sw}
Y_set = {w for w in Y_list if not w in sw}
# form a set containing keywords of both strings
rvector = X_set.union(Y_set)
for w in rvector:
if w in X_set: l1.append(1) # create a vector
else: l1.append(0)
if w in Y_set: l2.append(1)
else: l2.append(0)
c = 0
# cosine formula
for i in range(len(rvector)):
c+= l1[i]*l2[i]
cosine = c / float((sum(l1)*sum(l2))**0.5)
return cosine
questions=['Where do you live?', 'What is my favourite color?', 'What is your age', 'Do you like coding?']
answers=['I live in India','my favourite color is orange', 'my age is 16','I love coding']
ques='Do you live in india or america?'
lst=[simi(ques,char) for char in questions]
print(answers[lst.index(max(lst))])
解决方案
您可以创建一个字典,将关键字映射到他们的答案。
questions=['Where do you live?', 'What is my favourite color?', 'What is your age', 'Do you like coding?']
answers=['I live in India','my favourite color is orange', 'my age is 16','I love coding']
keywords = ['live', 'color', 'age', 'coding']
mapper = {key:value for key,value in zip(keywords, answers)}
def get_answer(question):
words = question.split(' ')
for word in words:
if word in keywords:
break
return mapper[word]
ans=get_answer('Do you live in india or america?')
print(ans) # prints "I live in India"
mapper
您可以使用 NLP扩展您的字典( )以包含更多单词。
推荐阅读
- c# - 在 Docker 上将 ASP.NET Core 2.2 中的默认时区更改为 24 小时制时间格式
- laravel - Laravel 中的大规模更新(Eloquent)
- ios - 如何快速将子视图塑造成它的超级视图?
- mysql - 当自动增量字段达到最大值时插入重复键更新失败且没有错误
- chat - 获取列表视图中的当前元素
- excel - 有没有更快的方法来执行这个循环 instr 函数?
- python - 为什么 Python 不从 Latin Extended-A 写入字符(写入文件时出现 UnicodeEncodeError)?
- javascript - 引导导航栏工具无法在小屏幕上工作
- python - 在 Python 中捕获的异常仍在冒泡
- python - 测试一个使用 sqlite 数据库的 Flask API