-1

I am using Stanford CoreNLP. I need to detect and identify the "Coreference set"s and "representative mention"s for each CorefChain in my input text:

For example: Input: Obama was elected to the Illinois state senate in 1996 and served there for eight years. In 2004, he was elected by a record majority to the U.S. Senate from Illinois and, in February 2007, announced his candidacy for President.

Output: With "Pretty Print" I can get the output below:

**Coreference set:
(2,4,[4,5]) -> (1,1,[1,2]), that is: "he" -> "Obama"

(2,24,[24,25]) -> (1,1,[1,2]), that is: "his" -> "Obama"

(3,22,[22,23]) -> (1,1,[1,2]), that is: "Obama" -> "Obama"**

However, I need to programmatically identify and detect the output above, which is called the "Coreference set". (I mean I need to identify all the pairs like: "he" -> "Obama")

Note: My base code is the one below (it is from http://stanfordnlp.github.io/CoreNLP/coref.html):

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.Properties;
public class CorefExample {

public static void main(String[] args) throws Exception {

Annotation document = new Annotation("Obama was elected to the Illinois state senate in 1996 and served there for eight years. In 2004, he was elected by a record majority to the U.S. Senate from Illinois and, in February 2007, announced his candidacy for President.");
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);
     }
   }
  }
 }

 ///// Any Idea? THANK YOU in ADVANCE
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ismail
  • 1
  • 1

1 Answers1

2

A CorefChain contains that information.

For instance you can get a:

List<CorefChain.CorefMention> 

by using this method:

cc.getMentionsInTextualOrder();

This will give you all the CorefChain.CorefMention's in the document for that particular cluster.

You can get the representative mention with this method:

cc.getRepresentativeMention();

A CorefChain.CorefMention represents a particular mention in the coref cluster. You can get information such as the full string and position from a CorefChain.CorefMention (sentence number, mention number in sentence):

for (CorefChain.CorefMention cm : cc.getMentionsInTextualOrder()) {
    String textOfMention = cm.mentionSpan;
    IntTuple positionOfMention = cm.position;
}

Here is the link to the javadoc for CorefChain:

http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/dcoref/CorefChain.html

Here is the link to the javadoc for CorefChain.CorefMention:

http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/dcoref/CorefChain.CorefMention.html

StanfordNLPHelp
  • 8,699
  • 1
  • 11
  • 9