Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Stanford nlp 如何获取Stanford解析树中的根节点?_Stanford Nlp - Fatal编程技术网

Stanford nlp 如何获取Stanford解析树中的根节点?

Stanford nlp 如何获取Stanford解析树中的根节点?,stanford-nlp,Stanford Nlp,我在这里有一个解析树: 我想要的是从子树的子树集合中给定一个单词的公共父级获取所有单词。例如,如果你用单词“瓶子”,那么我想得到“沃斯瓶””或者甚至“沃斯瓶水””,但我不知道怎么做 Annotation document = new Annotation(sentenceText); this.pipeline.annotate(document); List<CoreMap> sentences = document.get(SentencesAnnotation.class

我在这里有一个解析树:

我想要的是从子树的子树集合中给定一个单词的公共父级获取所有单词。例如,如果你用单词“瓶子”,那么我想得到“沃斯瓶””或者甚至“沃斯瓶水””,但我不知道怎么做

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
        }
    }
}
但我也得到了同样的结果

公共树父节点(树根)

返回树节点的父节点。此例程将从给定根遍历树(深度优先),并将正确找到父级,而不管具体类是否存储父级。如果此节点是根节点,或者此节点不包含在以根为根的树中,则仅返回null


我怎样才能做到这一点

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

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

此方法从根开始遍历树,跟踪它所经过的节点。当它到达您想要的节点时(在本例中为“离开”),它将返回相应的父节点

完整代码:

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

        }
    }

}
import java.io.*;
导入java.util.*;
导入edu.stanford.nlp.io.*;
导入edu.stanford.nlp.ling.*;
导入edu.stanford.nlp.pipeline.*;
导入edu.stanford.nlp.trees.*;
导入edu.stanford.nlp.trees.treeCorenotations.*;
导入edu.stanford.nlp.semgraph.*;
导入edu.stanford.nlp.ling.CoreAnnotations.*;
导入edu.stanford.nlp.util.*;
公共类RootFinderExample{
公共静态void main(字符串[]args)引发IOException{
//建造管道
Properties props=新属性();
props.setProperty(“注释器”、“标记化、ssplit、pos、引理、ner、解析”);
StanfordCoreNLP管道=新的StanfordCoreNLP(道具);
String text=“我们得到了桌子上的水,但没有被告知沃斯的瓶装水是8美元一瓶。”;
注释=新注释(文本);
管道注释(注释);
列出句子=annotation.get(SentencesAnnotation.class);
for(CoreMap句子:句子){
Tree-Tree=句子.get(TreeAnnotation.class);
List leaves=new ArrayList();
树叶=树。获取树叶(树叶);
用于(树木假期:树叶){
字符串比较=left.toString().toLowerCase();
如果(比较.equals(“瓶子”)==true){
System.out.println(树);
System.out.println(“--”);
系统输出打印项次(左);
System.out.println(leave.parent(tree));
System.out.println((leave.parent(tree)).parent(tree));
}
}
}
}
}

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

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

此方法从根开始遍历树,跟踪它所经过的节点。当它到达您想要的节点时(在本例中为“离开”),它将返回相应的父节点

完整代码:

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

        }
    }

}
import java.io.*;
导入java.util.*;
导入edu.stanford.nlp.io.*;
导入edu.stanford.nlp.ling.*;
导入edu.stanford.nlp.pipeline.*;
导入edu.stanford.nlp.trees.*;
导入edu.stanford.nlp.trees.treeCorenotations.*;
导入edu.stanford.nlp.semgraph.*;
导入edu.stanford.nlp.ling.CoreAnnotations.*;
导入edu.stanford.nlp.util.*;
公共类RootFinderExample{
公共静态void main(字符串[]args)引发IOException{
//建造管道
Properties props=新属性();
props.setProperty(“注释器”、“标记化、ssplit、pos、引理、ner、解析”);
StanfordCoreNLP管道=新的StanfordCoreNLP(道具);
String text=“我们得到了桌子上的水,但没有被告知沃斯的瓶装水是8美元一瓶。”;
注释=新注释(文本);
管道注释(注释);
列出句子=annotation.get(SentencesAnnotation.class);
for(CoreMap句子:句子){
Tree-Tree=句子.get(TreeAnnotation.class);
List leaves=new ArrayList();
树叶=树。获取树叶(树叶);
用于(树木假期:树叶){
字符串比较=left.toString().toLowerCase();
如果(比较.equals(“瓶子”)==true){
System.out.println(树);
System.out.println(“--”);
系统输出打印项次(左);
System.out.println(leave.parent(tree));
System.out.println((leave.parent(tree)).parent(tree));
}
}
}
}
}

如我在问题中所述,调用
leave.parent(tree)
不起作用,因为它每次在您使用“tree.parent(leave)”的问题中都会引发异常。我用leave.parent(tree)运行了一个示例,但没有得到null,我将稍后尝试发布。啊,对不起,这只是返回
null
,但没有例外。正如我在问题中所述,调用
leave.parent(tree)
不起作用,因为它每次在您使用的问题“tree.parent(leave)”中都会抛出异常“。我用leave.parent(tree)运行了一个示例,但没有得到空值。我将稍后尝试发布。啊,对不起,这只是返回
null
,但没有例外。”。
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));
                }
            }

        }
    }

}