Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
82 views
in Technique[技术] by (71.8m points)

python - Is there a simple way to switch the order of a CFG using nltk?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...