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

标签 java parsing dependencies nlp stanford-nlp

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

句子:微软的广告说 Mac 对它的客户来说太酷了。

依赖关系:

 - 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().equals("nsubj")

这是我的代码:

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")) {

                ????

}
         }
        }
    }
    }

最佳答案

你看过 Semgrex 上的幻灯片了吗?

它们在这里可用:

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

关于 Semgrex 的更多信息:

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

关于java - 从依赖解析器中提取特殊节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33408092/

相关文章:

parsing - 如何巧妙地从 HTML 页面中提取信息?

使用枚举重构 Java 方法

java - 如何使用反射来识别带注释的方法?

Python - 创建 JSON 数组?并传递给 ajax

java - 正确解析字符串且不丢失定界符

java - 为什么Spring Boot项目中的某些依赖不需要指定版本?

.NET 框架依赖项

ruby-on-rails - rails : Best practice for model dependency class locations?

java - GridBagLayout:如何防止不可见组件折叠

java - 如何在 RxJava2 中获取作为订阅者的主题的 Disposable