解析树中节点的深度
depth of node in a parse tree
我在 Java 7 和 NetBeans 7.3.1
中使用 stanford-nlp
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String text = "the dog who bit the man";// Add your text here!
Annotation document = new Annotation(text);
pipeline.annotate(document);
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
for(CoreMap sentence: sentences) {
for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
String word = token.get(TextAnnotation.class);
String pos = token.get(PartOfSpeechAnnotation.class);
String ne = token.get(NamedEntityTagAnnotation.class);
}
Tree tree = sentence.get(TreeAnnotation.class);
System.out.println(tree);
System.out.println(tree.depth());
有了这个我可以得到树的深度但是我怎样才能得到术语 'dog' 的深度或这个解析树中任何其他术语的深度?
经过一番研究,我才知道这是一个愚蠢的问题(非常愚蠢)抱歉:D
无论如何,这是我学到的:
由于句子的 terms/strings 在解析树中表示为叶子,因此它们的深度将为 0。
那么现在如何访问术语,即如何将树迭代到 term/string/leaf ::
for (Tree subtree : tree) {
if(subtree.label().value().equals("term----xxxx"))
//You can do your own stuff here
}
我在 Java 7 和 NetBeans 7.3.1
中使用 stanford-nlpProperties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String text = "the dog who bit the man";// Add your text here!
Annotation document = new Annotation(text);
pipeline.annotate(document);
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
for(CoreMap sentence: sentences) {
for (CoreLabel token: sentence.get(TokensAnnotation.class)) {
String word = token.get(TextAnnotation.class);
String pos = token.get(PartOfSpeechAnnotation.class);
String ne = token.get(NamedEntityTagAnnotation.class);
}
Tree tree = sentence.get(TreeAnnotation.class);
System.out.println(tree);
System.out.println(tree.depth());
有了这个我可以得到树的深度但是我怎样才能得到术语 'dog' 的深度或这个解析树中任何其他术语的深度?
经过一番研究,我才知道这是一个愚蠢的问题(非常愚蠢)抱歉:D
无论如何,这是我学到的:
由于句子的 terms/strings 在解析树中表示为叶子,因此它们的深度将为 0。
那么现在如何访问术语,即如何将树迭代到 term/string/leaf ::
for (Tree subtree : tree) {
if(subtree.label().value().equals("term----xxxx"))
//You can do your own stuff here
}