我正在尝试使用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);
}
}
}
}
但是当我想运行这些代码时,我得到null,这一行:“ sentence.get(CorefCoreAnnotations.CorefMentionsAnnotation.class)” 总是返回null ,而我确信该工具包已经注明了corefrence提及。 我真的搞砸了。解决办法是什么?我如何在java代码中收到coref annottaion?
答案 0 :(得分:1)
如果我使用最新的stanford-corenlp-3.6.0.jar在coref页面上运行示例代码,它将运行完成,因此我没有看到您正在讨论的null问题。
请务必使用网站3.6.0版本上提供的最新jar。
更新
如果您在此页面上剪切并粘贴代码:
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。
了解您如何运行代码会很有帮助,这样我们就能看出它们之间的区别。
答案 1 :(得分:0)
我在版本3.7.0中遇到了同样的问题,同时使用" dcoref "" 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)
。