user3318618
user3318618

Reputation: 73

Stanford nlp: Parse Tree

I have this sentence: My dog also likes eating sausage.

And I get the following parse tree:

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

How do I get only the grammatical category, namely: NP,ADVP,VP, etc?

I tried with this code:

  Tree t=sentence.get(TreeAnnotation.class);
  t.labels();

Upvotes: 3

Views: 3738

Answers (1)

Scott Cinnamond
Scott Cinnamond

Reputation: 106

From a sentence annotation you can get various typed collections of dependent words. This may be the 'next level up' you're looking for.

Tree tree = sentenceAnnotation.get(TreeAnnotation.class);                             
// print the tree if needed                                                           
SemanticGraph basic = sentenceAnnotation.get(BasicDependenciesAnnotation.class);      
Collection<TypedDependency> deps = basic.typedDependencies();                         
for (TypedDependency typedDep : deps) {                                               
    GrammaticalRelation reln = typedDep.reln();                                       
    String type = reln.toString();                                                    
}                                                                                     

SemanticGraph colapsed = sentenceAnnotation                                           
        .get(CollapsedDependenciesAnnotation.class);                          
Collection<TypedDependency> deps = colapsed.typedDependencies();                      
for (TypedDependency typedDep : deps) {                                               
    GrammaticalRelation reln = typedDep.reln();                                       
    String type = reln.toString();                                                    
}                                                                                     

SemanticGraph ccProcessed = sentenceAnnotation                                        
        .get(CollapsedCCProcessedDependenciesAnnotation.class);               
Collection<TypedDependency> deps = ccProcessed.typedDependencies();                   
for (TypedDependency typedDep : deps) {                                               
    GrammaticalRelation reln = typedDep.reln();                                       
    String type = reln.toString();                                                    
}      

Upvotes: 4

Related Questions