如何在斯坦福解析树中获取根节点? [英] How to get the root node in Stanford Parse-Tree?

查看:144
本文介绍了如何在斯坦福解析树中获取根节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有这个解析树:

I have this parse tree here:

我想要的是从给定的子树子集的单词中的一个共同的父单词中获取所有单词.例如,如果您使用""一词,那么我想获得" Voss瓶"甚至是" Voss瓶水" 但是我不知道该怎么做.

What I want is to get all words from a common parent given a word in the set of children of a subtree. For example if you take the word "bottles" then I want to get "the Voss bottles" or maybe even "the Voss bottles of water" but I don't know how to do that.

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).

Calling leave.parent() does not work. I also tried tree.parent(leave) but that doesn't work either (returns null).

我也尝试过

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

但我也一样.

public Tree parent(Tree root)
返回树节点的父节点.此例程将从给定的根开始遍历一棵树(深度优先),并且将正确地找到父级,而不管具体类是否存储父级.仅当该节点是根节点或该节点不包含在以根为根的树中时,它才会返回null.

public Tree parent(Tree root)
Return the parent of the tree node. This routine will traverse a tree (depth first) from the given root, and will correctly find the parent, regardless of whether the concrete class stores parents. It will only return null if this node is the root node, or if this node is not contained within the tree rooted at root.

我如何做到这一点? #EverythingIsBrokenAllTheTime

How can I manage to do that? #EverythingIsBrokenAllTheTime

推荐答案

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

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"应该是单词的节点 树"应该是整个句子的树

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

此方法从根开始遍历树,跟踪其经过的节点.当它碰到您想要的节点时(在本示例中为"leave"),它将返回适当的父级.

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.

完整代码:

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

        }
    }

}

这篇关于如何在斯坦福解析树中获取根节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆