Freitag, 27. September 2013

Creating a specific preposition with Stanford CoreNLP

If you have worked with the Stanford CoreNLP tools, you may have come across the dependency graph. The graph is probably the most powerful tool for analyzing sentence structure in CoreNLP. However, it is barely documented.

How do you create a grammatical relation in Java?

Let's say you'd like to know whether a noun comes with a determiner, like "the" in "the cow". So in a first step, you'll want to create the proper grammatical relation:
GrammaticalRelation detreln =
edu.stanford.nlp.trees.GrammaticalRelation.getRelation(
EnglishGrammaticalRelations.DeterminerGRAnnotation.class);
The variable detreln can now be used to look for determiners in a dependency graph. Let's assume the word "cow" is stored in the variable "cow":
IndexedWord det =
    graph.getChildWithReln(cow, detreln);
if (det != null) {
  // success! this graph contains the relation "det"
  return true;
}
There's your determiner. The variable "det" now contains the word "the".

But what about a specific grammatical relation?

You can't create all the relations in this manner. Most prepositions, for example, will occur only in their collapsed form: prep_in, prep_of, prep_on, prep_behind or prep_in_front_of. In this case, you can use the factory method of the EnglishGrammaticalRelations class:
GrammaticalRelation prep_in = EnglishGrammaticalRelations.getPrep("in");   
The above line will create a specific relation for the preposition "in". This should help you identify relations more accurately when querying the dependency tree.