Ishank
Ishank

Reputation: 2926

How to obtain the "Grammatical Relation" using Stanford NLP Parser?

I am absolutely new to Java development.

Can someone please elaborate on how to obtain "Grammatical Relations" using the Stanfords's Natural Language Processing Lexical Parser- open source Java code?

Thanks!

Upvotes: 0

Views: 2510

Answers (1)

nflacco
nflacco

Reputation: 5082

See line 88 of first file in my code to run the Stanford Parser programmatically

GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCollapsed();

System.out.println("words: "+words); 
System.out.println("POStags: "+tags); 
System.out.println("stemmedWordsAndTags: "+stems); 
System.out.println("typedDependencies: "+tdl); 

The collection tdl is a list of these typed dependencies. If you look on the javadoc for TypedDependency you'll see that using the .reln() method gets you the grammatical relation.

Lines 311-318 of the third file in my code show how to use that list of typed dependencies. I happen to get the name of the relation, but you could get the relation itself, which would be of the class GrammaticalRelation.

for( Iterator<TypedDependency> iter = tdl.iterator(); iter.hasNext(); ) {
    TypedDependency var = iter.next();

    TreeGraphNode dep = var.dep();
    TreeGraphNode gov = var.gov();

    // All useful information for a node in the tree
    String reln = var.reln().getShortName();

Don't feel bad, I spent a miserable day or two trying to figure out how to use the parser. I don't know if the docs have improved, but when I used it they were pretty damn awful.

Upvotes: 6

Related Questions