【发布时间】:2014-11-25 10:15:58
【问题描述】:
我正在尝试创建一种方法,该方法可以检查给定短语是否与短语列表中的至少一项匹配并返回它们。输入是短语、短语列表和同义词列表字典。关键是使其具有普遍性。
示例如下:
phrase = 'This is a little house'
dictSyns = {'little':['small','tiny','little'],
'house':['cottage','house']}
listPhrases = ['This is a tiny house','This is a small cottage','This is a small building','I need advice']
我可以在这个返回 bool 的示例中创建一个可以执行此操作的代码:
if any('This'+' '+'is'+' '+'a'+x+' '+y == phrase for x in dictSyns['little'] for y in dictSyns['house']):
print 'match'
第一点是我必须创建通用的函数(取决于结果)。第二个是我希望这个函数返回匹配短语的列表。
您能否给我一个建议,以便在这种情况下该方法返回 ['This is a tiny house','This is a small cottage']?
输出如下:
>>> getMatches(phrase, dictSyns, listPhrases)
['This is a tiny house','This is a small cottage']
【问题讨论】:
标签: python nlp text-processing synonym