Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stanford Parser: how to extract dependencies?

My work consists in finding a query (can be noun+verb) in a sentence, then extract the object.

exemple: "coding is sometimes a tough work." My query would be: "coding is".

the typed dependencies i get are:

nsubj(work-6, coding-1)   
cop(work-6, is-2)    
advmod(work-6, sometimes-3)
det(work-6, a-4)
amod(work-6, tough-5)

My program should extract the nsubj dependency, identify "coding" as the query and save "work".

May be this seems simple, but until now, i didn't find a method able to extract a specific typed dependency, and I really need this to finish my work.

Any help is welcome,

like image 565
safé Avatar asked Mar 27 '11 12:03

safé


People also ask

What is Stanford dependency parser?

Introduction. A dependency parser analyzes the grammatical structure of a sentence, establishing relationships between "head" words and words which modify those heads. The figure below shows a dependency parse of a short sentence.

How does dependency parser work?

Dependency parsing is the process of analyzing the grammatical structure of a sentence based on the dependencies between the words in a sentence. In Dependency parsing, various tags represent the relationship between two words in a sentence. These tags are the dependency tags.


2 Answers

You can find dependencies by following code:

Tree tree = sentence.get(TreeAnnotation.class);
// Get dependency tree
TreebankLanguagePack tlp = new PennTreebankLanguagePack();
GrammaticalStructureFactory gsf = tlp.grammaticalStructureFactory();
GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
Collection<TypedDependency> td = gs.typedDependenciesCollapsed();
System.out.println(td);

Object[] list = td.toArray();
System.out.println(list.length);
TypedDependency typedDependency;
for (Object object : list) {
typedDependency = (TypedDependency) object;
System.out.println("Depdency Name"typedDependency.dep().nodeString()+ " :: "+ "Node"+typedDependency.reln());
if (typedDependency.reln().getShortName().equals("something")) {
   //your code
}
like image 173
Imran Avatar answered Sep 28 '22 01:09

Imran


I don't think there's a way to tell the parser to extract the dependencies around a given word. However, you can just run through the list of dependencies for each sentence, searching for all instances in which the query word appears in an nsubj relationship.

Also, how are you storing the parses of the sentences? If (as I gather from your question) it's in a text file, you can just use 2 successive greps, one for the query word, and one for the relationship you desire, to get a list of relevant other words.

like image 39
silverasm Avatar answered Sep 28 '22 01:09

silverasm