How to find word occuring Place numerically using Stanford Parser API?
In typed dependency, Stanford Parser also shows the Word occuring place e.g. "love-2". Now it shows that "love" in "2" place.
nsubj(love-2, I-1)
poss(country-4, my-3)
dobj(love-2, cou开发者_StackOverflowntry-4)
Now, how can I find the place of word programtically using Stanford parser API? Is there any fanuction in API?
If you want to get the index of a particular word in a sentence, you may chose to directly tokenize it and get place as indexOf(token)+1
TypedDependency format >>> abbreviated_form_reln (governor, dependent)
If you want to access the index of a particular word in the TypedDependency (or any other attribute), just use the API
For instance:
Say, TypedDepency td represents nsubj (love-2, I-1)
td.gov(); //gives the governer (of type TreeGraphNode)
td.dep(); //gives the dependent (")
td.reln(); //gives the relation (of type GrammaticalRelation)
You may then use the methods of TreeGraphNode to retrieve further details
Say, TreeGraphNode tgn = td.gov();
tgn.index(); //yields the required index (for the above case, 2)
Feel free to refer to the javadoc http://nlp.stanford.edu/nlp/javadoc/javanlp/
You must have given it a sentence already, so I am not sure why you don't already know the position of the word in it.
If you are instead trying to understand why you have multiple dependencies mentioning the same word, then this is because words can propagate from one dependency to another one.
You do something like the below. The wordIndex is what you want.
import edu.stanford.nlp.ling.CoreAnnotations.IndexAnnotation;
...
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
List<TypedDependency> tdl = gs.typedDependenciesCCprocessed();
TypedDependency td = tdl.get(0);
CoreLabel cl = td.dep().label();
int wordIndex = cl.get(IndexAnnotation.class);
精彩评论