NLTK 3: Generating the list of possible productions for a node

26 views
Skip to first unread message

Lenin Raj Rajasekaran

unread,
Mar 27, 2015, 7:47:30 AM3/27/15
to nltk-...@googlegroups.com
Given I have a CFG like this:

cfg = nltk.CFG.fromstring("""
root -> who_player has the most runs
who_player -> who
who_player -> which player
who_player -> which team player
who -> 'who'
which -> 'which'
player -> 'player'
team -> 'indian' | 'australian' | 'england'
has -> 'has'
this -> 'this'
most -> 'most'
runs -> 'runs'
""")

Now, cfg.productions() provides a data structure with lhs, rhs etc., 

How do I get the list of possible generations for "who_player" node. Following is the expected result:

(list) ['who', 'which player', 'which indian player', 'which australian player', 'which england player']

Lenin Raj Rajasekaran

unread,
Mar 27, 2015, 11:41:15 AM3/27/15
to nltk-...@googlegroups.com
I just found that we could use 

list((n,sent) for n, sent in enumerate(generate.generate(cfg, n=10, start=Nonterminal('who_player')), 1))

to generate the possible productions of 'who_player' node.
Reply all
Reply to author
Forward
0 new messages