python - Grammar rule extraction from parsed result -
i following result when execute stanford parser nltk.
(s (vp (vb get) (np (prp me)) (advp (rb now))))
but need in form
s -> vp vp -> vb np advp vb -> prp -> me rb ->
how can result, perhaps using recursive function. there in-built function already?
first navigate tree, see how iterate through nodes of tree? , how navigate nltk.tree.tree? :
>>> nltk.tree import tree >>> bracket_parse = "(s (vp (vb get) (np (prp me)) (advp (rb now))))" >>> ptree = tree.fromstring(bracket_parse) >>> ptree tree('s', [tree('vp', [tree('vb', ['get']), tree('np', [tree('prp', ['me'])]), tree('advp', [tree('rb', ['now'])])])]) >>> subtree in ptree.subtrees(): ... print subtree ... (s (vp (vb get) (np (prp me)) (advp (rb now)))) (vp (vb get) (np (prp me)) (advp (rb now))) (vb get) (np (prp me)) (prp me) (advp (rb now)) (rb now)
and you're looking https://github.com/nltk/nltk/blob/develop/nltk/tree.py#l341:
>>> ptree.productions() [s -> vp, vp -> vb np advp, vb -> 'get', np -> prp, prp -> 'me', advp -> rb, rb -> 'now']
note tree.productions()
returns production
object, see https://github.com/nltk/nltk/blob/develop/nltk/tree.py#l22 , https://github.com/nltk/nltk/blob/develop/nltk/grammar.py#l236.
if want string form of grammar rules, can either do:
>>> rule in ptree.productions(): ... print rule ... s -> vp vp -> vb np advp vb -> 'get' np -> prp prp -> 'me' advp -> rb rb -> 'now'
or
>>> rules = [str(p) p in ptree.productions()] >>> rules ['s -> vp', 'vp -> vb np advp', "vb -> 'get'", 'np -> prp', "prp -> 'me'", 'advp -> rb', "rb -> 'now'"]
Comments
Post a Comment