【问题标题】:how to get a dependency tree with Stanford NLP parser如何使用斯坦福 NLP 解析器获取依赖树
【发布时间】:2012-11-20 08:03:02
【问题描述】:

我怎样才能得到如下图的依赖树。我可以得到纯文本的依赖关系,也可以借助dependencysee工具得到依赖关系图。但是以单词为节点,以依赖为边的依赖树怎么样。非常感谢!

【问题讨论】:

    标签: stanford-nlp


    【解决方案1】:

    这些图表是使用 GraphViz 生成的,这是一个开源图形绘图包,最初来自 AT&T Research。您可以在edu.stanford.nlp.trees.semgraph.SemanticGraph 中找到一个方法toDotFormat(),它将SemanticGraph 转换为dot 可以由dot/GraphViz 呈现的输入语言格式。目前,还没有提供此功能的命令行工具,但使用该方法非常简单。

    【讨论】:

      【解决方案2】:

      这就是你将如何做到这一点(在 python 中

      安装所有需要的依赖项(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
      

      代码:

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

      导致:

      我在阅读Text Analytics With Python时使用了这个:CH3,很好,如果您需要更多关于基于依赖的解析的信息,请参考。

      【讨论】:

        【解决方案3】:

        我现在正在处理类似的事情。这不是一个理想的解决方案,但它可能会有所帮助。如上面的答案所述,使用 toDotFormat() 获取点语言的解析树。然后使用众多工具之一(我正在使用 python-graph)读取这些数据并将其呈现为图片。这个链接上有一个例子http://code.google.com/p/python-graph/wiki/Example

        【讨论】:

          【解决方案4】:

          我也非常需要它;现在很高兴看到我们也有一个在线工具。使用这个:http://graphs.grevian.org/graph(这里提到:http://graphs.grevian.org/

          步骤如下:

          1. 解析句子:

            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. .to_dot() 格式打印为:

            print(p.to_dot())
            
          3. 将输出复制粘贴到http://graphs.grevian.org/graph,然后按“生成”按钮。

          您应该会看到所需的图表。

          【讨论】:

          • 谢谢克里斯托弗。你真好。
          猜你喜欢
          • 2014-04-20
          • 1970-01-01
          • 2015-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多