how to get a dependency tree with Stanford NLP parser

前端 未结 4 717
借酒劲吻你
借酒劲吻你 2020-12-16 16:22

How can I get the dependency tree as Figure below. I can get the dependency relation as pure text, and also the dependency graph with the help of dependencysee tool. But how

相关标签:
4条回答
  • 2020-12-16 16:54

    These graphs are produced using GraphViz, an open source graph drawing package, originally from AT&T Research. You can find a method toDotFormat() in edu.stanford.nlp.trees.semgraph.SemanticGraph that will convert a SemanticGraph into dot input language format which can be rendered by dot/GraphViz. At present, there isn't a command-line tool that provides this functionality, but it's pretty straightforward using that method.

    0 讨论(0)
  • 2020-12-16 17:06

    I'm dealing with something similar at the moment. This is not an ideal solution but it may be helpful. as mentioned in the answer above, use the toDotFormat() to get the parse trees in dot language. then use one of the many tools (i'm using python-graph) to read this data and render it as a picture. there is an example on this link http://code.google.com/p/python-graph/wiki/Example

    0 讨论(0)
  • 2020-12-16 17:08

    Here is how you would do exactly that (in python)

    Installing all needed dependencies (OS X):

    # assuming you have java installed and available in PATH
    # and homebrew installed
    
    brew install stanford-parser
    brew install graphviz
    pip install nltk
    pip install graphviz
    

    code:

    import os
    from nltk.parse.stanford import StanfordDependencyParser
    from graphviz import Source
    
    # make sure nltk can find stanford-parser
    # please check your stanford-parser version from brew output (in my case 3.6.0) 
    os.environ['CLASSPATH'] = r'/usr/local/Cellar/stanford-parser/3.6.0/libexec'
    
    sentence = 'The brown fox is quick and he is jumping over the lazy dog'
    
    sdp = StanfordDependencyParser()
    result = list(sdp.raw_parse(sentence))
    
    dep_tree_dot_repr = [parse for parse in result][0].to_dot()
    source = Source(dep_tree_dot_repr, filename="dep_tree", format="png")
    source.view()
    

    which results in:

    I used this when reading Text Analytics With Python: CH3, good read, please reference if you need more info about dependency-based parsing.

    0 讨论(0)
  • 2020-12-16 17:19

    I also badly needed it; now it's great to see that we have an online tool as well. Use this: http://graphs.grevian.org/graph (as mentioned here: http://graphs.grevian.org/ )

    The steps are:

    1. Parse the sentence:

      sent = 'What is the step by step guide to invest in share market in india?'
      p = dep_parser.raw_parse(sent)
      for e in p:
          p = e
          break
      
    2. Print the .to_dot() format as:

      print(p.to_dot())
      
    3. Copy paste the output to http://graphs.grevian.org/graph and press the Generate button.

    You should see the desired graph.

    0 讨论(0)
提交回复
热议问题