繁体   English   中英

需要帮助来确定用户是否使用Python回答了“是”或“否”

[英]Need help determining whether a user has answered “yes” or “no” using Python

我正在编写一个微型聊天机器人,但似乎无法找出确定用户是否回答“是”或“否”的方法。 例如,如果用户键入“确定”,我想知道他们实际上回答了“是”。 或者,如果他们写的是“不”,我想知道他们实际上回答了“不”。

使用nltk的wordnet并没有多大帮助。 这是我尝试过的:

import nltk
from nltk.corpus import wordnet as wn

for syn in wn.synsets('yes'):
    print(syn.name(), syn.lemma_names())

我希望能得到像yes.n.01 ['yes', 'okay', 'sure', 'yup', 'yeah'] ,但是我得到的只是yes.n.01 ['yes'] 我正在寻找Python中的解决方案,尽管不一定需要通过nltk软件包。

因此,我不确定如何使用NLP来执行此操作,但是对于您的目的而言,这可能会显得有些过头。 您可以只创建一个包含所有“是”和“否”字词的字词集。

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')

我认为可以选择使用PyDictionary https://pypi.org/project/PyDictionary/

如果您执行以下操作:

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")

那可能很好

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM