如何将依赖解析输出完全作为在线演示?

时间:2015-01-09 17:19:28

标签: nlp stanford-nlp

如何使用stanford corenlp以编程方式获取相同的依赖关系解析,如在线演示中所示?

我正在使用corenlp包来获取以下句子的依赖关系解析。

当局称,德克萨斯州的第二名医护人员对埃博拉病毒检测结果为阳性。

我尝试使用下面的代码以编程方式获取解析

            Properties props = new Properties();
            props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

            String text = "Second healthcare worker in Texas tests positive for Ebola , authorities say ."; // Add your text here!
            Annotation document = new Annotation(text);
            pipeline.annotate(document);
            String[] myStringArray = {"SentencesAnnotation"};
            List<CoreMap> sentences = document.get(SentencesAnnotation.class);
            for(CoreMap sentence: sentences) {
                SemanticGraph dependencies = sentence.get(BasicDependenciesAnnotation.class);
                IndexedWord root = dependencies.getFirstRoot();
                System.out.printf("root(ROOT-0, %s-%d)%n", root.word(), root.index());
                for (SemanticGraphEdge e : dependencies.edgeIterable()) {
                    System.out.printf ("%s(%s-%d, %s-%d)%n", e.getRelation().toString(), e.getGovernor().word(), e.getGovernor().index(), e.getDependent().word(), e.getDependent().index());
                }
            }

    }

我使用stanford corenlp 3.5.0软件包获得以下输出。

root(ROOT-0, worker-3)
amod(worker-3, Second-1)
nn(worker-3, healthcare-2)
prep(worker-3, in-4)
amod(worker-3, positive-7)
dep(worker-3, say-12)
pobj(in-4, tests-6)
nn(tests-6, Texas-5)
prep(positive-7, for-8)
pobj(for-8, ebola-9)
nsubj(say-12, authorities-11)

但是,在线演示提供了一个不同的答案,标记为根,并且具有其他关系,例如解析中的单词之间的ccomp。

amod(worker-3, Second-1)
nn(worker-3, healthcare-2)
nsubj(tests-6, worker-3)
prep(worker-3, in-4)
pobj(in-4, Texas-5)
ccomp(say-12, tests-6)
acomp(tests-6, positive-7)
prep(positive-7, for-8)
pobj(for-8, Ebola-9)
nsubj(say-12, authorities-11)
root(ROOT-0, say-12)

如何解析输出以匹配在线演示?

1 个答案:

答案 0 :(得分:9)

输出不同的原因是,如果使用parser demo,则使用独立解析器分发,并且您的代码使用整个CoreNLP分发。虽然它们都使用相同的解析器和相同的模型,但CoreNLP的默认配置在运行解析器之前运行词性(POS)标记器,并且解析器包含POS信息,在某些情况下可能导致不同的结果。

为了获得相同的结果,您可以通过更改注释器列表来禁用POS标记器:

props.put("annotators", "tokenize, ssplit, parse, lemma, ner, dcoref");

但请注意,引理,ner和dcoref注释器都需要POS标签,因此您必须更改注释器的顺序。

还有一个CoreNLP demo,它应该始终产生与您的代码相同的输出。