首页 > 解决方案 > 如何使用 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))])

标签: pythonpython-3.xlistnlpnltk

解决方案


您可以创建一个字典,将关键字映射到他们的答案。

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扩展您的字典( )以包含更多单词。


推荐阅读