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,
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
}
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.
精彩评论