2016-08-21 45 views
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 

上面的代码输出调用输出

(RRC(ADJP(JJ开))(NP(DT的)(NN门)))

RRC - > ADJP NP

ADJP - > JJ

JJ - > '打开'

NP - > DT NN

DT - > '的'

NN - > '门'

是否可以调用stat_parser输出一个是内Tree.fromstring大胆。

虽然它们是一样的,但Idea是为了避免将它粘贴到Tree.fromstring上。

CFG.fromstring是否也接受其他CFG输出?

语法= CFG.fromstring( “” “输出 ”“”)

+0

您需要更清楚一点,以便我们能更好地帮助您。通常,通过指定什么是您的输入,以及您希望的输出有助于我们理解您的需求。顺便说一句,只需要检查一下,你使用这个'stat_parser':https://github.com/emilmont/pyStatParser或者是其他的东西? – alvas

回答

1

你只需要解析输出转换为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) 
+0

另外有可能使用nltk解析器而不是stat_parser? –

+0

在nltk_data下可以使用多种语法,您可以将它们与nltk一起使用,也可以从nltk treebank语料库创建一个语法。检查:http://www.nltk.org/howto/parse.html和http://stackoverflow.com/q/6115677/1168680和第6点 - http://www.nltk.org/book/ch08。 HTML – RAVI