从依赖解析器中提取特殊节点

时间:2015-10-29 07:19:32

标签: java parsing dependencies nlp stanford-nlp

我想在Stanford依赖解析器中找到一些节点,例如:

句子:Microsoft ad says that Macs are too cool for its customers.

依赖关系:

 - compound(ad-2, Microsoft-1)
 - nsubj(says-3, ad-2)
 - root(ROOT-0, says-3)
 - mark(cool-8, that-4)
 - nsubj(cool-8, Macs-5)
 - cop(cool-8, are-6)
 - advmod(cool-8, too-7)
 - ccomp(says-3, cool-8)
 - case(customers-11, for-9)
 - nmod:poss(customers-11, its-10)
 - nmod:for(cool-8, customers-11)

我想捕获以下构造:

p1={Node with two outgoing edges with labels "nsubj" and "ccomp"},

In its dependency tree, `says` satisfies this condition, so p1={says}

s1={ n1={Node that connected to the p1 by an edge with label "nsubj"},
Node connected to n1 by an edge with label "nn" or "quantmod"} 

In its dependency tree s1={n1=ad, Microsoft}

我不知道如何提取这些节点,我尝试了这种结构来提取广告,但它也提取了Mac!我不知道提取其他节点!任何帮助将不胜感激。

  

typedDependency.reln()getShortName()等于。(" nsubj&#34)

这是我的代码:

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().toString()+ " :: "+ "Node  "+typedDependency.reln());



        if (typedDependency.reln().getShortName().equals("nsubj")) {

                ????

}
         }
        }
    }
    }

2 个答案:

答案 0 :(得分:1)

您是否查看了Semgrex上的幻灯片?

他们可以在这里找到:

http://nlp.stanford.edu/software/Semgrex.ppt

关于Semgrex的更多信息:

http://nlp.stanford.edu/software/tregex.shtml

答案 1 :(得分:0)

每个类型化的依赖项都连接一个依赖项和一个头。对于第一个构造,您需要迭代类型化的依赖项并记录那些具有标签“nsubj”和“ccomp”以及它们的头部的ID。可以按如下方式访问类型化依赖项头部的id:

typedDependency.dep().index()

然后检查哪一对nsubj和ccomp指向同一个头。在您的示例中,一个头将对应于“说”。

对于第二个构造,您还可以使用类型依赖项中的head的id来跟踪连接。