I have str with a phrase, for example:
phrase = "My cat has two eyes and like to catch rats"
And I have a set of words
that I would like to matche at least 3 of theses words in the phrase.
words = set(["eyes", "like", "cat"])
Currently I have the following code
found = bool(set(phrase.lower().split()) & words)
But it matches if any of the words are in the phrase, and I want at least 3 words matching.
What I can do to achieve this? I don't want to use regex
.
You can check if the length of the intersection is at least 3.
found = len(set(phrase.lower().split()).intersection(words)) >= 3
You can do something like the following:
from typing import Set
def words_matcher(phrase: str, words: Set[str], threshold: int = 3) -> bool:
phrase_as_set = set(phrase.lower().split())
common_words = phrase_as_set.intersection(words)
return len(common_words) >= threshold
You are almost there. &
performs intersection on the set
objects. But instead of doing bool
, you need to get the length
and check whether it is >=3. Hence use this:
>>> phrase = "My cat has two eyes and like to catch rats"
>>> words = set(["eyes", "like", "cat"])
>>> len(set(phrase.lower().split()) & words) >= 3
True
If you want to check if all words in your set appear in phrase you might check if it is subset ie
phrase = "My cat has two eyes and like to catch rats"
words = set(["eyes", "like", "cat"])
print(words.issubset(phrase.lower().split())) # True
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.