I am working on some sentence formation like this:
sentence = "PERSON is ADJECTIVE"
dictionary = {"PERSON": ["Alice", "Bob", "Carol"], "ADJECTIVE": ["cute", "intelligent"]}
I would now need all possible combinations to form this sentence from the dictionary, like:
Alice is cute
Alice is intelligent
Bob is cute
Bob is intelligent
Carol is cute
Carol is intelligent
The above use case was relatively simple, and it was done with the following code
dictionary = {"PERSON": ["Alice", "Bob", "Carol"], "ADJECTIVE": ["cute", "intelligent"]}
for i in dictionary["PERSON"]:
for j in dictionary["ADJECTIVE"]:
print(f"{i} is {j}")
But can we also make this scale up for longer sentences?
Example:
sentence = "PERSON is ADJECTIVE and is from COUNTRY"
dictionary = {"PERSON": ["Alice", "Bob", "Carol"], "ADJECTIVE": ["cute", "intelligent"], "COUNTRY": ["USA", "Japan", "China", "India"]}
This should again provide all possible combinations like:
Alice is cute and is from USA
Alice is intelligent and is from USA
.
.
.
.
Carol is intelligent and is from India
I tried to use https://www.pythonpool.com/python-permutations/ , but the sentence are all are mixed up - but how can we make a few words fixed, like in this example the words "and is from" is fixed
Essentially if any key in the dictionary is equal to the word in the string, then the word should be replaced by the list of values from the dictionary.
Any thoughts would be really helpful.
itertools.product(*dictionary.values())gives you,str.format()knows how to handle so you don't have to do thereplacestuff yourself.