ojflewis
ojflewis

Reputation: 13

How to create a GrammaticalRelation in Stanford CoreNLP

I have recently upgraded to the latest version of Stanford CoreNLP. The code I previously used to get the subject or object in a sentence was

System.out.println("subject: "+dependencies.getChildWithReln(dependencies.getFirstRoot(), EnglishGrammaticalRelations.NOMINAL_SUBJECT));

but this now returns null.

I have tried creating a relation with

GrammaticalRelation subjreln =
                edu.stanford.nlp.trees.GrammaticalRelation.valueOf("nsubj");

without success. If I extract a relation using code like

GrammaticalRelation target = (dependencies.childRelns(dependencies.getFirstRoot())).iterator().next();

Then run the same request,

System.out.println("target: "+dependencies.getChildWithReln(dependencies.getFirstRoot(), target));

then I get the desired result, confirming that the parsing worked fine (I also know this from printing out the full dependencies).

I suspect my problem has to do with the switch to universal dependencies, but I don't know how to create the GrammaticalRelation from scratch in a way that will match what the dependency parser found.

Upvotes: 1

Views: 330

Answers (1)

Sebastian Schuster
Sebastian Schuster

Reputation: 1563

Since version 3.5.2 the default dependency representation in CoreNLP is Universal Dependencies. This new representation is implemented in a different class (UniversalEnglishGrammaticalRelations) so the GrammaticalStructure objects are now defined somewhere else.

All you have to do to use the new version is to replace EnglishGrammaticalRelations with UniversalGrammaticalRelations:

System.out.println("subject: "+dependencies.getChildWithReln(dependencies.getFirstRoot(), UniversalEnglishGrammaticalRelations.NOMINAL_SUBJECT));

Note, however, that some relations in the new representation are different and might no longer exist (nsubj still does). We are currently compiling migration guidelines from the old representation to the new Universal Dependencies relations. It is still incomplete but it already contains all relation names and their class names in CoreNLP.

Upvotes: 0

Related Questions