2016-01-21 31 views
1

我有這個解析樹在這裏:如何獲得斯坦福分析樹中的根節點?

enter image description here

我想是從集合的子樹的孩子給出一個字一個共同的母公司獲得的所有單詞。例如,如果你拿「」這個詞,那麼我想獲得「沃斯瓶」或甚至「沃斯瓶裝水」但我不知道該怎麼做。

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)
返回樹節點的父節點。此例程將遍歷給定根的樹(第一個深度),並且將正確地查找父級,而不管具體類是否存儲父級。如果此節點是根節點,或者如果此節點未包含在以根爲根的樹中,它將僅返回null。

我該如何設法做到這一點? #EverythingIsBrokenAllTheTime

回答

0

此示例行應該爲給定單詞提供兩個級別(請注意,存在POS標記的節點,因此您需要執行兩次父級;第一個父級以POS標記作爲根返回子樹)

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

「離開」應該是字 「樹」應該是整個句子

該方法經過從根樹,跟蹤它通過節點的樹的節點通過。當它碰到你想要的節點時(在這個例子中「離開」)它返回合適的父節點。

全碼:

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

     } 
    } 

} 
+0

就像我在這個問題指出,調用'leave.parent(樹)',它拋出一個異常每次 – displayname

+0

在你使用的問題,不工作「tree.parent(離開)」。我用leave.parent(樹)運行了一個例子,並沒有變爲null,我將嘗試稍後發佈。 – StanfordNLPHelp

+0

對不起,這只是返回'null',但不是exceptoin。 – displayname

相關問題