简体   繁体   English

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

[英]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.

相关问题 如何使用 python 知道用户名属于组还是电报中的用户? 如果是,获取组标题 - How to know whether an username belongs to a group or a user in telegram using python? If yes, get the Group Title Python-需要帮助,使用python将用户输入存储在列表中 - Python - Need Help Getting User input stored in a list using python 需要帮助改进此 Python function 以确定平均绝对偏差 (MAD) - Need help improving this Python function for determining Mean Absolute Deviation (MAD) 使用Python快速确定图像是否(模糊地)在集合中 - Quickly determining using Python whether an image is (fuzzily) in a collection 使用Python中的BS4判断HTML是否包含文本 - Determining whether HTML contains text using BS4 in Python 仅在回答了安全性问题之后,才将用户保存在数据库上(使用ModelForm)? - Save user on database (using ModelForm), only after security questions has been answered? Python是/否用户输入 - Python Yes/No User Input 确定可迭代对象是python中的字符串还是列表 - Determining whether an iterable is a string or list in python 在 Python 中判断一个值是否为整数 - Determining whether an value is a whole number in Python 询问用户(是/否)是否显示情节的问题。 是否使用(if/else)语句运行图形代码块 - Asking user (yes/no) question of whether to show a plot. Using (if/else) statements to run block of graphing code or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM