java-斯坦福大学nlp:解析树
作者:互联网
我有这样一句话:我的狗也喜欢吃香肠.
我得到以下解析树:
(ROOT
(S
(NP (PRP$My) (NN dog))
(ADVP (RB also))
(VP (VBZ likes)
(S
(VP (VBG eating)
(NP (NN sausage)))))
(. .)))
我如何只获得语法类别,即:NP,ADVP,VP等?
我尝试使用以下代码:
Tree t=sentence.get(TreeAnnotation.class);
t.labels();
解决方法:
从句子注释中,您可以获得各种类型的从属单词集合.这可能是您要寻找的“下一个升级”.
Tree tree = sentenceAnnotation.get(TreeAnnotation.class);
// print the tree if needed
SemanticGraph basic = sentenceAnnotation.get(BasicDependenciesAnnotation.class);
Collection<TypedDependency> deps = basic.typedDependencies();
for (TypedDependency typedDep : deps) {
GrammaticalRelation reln = typedDep.reln();
String type = reln.toString();
}
SemanticGraph colapsed = sentenceAnnotation
.get(CollapsedDependenciesAnnotation.class);
Collection<TypedDependency> deps = colapsed.typedDependencies();
for (TypedDependency typedDep : deps) {
GrammaticalRelation reln = typedDep.reln();
String type = reln.toString();
}
SemanticGraph ccProcessed = sentenceAnnotation
.get(CollapsedCCProcessedDependenciesAnnotation.class);
Collection<TypedDependency> deps = ccProcessed.typedDependencies();
for (TypedDependency typedDep : deps) {
GrammaticalRelation reln = typedDep.reln();
String type = reln.toString();
}
标签:stanford-nlp,nlp,java 来源: https://codeday.me/bug/20191029/1962520.html