Java 使用核心注释与语法结构/headWordNode

Java 使用核心注释与语法结构/headWordNode,java,nlp,stanford-nlp,Java,Nlp,Stanford Nlp,我正在使用斯坦福大学的CoreNLP来解析句子。我试图用情感和其他语言注释创造词汇化语法 我通过以下方式获得了parse+情绪+dep: Properties props = new Properties(); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, depparse, sentiment"); StanfordCoreNLP pipeline =

我正在使用斯坦福大学的CoreNLP来解析句子。我试图用情感和其他语言注释创造词汇化语法

我通过以下方式获得了parse+情绪+dep:

Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, 
                  lemma, ner, parse, depparse, sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

Annotation annotation = pipeline.process(mySentence);
由于我还想在语法中使用依赖项,所以我一直在使用
grammaticstructurefactory
及其
newgrammaticstructure()
方法来获取语法结构实例。我注意到“TreeGraphNode”有一个“headWordNode”,因此,我同步遍历这两棵树,例如

public void BuildSomething(Tree tree, TreeGraphNode gsNode) {
    List<Tree> gsNodes = gsNode.getChildrenAsList();
    List<Tree> constNodes = tree.getChildrenAsList();

    for (int i = 0; i < constNodes.size(); i++) {
        Tree childTree = constNodes.get(i);
        TreeGraphNode childGsNode = (TreeGraphNode) gsNodes.get(i);

        // build for this "sub-tree"            
        BuildSomething(childTree, childGsNode);
    }

    String headWord = gsNode.headWordNode().label().value();
    // do something with the head-word...

    // do other stuff...
}

使用类似于上述代码的代码,我注意到我得到的语法结构不一定与树的结构匹配(应该吗?),而且,我有时会得到一个“奇怪”的头选择(选择为头的限定词似乎不正确):

我对
语法结构的使用是否不正确


另外,我见过一些使用“HeadFinder”和“node.headTerminal(hf,parent)”的代码(),但我认为另一种方法会更快。。。因此,我不确定我是否可以使用上述方法或必须使用这种方法

“我得到的语法结构不一定与树的结构相匹配”-您能否更具体地回答:您期望什么和您观察到什么?语法结构和树表示对应于不同类型的语法分析,并且两种分析的结果之间并不总是一对一的映射。@JonGauthier-我的印象是它们应该一对一匹配,但现在我知道它们不会匹配。我主要看到(没有深入讨论)对应节点上的不同注释(可能还有不同的树结构)。是否有一种方法可以使用GS来查找头部,或者我应该使用
HeadFinder
?是的,直接使用
HeadFinder
实例即可。有关示例,请参见此处:
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
    Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
     GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);

     BuildSomething(tree, gs.root);
}