如何在 Stanford CoreNLP 中创建 GrammaticalRelation

How to create a GrammaticalRelation in Stanford CoreNLP

我最近升级到了最新版本的 Stanford CoreNLP。我之前用来获取句子中的主语或宾语的代码是

System.out.println("subject: "+dependencies.getChildWithReln(dependencies.getFirstRoot(), EnglishGrammaticalRelations.NOMINAL_SUBJECT));

但是现在这个returns null.

我试过与

建立关系
GrammaticalRelation subjreln =
                edu.stanford.nlp.trees.GrammaticalRelation.valueOf("nsubj");

没有成功。如果我使用

之类的代码提取关系
GrammaticalRelation target = (dependencies.childRelns(dependencies.getFirstRoot())).iterator().next();

然后运行同样的请求,

System.out.println("target: "+dependencies.getChildWithReln(dependencies.getFirstRoot(), target));

然后我得到了想要的结果,确认解析工作正常(我也从打印出完整的依赖关系中知道这一点)。

我怀疑我的问题与切换到通用依赖项有关,但我不知道如何从头开始创建 GrammaticalRelation 以匹配依赖项解析器找到的内容。

从 3.5.2 版开始,CoreNLP 中的默认依赖表示为 Universal Dependencies。这种新表示是在不同的 class (UniversalEnglishGrammaticalRelations) 中实现的,因此 GrammaticalStructure 对象现在在其他地方定义。

要使用新版本,您只需将 EnglishGrammaticalRelations 替换为 UniversalGrammaticalRelations:

System.out.println("subject: "+dependencies.getChildWithReln(dependencies.getFirstRoot(), UniversalEnglishGrammaticalRelations.NOMINAL_SUBJECT));

但是请注意,新表示中的某些关系有所不同并且可能不再存在(nsubj 仍然存在)。我们目前正在将 migration guidelines 从旧表示编译为新的通用依赖关系。它仍然不完整,但它已经包含所有关系名称及其在 CoreNLP 中的 class 名称。