[英]Python Context Free Grammar and PCFG generation benchmarks?
[英]NLTK - generate text from probabilistic context free grammar (PCFG)
我有一個上下文無關的語法,並用它來創建句子(在python中使用NLTK)。
# Create a CFG
from nltk import CFG
from nltk.parse.generate import generate
grammar = CFG.fromstring("""
Story -> Introduction MainQuest End
LocationInfo -> 'He found himself in a small village where he grew up.'
Introduction -> 'Long ago there was a boy who decided to become a knight.'
MainQuest -> LocationInfo 'He had to get a sword first to fight monsters' Navigate
Navigate -> '[He could go west]' GoodEnd | '[He could go east]' BadEnd
GoodEnd -> 'And he lived happily ever after.'
BadEnd -> 'Finally he died painfully.'
End -> 'The End'
""")
#print(grammar.start())
#print(grammar.productions())
for sentence in generate(grammar, n=2):
print('\n'.join(sentence))
print('\n')
這很容易並且有效。 但是現在,我想在特殊情況下添加概率,以便根據給定概率的隨機因素,我生成的故事的結局可以是好是壞。
我找不到任何這樣做的示例,當我將PCFG饋入nltk.parse.generate時,它會將其視為CFG。
希望你能幫幫我!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.