绘制解析树的工具?

时间:2011-02-11 18:16:19

标签: graphviz context-free-grammar parse-tree

有没有人有一个很好的工具来绘制由无上下文语法引起的解析树?有this question,但它专门处理有限自动机而不是解析树。我一直在使用graphviz,但是必须单独标记每个节点等等。

4 个答案:

答案 0 :(得分:2)

您可以使用http://ironcreek.net/phpsyntaxtree/

示例:

enter image description here

输入是:

[ROOT
  [S
    [S
      [NP [PRP It]]
      [VP [VBZ is]
        [NP
          [QP [RB nearly] [DT half] [JJ past] [CD five]]]]]
    [, ,]
    [S
      [NP [PRP we]]
      [VP [MD can] [RB not]
        [VP [VB reach]
          [NP [NN town]]
          [PP [IN before]
            [NP [NN dark]]]]]]
    [, ,]
    [S
      [NP [PRP we]]
      [VP [MD will]
        [VP [VB miss]
          [NP [NN dinner]]]]]
    [. .]]]

答案 1 :(得分:1)

ANTLRWorks会自动执行此操作,但您必须首先编写与ANTLR兼容的语法。由于ANTLR是LL(*),这可能不是微不足道的。源代码也可用。

答案 2 :(得分:0)

如果您有一个写得很好的无上下文语法(CFG),则可以使用nltk库简单地绘制树形图。

import nltk
#defining Contex Free Grammar
grammar = nltk.CFG.fromstring("""
  S  -> NP VP
  NP -> Det Nom | PropN
  Nom -> Adj Nom | N
  VP -> V Adj | V NP | V S | V NP PP
  PP -> P NP
  PropN -> 'Buster' | 'Chatterer' | 'Joe'
  Det -> 'the' | 'a'
  N -> 'bear' | 'squirrel' | 'tree' | 'fish' | 'log'
  Adj  -> 'angry' | 'frightened' |  'little' | 'tall'
  V ->  'chased'  | 'saw' | 'said' | 'thought' | 'was' | 'put'
  P -> 'on'
  """)

sentence = 'the angry bear chased the frightened little squirrel'.split()
def parse(sent):
    #Returns nltk.Tree.Tree format output
    a = []  
    parser = nltk.ChartParser(grammar)
    for tree in parser.parse(sent):
        a.append(tree)
    return(a[0]) 

#Gives output as structured tree   
print(parse(sentence))

#Gives tree diagrem in tkinter window
parse(sentence).draw()

结构化树输出

Tkinter窗口中的树图

答案 3 :(得分:0)

http://brenocon.com/parseviz/

输入:

(S (NP (DT The) (NN man)) (VP (VBZ is) (VP (VBG running) (PP (IN on) (NP (DT the) (NN mountain))))) (. .))

输出:

enter image description here