I am using python and CFG from nltk and importing a grammar like this:
grammar = CFG.fromstring("""
S -> NP VP
NP -> Det N
PP -> P NP
VP -> V NP | VP PP
V -> 'eats'
Det -> 'a'
N -> 'fish' | 'fork'
P -> 'with'
NP -> 'she'
VP -> 'eats'
""")
I know that from here I can do
for rule in grammar.productions():
if rule.rhs() == target:
achievement_unlocked = True
But the rhs is more verbose so I would really like to be able to have a table going the other way. Is there any nltk-y fast way to get a cfg.fromstring that has all of the RHS on the LHS and the LHS on the RHS?
End goal is this:
for rule in grammar.productions():
if target in rule.lhs():
achievement_unlocked = True
question from:
https://stackoverflow.com/questions/65927020/is-there-a-simple-way-to-switch-the-order-of-a-cfg-using-nltk 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…