単語の補題とその複数形を取得する小さなモジュールがあります。次に、両方の単語 (単数形または複数形) をいずれかの順序で含む文を探して文を検索します。私はそれを機能させましたが、この表現を構築するためのよりエレガントな方法があるかどうか疑問に思っていました. ありがとう!注: 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)"
次に、検索:
pat = re.compile(pat)
for sentence in sentences:
if len(pat.findall(sentence)) != 0:
print sentence+'\n'