Reputation: 13
Is there a way in Python to search an input string for a phrase, and then return e.g. a 1 if it is there, or a 0 if not?
I want it to work like this:
def findphrase(var):
if re.compile(r'\b({0})\b'.format(var), flags=re.IGNORECASE).search is True:
return 1
else:
return 0
def howareyou():
print("So",name, "how are you today?")
howis = input("")
if findphrase('not well')(howis) is 1:
print("Oh, that's not good. I hope you feel better soon")
elif findphrase('well')(howis) is 1:
print("That's good.")
elif findphrase('not bad')(howis) is 1:
print("Better than bad, I suppose.")
elif findphrase('bad')(howis) is 1:
print("Oh, that's not good. I hope you feel better soon")
elif findphrase('not good')(howis) is 1:
print("That's a shame. I hope you feel better soon.")
elif findphrase('good')(howis) is 1:
print("That's good.")
else:
print("I dont know how to respond to that. Keep in mind I am a work in progress. At some point I may know how to respond.")
Upvotes: 0
Views: 532
Reputation: 3803
A regex is probably overkill for this. I'd use in
.
The way I'd implement findphrase()
, given your requirement of returning a 1
or 0
is:
>>> def findphrase(phrase, to_find):
... if to_find.lower() in phrase.lower():
... return 1
... else:
... return 0
...
>>> phrase = "I'm not well today."
>>> to_find = 'not well'
>>> in_phrase = findphrase(phrase, to_find)
>>> assert in_phrase == 1
>>>
Note the use of to_find.lower()
and phrase.lower()
to ensure that capitalization doesn't matter.
But frankly, I'm not sure why you want to return 1 or 0. I'd just return a boolean, which would make this:
>>> def findphrase(phrase, to_find):
... return to_find.lower() in phrase.lower()
...
>>> phrase = "I'm not well today."
>>> to_find = "not well"
>>> in_phrase = findphrase(phrase, to_find)
>>> assert in_phrase == True
>>>
In the event that you truly need to use the results as a 1
or 0
(which you don't if you rewrite your howareyou()
function), True
and False
convert to 1
and 0
, respectively:
>>> assert int(True) == 1
>>> assert int(False) == 0
>>>
In your howareyou()
function, you've got a number of errors. You're calling findphrase()
as findphrase('not well')(howis)
. That would only work if you're returning a function from findphrase()
(a closure), like so:
>>> def findphrase(var):
... def func(howis):
... return var.lower() in howis.lower()
... return func
...
>>> phrase = "I'm not well today."
>>> to_find = "not well"
>>> in_phrase = findphrase(to_find)(phrase)
>>> assert in_phrase == True
>>>
This works because a function is just another type of object in Python. It can be returned just like any other object. Where you might want to use a construct like this is if you're doing something along these lines:
>>> def findphrase(var):
... def func(howis):
... return var.lower() in howis.lower()
... return func
...
>>> phrases = ["I'm not well today.",
... "Not well at all.",
... "Not well, and you?",]
>>>
>>> not_well = findphrase('not well')
>>>
>>> for phrase in phrases:
... in_phrase = not_well(phrase)
... assert in_phrase == True
...
>>>
This works because you're assigning the results of findphrase('not well')
to the variable not_well
. This returns a function, which you can then call as not_well(phrase)
. When you do this, it compares the variable phrase
that you're providing to not_well()
to the variable var
, which you provided to findphrase()
, and which is stored as part of the namespace of not_well()
.
But in this case, what you'd probably really want to do would be to define your findphrase()
function with two arguments, like one of the first two examples.
You're also using findphrase(...) is 1
. What you probably want there is findphrase(...) == 1
or, even more pythonic, if findphrase(...):
.
Upvotes: 1
Reputation: 76827
Your current implementation is buggy and won't work.
.search
is a function, it is an object. Since it is an object, it will never equal to True. Hence you will always return 0.findphrase('well')(howis)
in the code is invalid syntax, since you don't return a function from findphrase
input
in Python2 will evaluate the statement as well, which will throw NameError
s for string inputs. So use raw_input
instead.in
operator against using a regex hereif findphrase('good')(howis) is 1:
is an identity test, since you are returning 0
/1
only, you could check the values directly using if findphrase('good')(howis):
You could use a simple lambda function here:
findphrase = lambda s, var: var.lower() in s.lower()
and call it like:
>>> findphrase("I'm not well", "Not Well")
True
>>> findphrase("I'm not well", "Good")
False
If you want to return a function, then you could use
findphrase = lambda var: lambda original_string: var.lower() in original_string.lower()
>>> howis = raw_input()
I'm doing GooD
>>> findphrase("Good")(howis)
True
Upvotes: 2