现在我正在使用itertools
为网站创建数千种元描述变体。句子结构如下:
(SentencePart1a
| SentencePart1b
)Keyword1
,Keyword2
和Keyword3
。 (MiddleSentenceA
| MiddleSentenceB
)。 (FinalSentenceA
| FinalSentenceB
)。
脚本的简短版本:
import itertools
metas = [
['Shop our store for ', 'Browse our shop for ,'], #SentenceParts
['keyword,', 'keyword,', 'etc,'],
['keyword, and', 'keyword, and', 'etc, and'],
['keyword.', 'keyword.', 'etc.'],
['Sentence about our store.', 'A different sentence about our store.'], #MiddleSentences
['A final sentence about our store.', 'A different final sentence about our store.'] #FinalSentences
]
variantmetas = list(itertools.product(*metas))
#print (variantmetas)
for s in variantmetas:
print(' '.join(s))
现在我得到了所有这些东西的每一个变化。即使关键字1-3是相同的,我的程序也会吐出所有句子部分,所有中间句子和所有最终句子。
如何设置关键字1-3只显示一次,并且句子部分随机变化?换句话说:关键字的所有变体只有一次,一个SentencePart
,一个MiddleSentence
和一个FinalSentence
。
我正在努力减少最终列表中的冗余。
答案 0 :(得分:1)
只需在关键字上执行产品,而不是在句子上执行。
在开始时使用random.choice
&结束句子,并使用列表理解生成结果。
提案没有重新组织metas
列表,这可能更好地将关键词与开始/结束句子隔离开来:
import itertools,random
metas = [
['Shop our store for ', 'Browse our shop for ,'], #SentenceParts
['keyword,', 'keyword,', 'etc,'],
['keyword, and', 'keyword, and', 'etc, and'],
['keyword.', 'keyword.', 'etc.'],
['Sentence about our store.', 'A different sentence about our store.'], #MiddleSentences
['A final sentence about our store.', 'A different final sentence about our store.'] #FinalSentences
]
variantmetas = [[random.choice(metas[0])]+list(l)+[random.choice(metas[-2]),random.choice(metas[-1])] for l in itertools.product(*metas[1:-2])]
for s in variantmetas:
print(' '.join(s))
结果摘录:
Shop our store for etc, keyword, and keyword. Sentence about our store. A different final sentence about our store.
Browse our shop for , etc, keyword, and etc. Sentence about our store. A different final sentence about our store.
Browse our shop for , etc, etc, and keyword. Sentence about our store. A final sentence about our store.
Shop our store for etc, etc, and keyword. Sentence about our store. A different final sentence about our store.
Shop our store for etc, etc, and etc. Sentence about our store. A final sentence about our store.