Tool for drawing parse trees? [closed]
Asked Answered
B

3

5

Does anyone have a good tool for drawing parse trees arising from a context-free grammar? There is this question, but it dealt specifically with finite automata instead of parse trees. I've been using graphviz, but it's kind of annoying to have to label each node individually etc.

Barbarian answered 11/2, 2011 at 18:16 Comment(3)
Are you trying to visualize the parse tree of a parser's output, or the parser itself?Siloa
@Flynn1179: the output. But I don't want to write a parser, I want something that is like paint, i.e. I just draw some trees.Barbarian
Moved to software recommendations SE: Tool for drawing parse treesCredenza
C
4

You can use http://ironcreek.net/phpsyntaxtree/.

Example:

enter image description here

Input was:

[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]]]]]
    [. .]]]

Also works if the string has no line breaks, e.g.:

[S [NP [DT The] [NN man]] [VP [VBZ is] [VP [VBG running] [PP [IN on] [NP [DT the] [NN mountain]]]]] [. .]]
Credenza answered 8/1, 2018 at 4:25 Comment(0)
C
2

If you have a well-written context-free grammar(CFG) then you can draw the tree diagram simply by using nltk library.

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()

Structured tree output

Tree Diagram in Tkinter Window

Comeau answered 27/6, 2018 at 9:48 Comment(0)
C
2

http://brenocon.com/parseviz/:

Input:

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

Output:

enter image description here

Credenza answered 17/4, 2020 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.