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 open))(NP(DT)(NN门)))
RRC - > ADJP NP
ADJP - > JJ
JJ - > '开放的'
NP - > DT NN
DT - > '的'
NN - > '门'
是否可以在Tree.fromstring中调用stat_parser输出为粗体的输出。
虽然它们是相同的,但是想要避免将它复制粘贴到Tree.fromstring。
CFG.fromstring是否也接受其他CFG输出?
grammar = CFG.fromstring(“”“output”“”)
答案 0 :(得分: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)