是否有一个库/函数可以用输入的关键短语或单词生成句子



考虑这句话:

"给我三星的平均订单率"->我们可以把这个句子分解成关键短语,比如:

<action> <aggregate> <metric> <preposition> <organisation/brand> [1]

现在,对于每个关键短语,我们都可以有一个名称/输入列表。例如:

action -> ["get me", "find me", "what is", ...]
aggregate -> ["average", "net", "total", "mean", ...]
metric -> ["order rate", "request rate", "revenue", "profit",...]
preposition ->["for", "of",...]
organisation/brand -> ["Samsung", "ESPN", "Johnnie Walker" ...]

有没有一种方法可以通过使用关键短语名称/输入的所有组合来生成遵循[1]顺序的句子?感谢您的帮助。

谢谢。

对于从一些列表中生成所有可能的组合的暴力,您可以使用itertools.product

import itertools
actions = ["get me", "find me", "what is"]
aggregates = ["average", "net", "total", "mean"]
metrics = ["order rate", "request rate", "revenue", "profit"]
prepositions = ["for", "of"]
organisations = ["Samsung", "ESPN", "Johnnie Walker"]
sentence_format = "{action} {aggregate} {metric} {preposition} {organisation}"
# go through each possible combination
for action, aggregate, metric,preposition, organisation in itertools.product(actions, aggregates, metrics, prepositions, organisations):
print(sentence_format.format(**locals())) # pass the values from the product

这给了你:

get me average order rate for Samsung
get me average order rate for ESPN
get me average order rate for Johnnie Walker
get me average order rate of Samsung
get me average order rate of ESPN
...
what is mean profit for ESPN
what is mean profit for Johnnie Walker
what is mean profit of Samsung
what is mean profit of ESPN
what is mean profit of Johnnie Walker

最新更新