我正在尝试斯坦福大学的新 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/