Nlp 如何获得与在线演示完全相同的依赖项解析输出?

Nlp 如何获得与在线演示完全相同的依赖项解析输出?,nlp,stanford-nlp,Nlp,Stanford Nlp,如何使用StanfordCorenlp以编程方式获得与在线演示中相同的依赖项解析 我正在使用corenlp包获取下面句子的依赖项解析 德克萨斯州第二名医护人员埃博拉检测呈阳性,当局称。 我尝试使用下面的代码以编程方式获得解析 Properties props = new Properties(); props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");

如何使用StanfordCorenlp以编程方式获得与在线演示中相同的依赖项解析

我正在使用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());
                }
            }

    }
但是在线演示给出了一个不同的答案,它将say标记为根,并具有其他关系,如解析中单词之间的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)

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

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

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

props.put("annotators", "tokenize, ssplit, parse, lemma, ner, dcoref");
但是,请注意,lemma、ner和dcoref注释器都需要POS标记,因此必须更改注释器的顺序


还有一个解析器,它应该总是产生与代码相同的输出。

我相信解析器是确定性的。确保您运行的CoreNLP版本与在线演示的版本相同,并且您使用的是相同的模型。你可能需要给斯坦福团队发电子邮件,询问他们在网站上运行的是什么版本/型号-不确定他们是否真的提到了这一点。谢谢你的解释。我验证了使用您提到的注释器,我能够获得与我提到的示例的在线演示中完全相同的依赖项解析输出。我认为这是正确的答案。
props.put("annotators", "tokenize, ssplit, parse, lemma, ner, dcoref");