java - 从语法结构到树

标签 java nlp stanford-nlp

我正在尝试斯坦福大学的新 NN 依赖解析器。根据他们提供的演示,解析是这样完成的:

import edu.stanford.nlp.process.DocumentPreprocessor;
import edu.stanford.nlp.trees.GrammaticalStructure;
import edu.stanford.nlp.parser.nndep.DependencyParser;

...

GrammaticalStructure gs = null;

DocumentPreprocessor tokenizer = new DocumentPreprocessor(new StringReader(sentence));
for (List<HasWord> sent : tokenizer) {
    List<TaggedWord> tagged = tagger.tagSentence(sent);
    gs = parser.predict(tagged);

    // Print typed dependencies
    System.out.println(Grammatical structure: " + gs);
} 

现在,我想要做的是这个属于 GrammaticalStructure 类的对象 gs,将其转换为 edu.stanford.nlp.trees.Tree 中的 Tree 对象。

我天真地尝试了简单的转换:

Tree t = (Tree) gs;

但是,这是不可能的(IDE 给出错误:无法从 GrammaticalStructure 转换为 Tree)。

我该怎么做?

最佳答案

您应该能够使用 gs.root() 获取树。 根据documentation ,该方法返回一个表示语法结构的 Tree(实际上是一个 TreeGraphNode)。

您可以使用 gs.root().pennPrint() 以人性化的方式打印该树。

关于java - 从语法结构到树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32565312/

相关文章:

java - NullPointerException 创建自己的 JSON 响应对象

nlp - 两句话意思的距离

scala - 是否可以将 word2vec 预训练的可用向量加载到 Spark 中?

java 将长时间转换为 java.util.Date

java - POST 500(内部服务器错误) - AngularJS - Jersey API

java - JasperReports fillReport太慢且耗资源

python - 从关键字生成句子。每个关键字都有相关单词包

statistics - 分类时计算置信度

nlp - 准确度:UIMA的ANNIE,斯坦福大学,OpenNLP

java - 加载 NER 分类器时出错 - ZLIB 输入流意外结束