在Python中搜索字符串输入以查找短语

时间:2015-05-04 03:00:20

标签: python string-matching

在Python中是否有办法搜索输入字符串中的短语,然后返回例如如果它在那里是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.")

2 个答案:

答案 0 :(得分:2)

您当前的实施工具有缺陷并且无法正常工作。

  1. .search是一个函数,它是一个对象。由于它是一个对象,它永远不会等于True。因此,您将始终返回0。
  2. 代码中的
  3. findphrase('well')(howis)语法无效,因为您不会从findphrase
  4. 返回函数 Python2中的
  5. input也将评估该语句,这将为字符串输入抛出NameError。所以请改用raw_input
  6. 您可以轻松地使用in运算符来反对使用正则表达式
  7. if findphrase('good')(howis) is 1:是一种身份测试,因为您只返回0 / 1,您可以使用if findphrase('good')(howis):直接检查值
  8. 你可以在这里使用一个简单的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
    

    如果要返回功能, 然后你可以使用

    findphrase = lambda var: lambda original_string: var.lower() in original_string.lower()
    
    >>> howis = raw_input()
    I'm doing GooD
    >>> findphrase("Good")(howis)
    True
    

答案 1 :(得分:1)

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

示例1:

根据您要求退回findphrase()1的方式,我实施0的方式是:

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

但是坦率地说,我不确定你为什么要返回1或0.我只是返回一个布尔值,这会产生这个:

>>> 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)。只有当你从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中的另一种对象。它可以像任何其他对象一样返回。你可能想要使用这样的结构,如果你在这些方面做了些什么:

>>> 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。这将返回一个函数,然后您可以将其称为not_well(phrase)。执行此操作时,它会将您提供给phrase的变量not_well()与您提供给var的变量findphrase()进行比较,并将其存储为部分not_well()的命名空间。

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

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

相关问题