如何在 stanford core nlp 工具包中获取 Coreference Resolution 注释?
how to get Coreference Resolution annotation in stanford core nlp toolkit?
我正在尝试使用 Stanford Corenlp 工具包来注释文本。我尝试使用此处提供的代码:http://stanfordnlp.github.io/CoreNLP/
它运作良好。问题是当我想使用嵌入在 coreNLP 工具包 中的 共同参考解析工具时。这是行不通的。我使用了 stanford nlp group 发布的代码。代码如下:
public class CorefExample {
public static void main(String[] args) throws Exception {
Annotation document = new Annotation("Barack Obama was born in Hawaii. He is the president. Obama was elected in 2008.");
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,mention,coref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
pipeline.annotate(document);
System.out.println("---");
System.out.println("coref chains");
for (CorefChain cc : document.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) {
System.out.println("\t"+cc);
}
for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class))
{
System.out.println("---");
System.out.println("mentions");
for (Mention m : sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)) {
System.out.println("\t"+m);
}
}
}
}
但是当我想 运行 这些代码时,我得到了空值,这一行:“sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)”
总是 return null,而我确信工具包已经注释了 corefrence 提及。
我真的混了。解决办法是什么?我如何在 java 代码中接收 coref annottaion?
如果我 运行 coref 页面上的示例代码具有最新的 stanford-corenlp-3.6。0.jar 它 运行s 完成,所以我没有看到您正在谈论的空问题。
确保使用网站上提供的最新 jar,版本 3.6.0
更新:
如果您剪切并粘贴此页面上的代码:
http://stanfordnlp.github.io/CoreNLP/coref.html
并放入名为 CorefExample.java 的文件中,然后执行:
javac -cp "stanford-corenlp-full-2015-12-09/*" CorefExample.java
java -cp "stanford-corenlp-full-2015-12-09/*:." CorefExample
您应该会看到打印出来的提及。
我们已经更新了发行版,所以还要确保您最近下载了它。
如果您仍然遇到问题,我们将不得不找出与我刚才描述的和您的设置有什么不同。我只是剪切并粘贴代码并 运行 如上所述,我看到打印出来的提及(我什至在示例文本中添加了一个没有提及的句子)并且我得到了一个包含提及的列表(或空列表).因此,如果您在最新的 jar 中使用该确切代码,则不应得到 null。
了解您如何 运行 代码会很有帮助,这样我们就可以看到有什么区别。
我在 3.7.0 版中遇到了同样的问题,同时使用“dcoref”(确定性方法)参数而不是“coref”。我不确定您在执行共指解析时导入了哪些包并遇到错误(上面的代码片段中未提及),但我想是 this 示例代码中提到的包。
在我的例子中,我手动包含了这些包,并没有从示例中复制它们。因此,Intellij建议我要么使用
import edu.stanford.nlp.coref.CorefCoreAnnotations;
import edu.stanford.nlp.coref.data.CorefChain;
如示例所示,
或:
import edu.stanford.nlp.dcoref.CorefChain;
import edu.stanford.nlp.dcoref.CorefCoreAnnotations;
我选择了第二个选项,这为我创建了空错误。实际上,不仅 sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)
为空,document.get(CorefCoreAnnotations.CorefChainAnnotation.class)
也为空。
我正在尝试使用 Stanford Corenlp 工具包来注释文本。我尝试使用此处提供的代码:http://stanfordnlp.github.io/CoreNLP/ 它运作良好。问题是当我想使用嵌入在 coreNLP 工具包 中的 共同参考解析工具时。这是行不通的。我使用了 stanford nlp group 发布的代码。代码如下:
public class CorefExample {
public static void main(String[] args) throws Exception {
Annotation document = new Annotation("Barack Obama was born in Hawaii. He is the president. Obama was elected in 2008.");
Properties props = new Properties();
props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,mention,coref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
pipeline.annotate(document);
System.out.println("---");
System.out.println("coref chains");
for (CorefChain cc : document.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) {
System.out.println("\t"+cc);
}
for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class))
{
System.out.println("---");
System.out.println("mentions");
for (Mention m : sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)) {
System.out.println("\t"+m);
}
}
}
}
但是当我想 运行 这些代码时,我得到了空值,这一行:“sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)” 总是 return null,而我确信工具包已经注释了 corefrence 提及。 我真的混了。解决办法是什么?我如何在 java 代码中接收 coref annottaion?
如果我 运行 coref 页面上的示例代码具有最新的 stanford-corenlp-3.6。0.jar 它 运行s 完成,所以我没有看到您正在谈论的空问题。
确保使用网站上提供的最新 jar,版本 3.6.0
更新:
如果您剪切并粘贴此页面上的代码:
http://stanfordnlp.github.io/CoreNLP/coref.html
并放入名为 CorefExample.java 的文件中,然后执行:
javac -cp "stanford-corenlp-full-2015-12-09/*" CorefExample.java
java -cp "stanford-corenlp-full-2015-12-09/*:." CorefExample
您应该会看到打印出来的提及。
我们已经更新了发行版,所以还要确保您最近下载了它。
如果您仍然遇到问题,我们将不得不找出与我刚才描述的和您的设置有什么不同。我只是剪切并粘贴代码并 运行 如上所述,我看到打印出来的提及(我什至在示例文本中添加了一个没有提及的句子)并且我得到了一个包含提及的列表(或空列表).因此,如果您在最新的 jar 中使用该确切代码,则不应得到 null。
了解您如何 运行 代码会很有帮助,这样我们就可以看到有什么区别。
我在 3.7.0 版中遇到了同样的问题,同时使用“dcoref”(确定性方法)参数而不是“coref”。我不确定您在执行共指解析时导入了哪些包并遇到错误(上面的代码片段中未提及),但我想是 this 示例代码中提到的包。
在我的例子中,我手动包含了这些包,并没有从示例中复制它们。因此,Intellij建议我要么使用
import edu.stanford.nlp.coref.CorefCoreAnnotations;
import edu.stanford.nlp.coref.data.CorefChain;
如示例所示,
或:
import edu.stanford.nlp.dcoref.CorefChain;
import edu.stanford.nlp.dcoref.CorefCoreAnnotations;
我选择了第二个选项,这为我创建了空错误。实际上,不仅 sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)
为空,document.get(CorefCoreAnnotations.CorefChainAnnotation.class)
也为空。