从字典中造出所有可能的句子 python

Make all possible sentences from a dictionary python

我有一个句子(把昨天的错误发给我),它被每个单词的同义词转换成这样的字典:

{'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']}

现在,我想生成具有所有可能组合的句子列表,但应保持顺序,即 "send" 的同义词应始终出现在 "me" 之前。例如,一些可以造的句子是:

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

整个代码是:-

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

很明显,我的解决方案不好,没有给出我想要的输出,任何帮助将不胜感激,谢谢!

这是 itertools 的工作:将您的词汇成分按顺序放入列表中。将该列表列表提供给 product。它将 return 一系列单词列表。将每个 returned 列表加入一个句子,然后打印。

从 itertools 导入产品

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))

输出:

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

您可以对生成器使用递归:

data = {'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']}
new_d = [b for a, b in data.items()]
def group(d, c = []):
  if len(c) == len(new_d):
    yield ' '.join(c).capitalize()
  if d:
    for i in d[0]:
      yield from group(d[1:], c+[i])

print(list(group(new_d)))

输出:

['Direct me glitch from yesterday', 'Direct me hemipterous_insect from yesterday', 'Direct me hemipteran from yesterday', 'Direct me hemipteron from yesterday', 'Direct me microbe from yesterday', 'Direct me germ from yesterday', 'Direct me tease from yesterday', 'Direct me badger from yesterday', 'Direct me pester from yesterday', 'Direct me beleaguer from yesterday', 'Direct me wiretap from yesterday', 'Direct me tap from yesterday', 'Direct me intercept from yesterday', 'Send_out me glitch from yesterday', 'Send_out me hemipterous_insect from yesterday', 'Send_out me hemipteran from yesterday', 'Send_out me hemipteron from yesterday', 'Send_out me microbe from yesterday', 'Send_out me germ from yesterday', 'Send_out me tease from yesterday', 'Send_out me badger from yesterday', 'Send_out me pester from yesterday', 'Send_out me beleaguer from yesterday', 'Send_out me wiretap from yesterday', 'Send_out me tap from yesterday', 'Send_out me intercept from yesterday', 'Mail me glitch from yesterday', 'Mail me hemipterous_insect from yesterday', 'Mail me hemipteran from yesterday', 'Mail me hemipteron from yesterday', 'Mail me microbe from yesterday', 'Mail me germ from yesterday', 'Mail me tease from yesterday', 'Mail me badger from yesterday', 'Mail me pester from yesterday', 'Mail me beleaguer from yesterday', 'Mail me wiretap from yesterday', 'Mail me tap from yesterday', 'Mail me intercept from yesterday', 'Post me glitch from yesterday', 'Post me hemipterous_insect from yesterday', 'Post me hemipteran from yesterday', 'Post me hemipteron from yesterday', 'Post me microbe from yesterday', 'Post me germ from yesterday', 'Post me tease from yesterday', 'Post me badger from yesterday', 'Post me pester from yesterday', 'Post me beleaguer from yesterday', 'Post me wiretap from yesterday', 'Post me tap from yesterday', 'Post me intercept from yesterday', 'Transport me glitch from yesterday', 'Transport me hemipterous_insect from yesterday', 'Transport me hemipteran from yesterday', 'Transport me hemipteron from yesterday', 'Transport me microbe from yesterday', 'Transport me germ from yesterday', 'Transport me tease from yesterday', 'Transport me badger from yesterday', 'Transport me pester from yesterday', 'Transport me beleaguer from yesterday', 'Transport me wiretap from yesterday', 'Transport me tap from yesterday', 'Transport me intercept from yesterday', 'Ship me glitch from yesterday', 'Ship me hemipterous_insect from yesterday', 'Ship me hemipteran from yesterday', 'Ship me hemipteron from yesterday', 'Ship me microbe from yesterday', 'Ship me germ from yesterday', 'Ship me tease from yesterday', 'Ship me badger from yesterday', 'Ship me pester from yesterday', 'Ship me beleaguer from yesterday', 'Ship me wiretap from yesterday', 'Ship me tap from yesterday', 'Ship me intercept from yesterday', 'Station me glitch from yesterday', 'Station me hemipterous_insect from yesterday', 'Station me hemipteran from yesterday', 'Station me hemipteron from yesterday', 'Station me microbe from yesterday', 'Station me germ from yesterday', 'Station me tease from yesterday', 'Station me badger from yesterday', 'Station me pester from yesterday', 'Station me beleaguer from yesterday', 'Station me wiretap from yesterday', 'Station me tap from yesterday', 'Station me intercept from yesterday', 'Post me glitch from yesterday', 'Post me hemipterous_insect from yesterday', 'Post me hemipteran from yesterday', 'Post me hemipteron from yesterday', 'Post me microbe from yesterday', 'Post me germ from yesterday', 'Post me tease from yesterday', 'Post me badger from yesterday', 'Post me pester from yesterday', 'Post me beleaguer from yesterday', 'Post me wiretap from yesterday', 'Post me tap from yesterday', 'Post me intercept from yesterday', 'Place me glitch from yesterday', 'Place me hemipterous_insect from yesterday', 'Place me hemipteran from yesterday', 'Place me hemipteron from yesterday', 'Place me microbe from yesterday', 'Place me germ from yesterday', 'Place me tease from yesterday', 'Place me badger from yesterday', 'Place me pester from yesterday', 'Place me beleaguer from yesterday', 'Place me wiretap from yesterday', 'Place me tap from yesterday', 'Place me intercept from yesterday', 'Get_off me glitch from yesterday', 'Get_off me hemipterous_insect from yesterday', 'Get_off me hemipteran from yesterday', 'Get_off me hemipteron from yesterday', 'Get_off me microbe from yesterday', 'Get_off me germ from yesterday', 'Get_off me tease from yesterday', 'Get_off me badger from yesterday', 'Get_off me pester from yesterday', 'Get_off me beleaguer from yesterday', 'Get_off me wiretap from yesterday', 'Get_off me tap from yesterday', 'Get_off me intercept from yesterday', 'Send_off me glitch from yesterday', 'Send_off me hemipterous_insect from yesterday', 'Send_off me hemipteran from yesterday', 'Send_off me hemipteron from yesterday', 'Send_off me microbe from yesterday', 'Send_off me germ from yesterday', 'Send_off me tease from yesterday', 'Send_off me badger from yesterday', 'Send_off me pester from yesterday', 'Send_off me beleaguer from yesterday', 'Send_off me wiretap from yesterday', 'Send_off me tap from yesterday', 'Send_off me intercept from yesterday', 'Commit me glitch from yesterday', 'Commit me hemipterous_insect from yesterday', 'Commit me hemipteran from yesterday', 'Commit me hemipteron from yesterday', 'Commit me microbe from yesterday', 'Commit me germ from yesterday', 'Commit me tease from yesterday', 'Commit me badger from yesterday', 'Commit me pester from yesterday', 'Commit me beleaguer from yesterday', 'Commit me wiretap from yesterday', 'Commit me tap from yesterday', 'Commit me intercept from yesterday', 'Institutionalize me glitch from yesterday', 'Institutionalize me hemipterous_insect from yesterday', 'Institutionalize me hemipteran from yesterday', 'Institutionalize me hemipteron from yesterday', 'Institutionalize me microbe from yesterday', 'Institutionalize me germ from yesterday', 'Institutionalize me tease from yesterday', 'Institutionalize me badger from yesterday', 'Institutionalize me pester from yesterday', 'Institutionalize me beleaguer from yesterday', 'Institutionalize me wiretap from yesterday', 'Institutionalize me tap from yesterday', 'Institutionalize me intercept from yesterday', 'Institutionalise me glitch from yesterday', 'Institutionalise me hemipterous_insect from yesterday', 'Institutionalise me hemipteran from yesterday', 'Institutionalise me hemipteron from yesterday', 'Institutionalise me microbe from yesterday', 'Institutionalise me germ from yesterday', 'Institutionalise me tease from yesterday', 'Institutionalise me badger from yesterday', 'Institutionalise me pester from yesterday', 'Institutionalise me beleaguer from yesterday', 'Institutionalise me wiretap from yesterday', 'Institutionalise me tap from yesterday', 'Institutionalise me intercept from yesterday', 'Charge me glitch from yesterday', 'Charge me hemipterous_insect from yesterday', 'Charge me hemipteran from yesterday', 'Charge me hemipteron from yesterday', 'Charge me microbe from yesterday', 'Charge me germ from yesterday', 'Charge me tease from yesterday', 'Charge me badger from yesterday', 'Charge me pester from yesterday', 'Charge me beleaguer from yesterday', 'Charge me wiretap from yesterday', 'Charge me tap from yesterday', 'Charge me intercept from yesterday', 'Air me glitch from yesterday', 'Air me hemipterous_insect from yesterday', 'Air me hemipteran from yesterday', 'Air me hemipteron from yesterday', 'Air me microbe from yesterday', 'Air me germ from yesterday', 'Air me tease from yesterday', 'Air me badger from yesterday', 'Air me pester from yesterday', 'Air me beleaguer from yesterday', 'Air me wiretap from yesterday', 'Air me tap from yesterday', 'Air me intercept from yesterday', 'Broadcast me glitch from yesterday', 'Broadcast me hemipterous_insect from yesterday', 'Broadcast me hemipteran from yesterday', 'Broadcast me hemipteron from yesterday', 'Broadcast me microbe from yesterday', 'Broadcast me germ from yesterday', 'Broadcast me tease from yesterday', 'Broadcast me badger from yesterday', 'Broadcast me pester from yesterday', 'Broadcast me beleaguer from yesterday', 'Broadcast me wiretap from yesterday', 'Broadcast me tap from yesterday', 'Broadcast me intercept from yesterday', 'Beam me glitch from yesterday', 'Beam me hemipterous_insect from yesterday', 'Beam me hemipteran from yesterday', 'Beam me hemipteron from yesterday', 'Beam me microbe from yesterday', 'Beam me germ from yesterday', 'Beam me tease from yesterday', 'Beam me badger from yesterday', 'Beam me pester from yesterday', 'Beam me beleaguer from yesterday', 'Beam me wiretap from yesterday', 'Beam me tap from yesterday', 'Beam me intercept from yesterday', 'Transmit me glitch from yesterday', 'Transmit me hemipterous_insect from yesterday', 'Transmit me hemipteran from yesterday', 'Transmit me hemipteron from yesterday', 'Transmit me microbe from yesterday', 'Transmit me germ from yesterday', 'Transmit me tease from yesterday', 'Transmit me badger from yesterday', 'Transmit me pester from yesterday', 'Transmit me beleaguer from yesterday', 'Transmit me wiretap from yesterday', 'Transmit me tap from yesterday', 'Transmit me intercept from yesterday']