如何在Stanford Parse-Tree中获取根节点?

时间:2016-01-21 16:12:35

标签: stanford-nlp

我在这里有这个解析树:

enter image description here

我想要的是从一个共同的父母那里得到所有的单词,在一个子树的子集中给出一个单词。例如,如果你采用" 瓶子"然后我想得到" Voss瓶"或者甚至" Voss瓶装水"但我不知道该怎么做。

Annotation document = new Annotation(sentenceText); 
this.pipeline.annotate(document);

List<CoreMap> sentences = document.get(SentencesAnnotation.class);

for (CoreMap sentence : sentences) {

    Tree tree = sentence.get(TreeAnnotation.class);

    List<Tree> leaves = new ArrayList<>();
    leaves = tree.getLeaves(leaves);

    for (Tree leave : leaves) {         
        String compare = leave.toString().toLowerCase();            
        if(compare.equals(word) == true) {
            // Get other nodes in the same subtree
        }
    }
}

调用leave.parent()不起作用。我也尝试tree.parent(leave),但这也不起作用(返回null)。

我也试过

for (Tree leave : tree) {
    String compare = leave.toString().toLowerCase();        
    if(compare.equals(word) == true) {
        // ..
    }
}

但我也一样。

  

public Tree parent(Tree root)
返回树节点的父节点。此例程将从给定的根遍历树(深度优先),并且无论具体类是否存储父类,都将正确地找到父树。如果此节点是根节点,或者此节点未包含在以root为根的树中,则它将仅返回null。

我怎样才能做到这一点? #EverythingIsBrokenAllTheTime

1 个答案:

答案 0 :(得分:0)

此示例行应该为给定的单词提供两个级别(请注意,POS标记有节点,因此您需要执行两次父级;第一个父级返回以POS标记为根的子树)

Tree root = (leave.parent(tree)).parent(tree);

“leave”应该是该单词的节点 “树”应该是整个句子的树

此方法从根开始遍历树,跟踪它通过的节点。当它命中你想要的节点时(在这个例子中“离开”),它返回适当的父节点。

完整代码:

import java.io.*;
import java.util.*;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.trees.TreeCoreAnnotations.*;
import edu.stanford.nlp.semgraph.*;
import edu.stanford.nlp.ling.CoreAnnotations.*;
import edu.stanford.nlp.util.*;

public class RootFinderExample {

    public static void main (String[] args) throws IOException {
        // build pipeline
        Properties props = new Properties();
        props.setProperty("annotators","tokenize, ssplit, pos, lemma, ner, parse");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        String text = "We were offered water for the table but were not told the Voss bottles of water were $8 a piece.";
        Annotation annotation = new Annotation(text);
        pipeline.annotate(annotation);
        List<CoreMap> sentences = annotation.get(SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {
            Tree tree = sentence.get(TreeAnnotation.class);
            List<Tree> leaves = new ArrayList<>();
            leaves = tree.getLeaves(leaves);
            for (Tree leave : leaves) {
                String compare = leave.toString().toLowerCase();
                if(compare.equals("bottles") == true) {
                    System.out.println(tree);
                    System.out.println("---");
                    System.out.println(leave);
                    System.out.println(leave.parent(tree));
                    System.out.println((leave.parent(tree)).parent(tree));
                }
            }

        }
    }

}