gpt4 book ai didi

python - 从字典 python 中创建所有可能的句子

转载 作者:太空宇宙 更新时间:2023-11-04 11:13:34 26 4
gpt4 key购买 nike

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

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

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

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。它将返回一系列单词列表。将每个返回的列表连接成一个句子,然后打印。

从 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

关于python - 从字典 python 中创建所有可能的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57580850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com