I have a text say for example,
t = "The climate is super awesome"
by doing,
from nltk.tokenize import word_tokenize
words = word_tokenize(t)
what I got,
>>>words = ["The","climate","is","super","awesome"]
and I have multiple lists in a dictionary each having a list of synonyms. for example,
dict = {'climate' : [weather,region,zone], 'super' : [excellent, superior, outstanding], 'awesome' : [amazing,great,stunning]}
How to write the code to get the permutation combinations of the synonyms in the sentence. Assume that we have at least or exactly 3 identified synonyms for each of our words. Then totally there are 3 words in the selected first line of 't'. Therefore 3 to the power of 3 sentences = 27 sentences are possible to generate.
and how the output I want ?
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
Any help regarding this, will be really appreciable.
Using itertools.product
and 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)
Prints:
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