在字符串输入中搜索 Python 中的短语

Searching a string input for a phrase in Python

有没有办法在 Python 中搜索输入字符串中的短语,然后 return 例如如果存在则为 1,否则为 0?

我希望它像这样工作:

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.")

您当前的实施存在问题,无法正常工作。

  1. .search是一个函数,它是一个对象。因为它是一个对象,所以它永远不会等于 True。因此你总是 return 0.
  2. 代码中的
  3. findphrase('well')(howis) 是无效语法,因为您没有 return 来自 findphrase
  4. 的函数
  5. input in Python2 也将评估该语句,这将为字符串输入抛出 NameErrors。因此,请改用 raw_input
  6. 您可以轻松地使用 in 运算符来反对在此处使用正则表达式
  7. if findphrase('good')(howis) is 1: 是身份测试,因为您只 returning 0/1,您可以使用 if findphrase('good')(howis):[ 直接检查值=40=]

您可以在这里使用一个简单的 lambda 函数:

findphrase = lambda s, var: var.lower() in s.lower()

并这样称呼它:

>>> findphrase("I'm not well", "Not Well")
True
>>> findphrase("I'm not well", "Good")
False

如果你想return一个函数, 那么你可以使用

findphrase = lambda var: lambda original_string: var.lower() in original_string.lower()

>>> howis = raw_input()
I'm doing GooD
>>> findphrase("Good")(howis)
True

正则表达式对此可能有点矫枉过正。我会使用 in.

示例 1:

鉴于您对 10 的 return 要求,我实施 findphrase() 的方式是:

>>> 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
>>> 

请注意使用 to_find.lower()phrase.lower() 以确保大小写无关紧要。

示例 2:

但坦率地说,我不确定您为什么要 return 1 或 0。我只想 return 一个布尔值,这样就可以:

>>> 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
>>> 

如果您确实需要将结果用作 10(如果您重写 howareyou() 函数则不需要),TrueFalse分别转换为10

>>> assert int(True) == 1
>>> assert int(False) == 0
>>>

补充说明

在您的 howareyou() 函数中,您遇到了一些错误。您将 findphrase() 称为 findphrase('not well')(howis)。这只有在你 return 从 findphrase()(一个闭包)中调用一个函数时才有效,像这样:

>>> 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
>>> 

之所以可行,是因为函数只是 Python 中的另一种类型的对象。它可以像任何其他对象一样被 returned。如果您正在按照以下方式做某事,您可能想要使用这样的结构:

>>> 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
...
>>>

这是可行的,因为您将 findphrase('not well') 的结果分配给变量 not_well。这个 return 是一个函数,您可以调用它 not_well(phrase)。当您这样做时,它会将您提供给 not_well() 的变量 phrase 与您提供给 findphrase() 的变量 var 进行比较,后者存储为部分not_well().

的命名空间

但在这种情况下,您可能真正想要做的是使用两个参数定义 findphrase() 函数,就像前两个示例中的一个一样。

您也在使用 findphrase(...) is 1。您可能想要的是 findphrase(...) == 1 或者更像 pythonic 的 if findphrase(...):.