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. Thanks! 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'
something like: