I have a small module that gets the lemma of a word and its plural form. It then searches through sentences looking for a sentence that contains both words (singular or plural) in either order. I have it working but I was wondering if there is a more elegant way to build this expression.
Note: Python2
words = ((cell,), (wolf,wolves))
string1 = "(?:"+"|".join(words[0])+")"
string2 = "(?:"+"|".join(words[1])+")"
pat = ".+".join((string1, string2)) +"|"+ ".+".join((string2, string1))
# Pat output: "(?:cell).+(?:wolf|wolves)|(?:wolf|wolves).+(?:cell)"
Then the search:
pat = re.compile(pat)
for sentence in sentences:
if len(pat.findall(sentence)) != 0:
print sentence+'\n'
Alternatively, would this be a good solution?
words = ((cell,), (wolf,wolves))
for sentence in sentences:
sentence = sentence.lower()
if any(word in sentence for word in words[0]) and any(word in sentence for word in words[1]):
print sentence