pythonalgorithmstring-algorithm

Python coding relating to function any and "more than once" keyword


I have this simple piece of code that tells me if a word in a given list appears in an article:

 if not any(word in article.text for word in keywords):
        print("Skipping article as there is no matching keyword\n")

What I need is if at least 3 words in the "keywords" list appear in the article - if they don't then it should skip the article.

Is there an easy way to do this? I can't seem to find anything.


Solution

  • If the set of keywords is large enough and the string being searched is long enough that it's often worth short-circuiting, a variation on other approaches that will stop when three hits are found (much like any stops when one hit found):

    from itertools import islice
    
    if sum(islice((1 for word in keywords if word in article.text), 3)) == 3:
    

    Once you get three hits, it immediately stops iterating the keywords and the test passes.