我的工作包括在句子中查找查询(可以是noun+verb),然后提取对象。

例如:"coding is sometimes a tough work."我的查询是:"coding is"

我得到的类型依赖是:

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
}

08-15 23:10