逐句查找关键词

时间:2018-05-11 03:23:36

标签: python python-3.x

我正在尝试使用Python来搜索句子中的关键字。我发现Chris_Rands提供的一个编码确实很有帮助,但我想改变输出格式。这是代码及其输出,然后是我想要的新输出。

您能否建议更改代码以创建新输出?

来自Chris_Rands的原始代码

Selenium::WebDriver::Firefox::Binary.path = 'C:\Ruby24-x64\bin C:\Users\farid\AppData\Local\Google\Chrome\application\chrome.exe'

Chris_Rands的输出:

sentences = "My name is sing song. I am a mother. I am happy. You sing like my mother".split(".")
search_keywords=['mother','sing','song']

for sentence in sentences:
    print("{} key words in sentence:".format(sum(1 for word in search_keywords if word in sentence)))
    for word in search_keywords:
        if word in sentence:
          print(word)
    print(sentence + "\n")

我想知道如何打印这些"关键字"在结果中,结果将如下所示:

所需输出

2 key words in sentence:
My name is sing song

1 key words in sentence:
 I am a mother

0 key words in sentence:
 I am happy

2 key words in sentence:
 You sing like my mother

0 个答案:

没有答案