我的工作包括在句子中查找查询(可以是名词+动词
),然后提取对象。
示例:“编码有时是一项艰巨的工作。”
我的查询是:“编码是”
。
我得到的类型依赖项是:
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)
我的程序应该提取 nsubj 依赖项,将“coding”识别为查询并保存“work”。
可能这看起来很简单,但直到现在,我还没有找到能够提取特定类型依赖项的方法,我真的需要它来完成我的工作。
欢迎任何帮助,
最佳答案
您可以通过以下代码查找依赖项:
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
}
关于parsing - 斯坦福解析器 : how to extract dependencies?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5449098/