How to get the root node in Stanford Parse-Tree?

自古美人都是妖i 提交于 2019-12-02 04:48:01

This example line should give you two levels up for a given word (note that there are nodes for POS tags, so you need to do parent twice ; the first parent returns the subtree with the POS tag as the root)

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

"leave" should be the node for the word "tree" should be the tree for the entire sentence

This method goes through the tree from the root, keeping track of the nodes it passes through. When it hits the node you want (in this example "leave") it returns the appropriate parent.

full code:

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));
                }
            }

        }
    }

}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!