从语法结构投射到树 [英] Cast from GrammaticalStructure to Tree
问题描述
我正在试用斯坦福大学的新NN依赖解析器.根据他们提供的演示,解析是这样完成的:
I am trying out the new NN Dependency Parser from Stanford. According to the demo they have provided, this is how the parsing is done:
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);
}
现在,我想做的是对象gs
,它是类GrammaticalStructure
,要从edu.stanford.nlp.trees.Tree
强制转换为Tree
对象.
Now, what I want to do is this object gs
, which is of class GrammaticalStructure
, to be casted as a Tree
object from edu.stanford.nlp.trees.Tree
.
我天真地尝试了简单的转换:
I naively tried out with simple casting:
Tree t = (Tree) gs;
但是,这是不可能的(IDE给出错误:无法从GrammaticalStructure
投射到Tree
).
but, this is not possible (the IDE gives an error: Cannot cast from GrammaticalStructure
to Tree
).
我该怎么做?
推荐答案
您应该能够使用gs.root()
获取树.
根据文档,该方法会返回表示语法结构的树(实际上是TreeGraphNode
).
You should be able to get the Tree using gs.root()
.
According to the documentation, that method returns a Tree (actually, a TreeGraphNode
) which represents the grammatical structure.
您可以使用gs.root().pennPrint()
以人性化的方式打印该树.
You could print that tree in a human-friendly way with gs.root().pennPrint()
.
这篇关于从语法结构投射到树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!