how to get a dependency tree with Stanford NLP parser

前端 未结 4 716
借酒劲吻你
借酒劲吻你 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条回答
  •  -上瘾入骨i
    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.

提交回复
热议问题