Java 从语法结构到树的转换
我正在试用斯坦福大学新的NN依赖解析器。根据他们提供的演示,解析过程如下:Java 从语法结构到树的转换,java,nlp,stanford-nlp,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; DocumentPrep
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);
}
但是,这是不可能的(IDE给出了一个错误:不能从语法结构
转换到树
)
如何执行此操作?您应该能够使用
gs.root()
获取树。
根据,该方法返回表示语法结构的树(实际上是TreeGraphNode
)
您可以使用
gs.root().pennPrint()
以人性化的方式打印该树。您应该能够使用gs.root()
获得该树。
根据,该方法返回表示语法结构的树(实际上是TreeGraphNode
)
您可以使用
gs.root().pennPrint()
以人性化的方式打印该树。首先,tnx非常感谢您的回答!但是,我已经尝试过了,但是它返回一个空树(它只有ROOT-0)。当我尝试使用gs.root().pennPrint()
时,它只打印root
,尽管语法结构对象gs
不是空的。还有其他建议吗?首先,谢谢你的回答!但是,我已经尝试过了,但是它返回一个空树(它只有ROOT-0)。当我尝试使用gs.root().pennPrint()
时,它只打印root
,尽管语法结构对象gs
不是空的。还有其他建议吗?
Tree t = (Tree) gs;