2013-01-17 5 views
3

각 단어에 해당하는 구문 태그를 얻으려면 어떻게해야합니까?스탠포드 CoreNLP에서 구문 태그를 얻는 방법은 무엇입니까?

예를 들어

:이 문장에서

,

내 개는 소시지를 먹고 좋아한다.

나는 위의 situtation에서

(ROOT (S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ likes) (NP (JJ eating) (NN sausage))) (. .))) 

같은

, 나는 구문 태그

(My - NP), (dog - NP), (also - ADVP), (likes - VP), ... 

처럼 각 단어에 해당 싶어 스탠포드 NLP의 파스 트리를 얻을 수있는 방법이 있나요 구문 태그에 대한이 간단한 추출을 위해?

도와주세요.

답변

2
//I guess this is how you get your parse tree. 
Tree tree = sentAnno.get(TreeAnnotation.class); 

//The children of a Tree annotation is an array of trees. 
Tree[] children = parent.children() 

//Check the label of any sub tree to see whether it is what you want (a phrase) 
for (Tree child: children){ 
    if (child.value().equals("NP")){// set your rule of defining Phrase here 
      List<Tree> leaves = child.getLeaves(); //leaves correspond to the tokens 
      for (Tree leaf : leaves){ 
      List<Word> words = leaf.yieldWords(); 
      for (Word word: words) 
       System.out.print(String.format("(%s - NP),",word.word())); 
      } 
    } 
} 

코드가 완벽하게 테스트되지는 않았지만 대략 필요한 것으로 생각됩니다. 그리고 재귀 적으로 하위 트리를 방문하는 것에 대해 쓰지는 않았지만, 그렇게 할 수 있어야한다고 생각합니다.