Reputation: 19
(I am new to Python and programming) I am using raw_input()
so I can make a program that talks to the user. For example:
Program: How are you? User: I am doing great!/I feel terrible.
I need my program to respond accordingly, as in "YAY!" or "Aw man... I hope you feel better soon." so can you please give me ways to scan for words such as "good" or "bad" in the user's raw input so my program knows how to respond?
I know a few ways to do this, but the problem is, I want multiple words for it to look for, like great, amazing, and awesome can all be classified into the "good" group. AND, I need it where it doesn't have to be exact. I keep on running into problems where the user has to exactly type, "I am good." instead of all the different variations that they could possibly say it. THANK YOU IN ADVANCE!
Upvotes: 0
Views: 379
Reputation: 6566
You might find set
s useful here.
sentence = "I feel great"
words = sentence.split(" ")
if set("good", "fine", "great").intersects(words):
print("user is good!")
if set("bad", "poorly", "sick").intersects(words):
print("user is bad")
set
is a special type of container, like a list
or a dict
. It ignores repeated entries, but is extremely fast at checking whether an item already exists. set.intersects
returns a new set
which contains only items found in both sides: so, in the first if
statement above, it returns set("great")
, which is "truthy" (behaves like True
in a boolean context). The second if statement gets an empty set, which is "falsey"
You could clean it up using a dictionary (but you have to make a slight modification I'll explain below)
responses = {frozenset("good", "fine", "great"): "that's fantastic",
frozenset("bad", "poorly", "sick"): "I hope you feel better!"}
words = sentence.split(" ")
for wordset, reply in responses:
if(wordset.intersects(words):
print(reply)
The change we had to make us to use frozenset
rather than set
. The only real difference that you need to worry about, is that a frozenset
is immutable, which means you can't add or remove members from it. For reasons relating to the implementation, dict
keys should be immutable: you can't use dict
, list
or set
as a key: but you can use tuple
, int
, str
etc.
You could get exactly the same behaviour without using set
: it just takes a couple more lines:
responses = {("good", "fine", "great"): "that's fantastic",
("bad", "poorly", "sick"): "I hope you feel better!"}
words = sentence.split(" ")
for wordtuple, reply in responses:
for word in words:
if word in wordtuple:
print(reply)
break
So, we need two more lines, and end up doing a lot more checking than we actually need to. If there's a lot of possible words to check, that can slow your program down. It isn't always bad to do it this way, for instance, you might get given responses
, which means you won't have control over the types, but if you know you're writing all your own code, it's good to do things pythonically from the start.
Upvotes: 2
Reputation: 73
Well I'm not sure of having a program understand the english language. It will only take a string literal as a string literal. "Good" does not mean Good or Bad to the Interpreter in Python.
What I'd suggest is making a dictionary of all of the good phrases you want, such as I'm good, Feelin' great, I'm A OK. You can store all of these good feeling string literals to your "Good Feels" dictionary and vice versa for your Bad feels string literals.
I'm not too sure how you'd work around spelling with <100% accuracy and the interpreter still picking it up
I'm a bit inexperienced myself, but I'd say a predefined dictionary is your best bet, maybe throw in an else statement that prompts the user to spell correctly if he can't get one of the saying right.
Upvotes: 0