I'm trying to find keywords within a sentence, where the keywords are usually single words, but can be multi-word combos (like "cost in euros"). So if I have a sentence like cost in euros of bacon
it would find cost in euros
in that sentence and return true.
For this, I was using this code:
if any(phrase in line for phrase in keyword['aliases']:
where line
is the input and aliases
is an array of phrases that match a keyword (like for cost in euros, it's ['cost in euros', 'euros', 'euro cost']
).
However, I noticed that it was also triggering on word parts. For example, I had a match phrase of y
and a sentence of trippy cake
. I'd not expect this to return true, but it does, since it apparently finds the y
in trippy
. How do I get this to only check whole words? Originally I was doing this keyword search with a list of words (essentially doing line.split()
and checking those), but that doesn't work for multi-word keyword aliases.