0

I want to use the coreference resolution from Stanford NLP. I have downloaded the latest version 3.6.But when I run the following code, I am getting NullPointerException. I looked up in net, and few people seem to have to experienced the same problem, but no solution is mentioned. Any help would be appreciated.

public static void main(String[] args) {
  Properties props = new Properties();
  props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
  props.put("dcoref.score", true);
  StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
  String sentence="Stanford University is located in California. It is a great university, founded in 1891.";
  Annotation document = new Annotation(sentence);

  pipeline.annotate(document);
  System.out.println(document);
  Map<Integer, CorefChain> graph = document.get(CorefChainAnnotation.class);
  Set<Integer> set=graph.keySet();
  Iterator <Integer> it=set.iterator();
  while(it.hasNext()) {
    CorefChain c=graph.get(it.next());
    System.out.println("CorefChain: "+c);
  }
}
JFPicard
  • 5,029
  • 3
  • 19
  • 43
xyz
  • 57
  • 1
  • 5
  • 3
    Where do you get the NullPointerException in the code ? – JFPicard Feb 15 '16 at 14:47
  • 1
    It would help if you could find which line # in your code the exception is thrown from and post the stacktrace, ideally highlighting in your code the line # in question. – Pedantic Feb 15 '16 at 14:48
  • I am getting it at this line: Set set=graph.keySet(); I am new to java and not very comfortable in it. Thanks for your help – xyz Feb 15 '16 at 15:24
  • Then `graph` is null. Check why `document.get(CorefChainAnnotation.class)` returns `null`. – user1803551 Feb 15 '16 at 16:50
  • Can we unmark this as a duplicate? How on earth is "I'm getting a null pointer exception in a specific library in a specific use case" a duplicate of "what is a null pointer exception"? – Gabor Angeli Feb 16 '16 at 03:07
  • The answer to the question is, incidentally, that you need to include the `mention` annotator before `coref`. You should have a line `props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, mention, dcoref");` – Gabor Angeli Feb 16 '16 at 03:07
  • @GaborAngeli I added the line which you mentioned. But when I am trying to print the graph, I am getting null. What am I missing? Thank you for helping me out. – xyz Feb 16 '16 at 05:27
  • The issue is that we have changed how dcoref results are stored. – StanfordNLPHelp Feb 16 '16 at 10:15
  • You need these import statements: – StanfordNLPHelp Feb 16 '16 at 10:16
  • 1
    import edu.stanford.nlp.hcoref.CorefCoreAnnotations; – StanfordNLPHelp Feb 16 '16 at 10:16
  • 1
    import edu.stanford.nlp.hcoref.data.CorefChain; – StanfordNLPHelp Feb 16 '16 at 10:16
  • 2
    We don't use: edu.stanford.nlp.dcoref.CorefCoreAnnotations any more – StanfordNLPHelp Feb 16 '16 at 10:16
  • The line should be like this: Map graph = document.get(CorefCoreAnnotations.CorefChainAnnotation.class); – StanfordNLPHelp Feb 16 '16 at 10:17
  • with these imports: import edu.stanford.nlp.hcoref.CorefCoreAnnotations; import edu.stanford.nlp.hcoref.data.CorefChain; import edu.stanford.nlp.hcoref.data.Mention; import edu.stanford.nlp.ling.CoreAnnotations; import edu.stanford.nlp.pipeline.Annotation; import edu.stanford.nlp.pipeline.StanfordCoreNLP; import edu.stanford.nlp.util.CoreMap; import java.util.*; – StanfordNLPHelp Feb 16 '16 at 10:17
  • With that set up I got your example to work. In 3.6 we changed the details a bit...in short, dcoref and hcoref have to use the same type of key (or we'd have to rewrite a bunch of stuff), so I changed dcoref to use the hcoref keys...I suspect in your example you are doing import edu.stanford.nlp.dcoref.* or import edu.stanford.nlp.dcoref.CorefCoreAnnotations...so don't do either of those. – StanfordNLPHelp Feb 16 '16 at 10:20
  • Also there is a bunch of documentation on coref here: http://stanfordnlp.github.io/CoreNLP/coref.html – StanfordNLPHelp Feb 16 '16 at 10:21
  • For instance you might want to use the "coref" annotator instead of "dcoref"...that link I provided has a bunch of info! – StanfordNLPHelp Feb 16 '16 at 10:23

0 Answers0