Search code examples
nlpstanford-nlp

How can I get the edges containing the "root" modifier dependency in the Stanford NLP parser?


I have created the dependency graph for my scenario that takes a text input.

SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);

I am successful in getting the "num" modifier dependency using the following code:

List<SemanticGraphEdge> edgesContainingNumModifierDependency = dependencies.findAllRelns(GrammaticalRelation.valueOf("num"));

However, I want to find the edges pertaining to the "root" and hence, the following

List<SemanticGraphEdge> edgesContainingRootModifierDependency = dependencies.findAllRelns(GrammaticalRelation.valueOf("root"));

does not seem to work.

Can anyone explain why? And how I can get the edge for root?


Solution

  • We don't actually store a SemanticGraphEdge between the root word and a dummy ROOT node. (You can see that the dependency is manually tacked on in public-facing methods like toList).

    From the SemanticGraph documentation:

    The root is not at present represented as a vertex in the graph. At present you need to get a root/roots from the separate roots variable and to know about it. This should maybe be changed, because otherwise, doing things like simply getting the set of nodes or edges from the graph doesn't give you root nodes or edges.

    You might be able to get what you want, though, with SemanticGraph#getFirstRoot.