简体   繁体   中英

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. 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'] . I'm looking for a solution in Python, though it doesn't necessarily need to be through the nltk package.

So I'm not sure how to do this with NLP, but it might be overkill for your purpose. 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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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