简体   繁体   English

搜索字符串中的多个关键字

[英]Search multiple keywords in string

I'm looking for a way to complete this task as efficient as possible. 我正在寻找一种尽可能高效地完成此任务的方法。 Here is how I want it to work.Say user inputs 这是我希望它起作用的方式。说出用户输入

"My screen is broken"

The script finds the two keywords "screen" and "broken" and then prints an appropriate string. 该脚本找到两个关键字“ screen”和“ broken”,然后打印适当的字符串。 Being a noob I thought I might be able to use a dictionary like this 作为菜鸟,我想我也许可以使用这样的字典

{"screen", "broken", "smashed":"use a repair kit"}

Then I would just search all the keys in the dictionary. 然后,我只搜索字典中的所有键。 But upon further research it seems this is not possible. 但是经过进一步的研究,似乎这是不可能的。

So what would be the best way to do this? 那么什么是最好的方法呢? I thought maybe sql but I was wondering if there was a better way which would involve just python.Thanks 我以为可能是sql,但我想知道是否有更好的方法只涉及python。

Dictionary keys need to be immutable, you could use a tuple, eg 字典键必须是不可变的,您可以使用元组,例如

# {("screen", "broken"): "use a repair kit"}

# input is a keyword in Python, so use input_ instead
input_ = input_.split()
if 'screen' in input_ and 'broken' in input_:
    return "use a repair kit"
solutions = [{'keywords': ['screen', 'broken', 'smashed'], 'solution': 'use a repair kit'}]
s = 'My screen is broken'
words = set(s.lower().split(' '))
print '\n'.join([x.get('solution') for x in solutions if words & set(x.get('keywords', []))])

If you are just looking for "screen" and "broken", something like this can work. 如果您只是在寻找“屏幕”和“残破”,则可以使用这种方法。

sentence = "My screen is broken"
keys = ["screen", "broken"]

if all(i in sentence for i in keys):
    print "Use a repair kit"

Building on zyxue's answer, you could make it check for certain values but not all of them. 基于zyxue的答案,您可以使其检查某些值,但不是全部。 This will work with your above code, but you can nest multiple tuples together if you'd like to group other names. 这将与您上面的代码一起使用,但是如果您想对其他名称进行分组,则可以将多个元组嵌套在一起。

sentence = "My screen is smashed"

solution_dict = {}
solution_dict[("screen", ("broken", "smashed"))] = "use a repair kit"

#If value is a tuple, run function on every value and return if there are any matches
#If not, check the word is part of the sentence
def check_match(sentence_words, keyword):
    if isinstance(keyword, tuple):
        return any([check_match(sentence_words, i) for i in keyword])
    return keyword in sentence_words

#Make sure each value in the main tuple has a match
sentence_words = [i.lower() for i in sentence.split()]
for k,v in solution_dict.iteritems():
    if all(check_match(sentence_words, i) for i in k):
        print v

So you'll get results like this: 这样您将获得如下结果:

>>> sentence = "My screen is smashed"
use a repair kit
>>> sentence = "My screen is smashed and broken"
use a repair kit
>>> sentence = "My screen is broken"
use a repair kit
>>> sentence = "My phone is broken"
(nothing)

To work with phone too, along with iphone and android, you could set it like this, having iphone and android in another tuple makes no difference but just groups it a little better. 要与iPhone以及iPhone和android一起使用,也可以这样设置,将iPhone和android放在另一个元组中没有什么区别,只是将其分组更好。
solution_dict[(("screen", "phone", ("android", "iphone")), ("broken", "smashed"))] = "use a repair kit"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM