例如,我有一段文字说
t = "The climate is super awesome"
通过做,
from nltk.tokenize import word_tokenize words = word_tokenize(t)
我得到的
>>>words = ["The","climate","is","super","awesome"]
我在词典中有多个列表,每个列表都有一个同义词列表。例如,
dict = {'climate' : [weather,region,zone], 'super' : [excellent, superior, outstanding], 'awesome' : [amazing,great,stunning]}
如何编写代码以获取句子中同义词的排列组合。假设我们每个单词至少有3个已识别同义词。那么在所选的第一行't'中总共有3个单词。因此,可以生成3到3的幂= 27个句子。
以及我想要的输出如何?
The weather is excellent amazing
The weather is excellent great
The weather is excellent stunning
The weather is superior amazing
The weather is superior great
The weather is superior stunning
The weather is outstanding amazing
The weather is outstanding great
The weather is outstanding stunning
The region is excellent amazing
The region is excellent great
The region is excellent stunning
The region is superior amazing
The region is superior great
The region is superior stunning
The region is outstanding amazing
The region is outstanding great
The region is outstanding stunning
The zone is excellent amazing
The zone is excellent great
The zone is excellent stunning
The zone is superior amazing
The zone is superior great
The zone is superior stunning
The zone is outstanding amazing
The zone is outstanding great
The zone is outstanding stunning
关于此的任何帮助,将非常可观。
使用itertools.product
和str.replace
:
words = ["The","climate","is","super","awesome"]
synonyms = {'climate' : ['weather','region','zone'],
'super' : ['excellent', 'superior', 'outstanding'],
'awesome' : ['amazing','great','stunning']}
from itertools import product
s = ' '.join(words)
for val in product(*[[(k, i) for i in v] for k, v in synonyms.items()]):
new_s = s
for (orig, new_one) in val:
new_s = new_s.replace(orig, new_one)
print(new_s)
印刷品:
The weather is excellent amazing
The weather is excellent great
The weather is excellent stunning
The weather is superior amazing
The weather is superior great
The weather is superior stunning
The weather is outstanding amazing
The weather is outstanding great
The weather is outstanding stunning
The region is excellent amazing
The region is excellent great
The region is excellent stunning
The region is superior amazing
The region is superior great
The region is superior stunning
The region is outstanding amazing
The region is outstanding great
The region is outstanding stunning
The zone is excellent amazing
The zone is excellent great
The zone is excellent stunning
The zone is superior amazing
The zone is superior great
The zone is superior stunning
The zone is outstanding amazing
The zone is outstanding great
The zone is outstanding stunning
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句