我有一个问题列表:
>>> 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))])
您可以创建一个字典,将关键字映射到它们的答案
您可以使用NLP扩展词典(
mapper
),以包含更多的单词相关问题 更多 >
编程相关推荐