0
from stat_parser import Parser
sent = "Open the door"
print parser.parse(sent)

from nltk import Tree
t = Tree.fromstring("(RRC (ADJP (JJ open)) (NP (DT the) (NN door)))")
grammar_from_parse = "\n".join([rule.unicode_repr() for rule in t.productions()])
print grammar_from_parse

The code above outputs

(RRC (ADJP (JJ open)) (NP (DT the) (NN door)))

RRC -> ADJP NP

ADJP -> JJ

JJ -> 'open'

NP -> DT NN

DT -> 'the'

NN -> 'door'

Is it possible to call stat_parser output the one that is bold within Tree.fromstring.

Although they are the same,Idea is to avoid copy pasting it to Tree.fromstring.

Does CFG.fromstring also accepts other CFG output within?

grammar = CFG.fromstring(""" output """)

Programmer_nltk
  • 863
  • 16
  • 38
  • You have to be a little clearer in terms of what you need so that we can help you better. Normally, by specifying what is your input and what is your desired output helps us understand you needs better. By the way, just need to check, are you using this `stat_parser`: https://github.com/emilmont/pyStatParser Or is it something else? – alvas Aug 21 '16 at 12:59

1 Answers1

1

You just need to convert parse output to str().

from stat_parser import Parser
from nltk import Tree, CFG, RecursiveDescentParser

sent = "open the door"
parser = Parser()
print parser.parse(sent)

t = Tree.fromstring(str(parser.parse(sent)))
grammar_from_parse = "\n".join([rule.unicode_repr() for rule in t.productions()])
print grammar_from_parse
grammar = CFG.fromstring(grammar_from_parse)

rd_parser = RecursiveDescentParser(grammar)
for tree in rd_parser.parse(sent.split()):
    print(tree)
RAVI
  • 3,143
  • 4
  • 25
  • 38
  • Additionally is it possible to use nltk parser instead of stat_parser? – Programmer_nltk Aug 21 '16 at 05:05
  • There are multiple grammars available under nltk_data you can use them with nltk or you can create one from nltk treebank corpus. Check: http://www.nltk.org/howto/parse.html and http://stackoverflow.com/q/6115677/1168680 and Point 6 on - http://www.nltk.org/book/ch08.html – RAVI Aug 21 '16 at 09:27