Reputation: 21
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.
Upvotes: 0
Views: 456
Reputation: 3298
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
Upvotes: 0
Reputation: 1508
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')
Upvotes: 1