how to get relationship between words with nlp stanford parser
I am trying to get the connections between string and other words like:
The screen is very good
so I want to get
screen good
I just don't know how to get that the subject is screen and the description is very good.
My开发者_开发百科 code is
public synchronized String test(String s, LexicalizedParser lp){
if (s.isEmpty()) return "";
if (s.length()>80) return "";
System.out.println(s);
Tree parse = (Tree) lp.apply(s);
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
System.out.println(parse.dependencies(tlp.headFinder()));
}
Can someone give me an example of how to do it right?
The string s
is the sentence to find the connection between words.
To get the typed Stanford Dependencies (like nsubj, dobj) you need to use the GrammaticalStructure classes. A plain Tree only has untyped dependencies. Use something like this:
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(parse);
Collection tdl = gs.typedDependenciesCollapsed();
System.out.println(tdl);
精彩评论