Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get relationship between words with nlp stanford parser

Tags:

stanford-nlp

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.

like image 405
vitaly Avatar asked Jan 23 '11 13:01

vitaly


1 Answers

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);
like image 115
Christopher Manning Avatar answered Oct 03 '22 12:10

Christopher Manning