[英]Need help determining whether a user has answered “yes” or “no” using Python
I am writing a mini-chatbot and I can't seem to figure out a way to determine whether a user has answered "yes" or "no". 我正在编写一个微型聊天机器人,但似乎无法找出确定用户是否回答“是”或“否”的方法。 For example, if the user has typed "okay", I'd like to know that they have essentially answered "yes".
例如,如果用户键入“确定”,我想知道他们实际上回答了“是”。 Or, if they've written "nope", I'd like to know that they've essentially answered "no".
或者,如果他们写的是“不”,我想知道他们实际上回答了“不”。
Using nltk's wordnet hasn't been much of a help. 使用nltk的wordnet并没有多大帮助。 Here is what I tried:
这是我尝试过的:
import nltk
from nltk.corpus import wordnet as wn
for syn in wn.synsets('yes'):
print(syn.name(), syn.lemma_names())
I was hoping to get back something like yes.n.01 ['yes', 'okay', 'sure', 'yup', 'yeah']
, but instead all I get is yes.n.01 ['yes']
. 我希望能得到像
yes.n.01 ['yes', 'okay', 'sure', 'yup', 'yeah']
,但是我得到的只是yes.n.01 ['yes']
。 I'm looking for a solution in Python, though it doesn't necessarily need to be through the nltk package. 我正在寻找Python中的解决方案,尽管不一定需要通过nltk软件包。
So I'm not sure how to do this with NLP, but it might be overkill for your purpose. 因此,我不确定如何使用NLP来执行此操作,但是对于您的目的而言,这可能会显得有些过头。 You could just create a word set of all 'yes' and 'no' words.
您可以只创建一个包含所有“是”和“否”字词的字词集。
no_words = set(['no', 'nope', 'nah'])
yes_words = set('yes', 'yea', 'yeah', 'ok', 'okay', 'sure'])
user_input = input('Please type in your answer')
if user_input in yes_words:
print('user says yes')
elif user_input in no_words:
print('user says no')
I think an option could be to use PyDictionary
https://pypi.org/project/PyDictionary/ 我认为可以选择使用
PyDictionary
https://pypi.org/project/PyDictionary/
If you do something like: 如果您执行以下操作:
from PyDictionary import PyDictionary
dictionary=PyDictionary()
yes_synonyms = dictionary.synonym("yes")
no_synonyms = dictionary.synonym("no")
user_input = input('yes or no')
if user_input in yes_synonyms:
print("yes")
elif user_input in input('yes or no'):
print("no")
That might work well 那可能很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.