2016-03-23 9 views
0

これは私が絵としてスタンフォードパーサの木を持っているスタンフォードパーサスタンフォードパーサー:Cの親のパスを取得する方法--->ルート?

This is a picture of Stanford Parser

の写真です。私はC'parentのパスをルートにしたい。例:"but"のパス - > root = CC Sルートまたは "it"のパスparent - > root:PRP NP VP VP S Sルート。しかし、私はそれをどうやって行うのか分かりません。私はステートメントを解析するためにStanford Parserを使用します。

 statement = "I have a dog , but I don't like it" 
     Annotation document = new Annotation(statement); 
     pipeline.annotate(document); 
     List<CoreMap> sentences = document 
      .get(CoreAnnotations.SentencesAnnotation.class); 

     for (CoreMap sentence : sentences) { 
     Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class); 
     List<Tree> leaves = new ArrayList<>(); 
     leaves = tree.getLeaves(leaves); 
     for (Tree leave : leaves) { 
      String compare = leave.toString(); 
      if(compare.equals(data.connective) == true) { 


      // How to get path but --> root 

      } 
     } 

答えて

0

Treeなし場合、ツリーノードの親を返し、又はnullparent()方法を有しています。だから、あなたのforループの中で次のようなことができるはずです。

for (Tree leave : leaves) { 
    String compare = leave.toString(); 
    if(compare.equals(data.connective) == true) { 
     Tree curr = leave; 
     while (curr != null) { 
      System.out.println(curr.toString()); 
      curr = curr.parent(); // will be null if no parent 
     } 
     break; 
    } 
} 
+0

私はTree.parent()またはTree.parent(curr)を使用しようとしました。しかし、出力はエラーまたは間違った答えを表示します.... – drag

+0

それはあなたの元の質問に含める非常に有益な情報でした。あなたは何を試してみましたか? – whrrgarbl

+0

ありがとうございます!それは本当に便利です。私は解決し解決方法を見つける。ありがとうございました:D – drag