我有一段简单的代码,告诉我给定列表中的一个单词是否出现在一篇文章中:
 if not any(word in article.text for word in keywords):
        print("Skipping article as there is no matching keyword\n")我需要的是,如果“关键字”列表中至少有3个单词出现在文章中--如果它们没有出现,那么应该跳过这篇文章。
有什么简单的方法吗?我似乎什么都找不到。
发布于 2016-01-06 02:40:15
我的短信和列表都很长
如果文本很大,并且有许多关键字,那么可以使用Aho-Corasick算法 (如grep -Ff keywords.txt text.txt),例如,如果要查找不重叠的事件,可以使用套餐 (未测试):
#!/usr/bin/env python
from itertools import islice
from noaho import NoAho  # $ pip install noaho
trie = NoAho()
for word in keywords:
    trie.add(word)
found_words = trie.findall_long(article.text)
if len(list(islice(found_words, 3))) == 3:
    print('at least 3 words in the "keywords" list appear in the article')https://stackoverflow.com/questions/34605144
复制相似问题