Search code examples
pythonlistdictionarysentence

Make all possible sentences from a dictionary python


I have a sentence (Send me bugs from yesterday) and it was converted with synonyms for each word to a dictionary like this:

{'Send': ['direct', 'send_out', 'mail', 'post', 'transport', 'ship', 'station', 'post', 'place', 'get_off', 'send_off', 'commit', 'institutionalize', 'institutionalise', 'charge', 'air', 'broadcast', 'beam', 'transmit'], 
'me': ['me'], 
'bugs': ['glitch', 'hemipterous_insect', 'hemipteran', 'hemipteron', 'microbe', 'germ', 'tease', 'badger', 'pester', 'beleaguer', 'wiretap', 'tap', 'intercept'], 
'from': ['from'], 
'yesterday': ['yesterday']}

Now, I want to generate list of sentences having all possible combinations but the order should be maintained i.e. synonyms of "send" should always come before "me". For example some of the sentences that can be made are:

Direct me glitch from yesterday.
Send_out me glitch from yesterday.
.... Transport me hemipteran from yesterday. etc

The whole code is:-

words_dict={'Send': ['direct', 'send_out', 'mail', 'post', 'transport', 'ship', 'station', 'post', 'place', 'get_off', 'send_off', 'commit', 'institutionalize', 'institutionalise', 'charge', 'air', 'broadcast', 'beam', 'transmit'], 'me': ['me'], 'bugs': ['glitch', 'hemipterous_insect', 'hemipteran', 'hemipteron', 'microbe', 'germ', 'tease', 'badger', 'pester', 'beleaguer', 'wiretap', 'tap', 'intercept'], 'from': ['from'], 'yesterday': ['yesterday']}



for f_word in words_dict.keys()[0]:
    sent=[]
    sent.append(f_word)
    for k2,v2 in words_dict.items():
        for val in v2:
             sent.append(val)
             break

Clearly, my solution is not good and is not giving the output I want, any help will be much appreciated, thanks!


Solution

  • This is a job for itertools: put your lexical components into a list in order. Feed that list of lists to product. It will return a sequence of word lists. Join each returned list into a sentence, and print.

    from itertools import product

    lexicon = [
        ['direct', 'send_out', 'mail', 'post', 'transport', 'ship', 'station', 'post', 'place', 'get_off', 'send_off', 'commit', 'institutionalize', 'institutionalise', 'charge', 'air', 'broadcast', 'beam', 'transmit'],
        ['me'], 
        ['glitch', 'hemipterous_insect', 'hemipteran', 'hemipteron', 'microbe', 'germ', 'tease', 'badger', 'pester', 'beleaguer', 'wiretap', 'tap', 'intercept'], 
        ['from'], 
        ['yesterday']
    ]
    
    for sent in product(*lexicon):
        print(' '.join(sent))
    

    Output:

    direct me glitch from yesterday
    direct me hemipterous_insect from yesterday
    ...
    transmit me wiretap from yesterday
    transmit me tap from yesterday
    transmit me intercept from yesterday