use of edu.stanford.nlp.coref.data.CorefChain.CorefMention in project CoreNLP by stanfordnlp.
the class HybridCorefAnnotator method annotateOldFormat.
private static void annotateOldFormat(Map<Integer, CorefChain> result, Document corefDoc) {
List<Pair<IntTuple, IntTuple>> links = getLinks(result);
Annotation annotation = corefDoc.annotation;
if (VERBOSE) {
System.err.printf("Found %d coreference links:%n", links.size());
for (Pair<IntTuple, IntTuple> link : links) {
System.err.printf("LINK (%d, %d) -> (%d, %d)%n", link.first.get(0), link.first.get(1), link.second.get(0), link.second.get(1));
}
}
//
// save the coref output as CorefGraphAnnotation
//
// this graph is stored in CorefGraphAnnotation -- the raw links found by the coref system
List<Pair<IntTuple, IntTuple>> graph = new ArrayList<>();
for (Pair<IntTuple, IntTuple> link : links) {
//
// Note: all offsets in the graph start at 1 (not at 0!)
// we do this for consistency reasons, as indices for syntactic dependencies start at 1
//
int srcSent = link.first.get(0);
int srcTok = corefDoc.getOrderedMentions().get(srcSent - 1).get(link.first.get(1) - 1).headIndex + 1;
int dstSent = link.second.get(0);
int dstTok = corefDoc.getOrderedMentions().get(dstSent - 1).get(link.second.get(1) - 1).headIndex + 1;
IntTuple dst = new IntTuple(2);
dst.set(0, dstSent);
dst.set(1, dstTok);
IntTuple src = new IntTuple(2);
src.set(0, srcSent);
src.set(1, srcTok);
graph.add(new Pair<>(src, dst));
}
annotation.set(CorefCoreAnnotations.CorefGraphAnnotation.class, graph);
for (CorefChain corefChain : result.values()) {
if (corefChain.getMentionsInTextualOrder().size() < 2)
continue;
Set<CoreLabel> coreferentTokens = Generics.newHashSet();
for (CorefMention mention : corefChain.getMentionsInTextualOrder()) {
CoreMap sentence = annotation.get(CoreAnnotations.SentencesAnnotation.class).get(mention.sentNum - 1);
CoreLabel token = sentence.get(CoreAnnotations.TokensAnnotation.class).get(mention.headIndex - 1);
coreferentTokens.add(token);
}
for (CoreLabel token : coreferentTokens) {
token.set(CorefCoreAnnotations.CorefClusterAnnotation.class, coreferentTokens);
}
}
}
use of edu.stanford.nlp.coref.data.CorefChain.CorefMention in project CoreNLP by stanfordnlp.
the class HybridCorefAnnotator method getLinks.
public static List<Pair<IntTuple, IntTuple>> getLinks(Map<Integer, CorefChain> result) {
List<Pair<IntTuple, IntTuple>> links = new ArrayList<>();
CorefChain.CorefMentionComparator comparator = new CorefChain.CorefMentionComparator();
for (CorefChain c : result.values()) {
List<CorefMention> s = c.getMentionsInTextualOrder();
for (CorefMention m1 : s) {
for (CorefMention m2 : s) {
if (comparator.compare(m1, m2) == 1)
links.add(new Pair<>(m1.position, m2.position));
}
}
}
return links;
}
use of edu.stanford.nlp.coref.data.CorefChain.CorefMention in project CoreNLP by stanfordnlp.
the class CorefAnnotator method getLinks.
public static List<Pair<IntTuple, IntTuple>> getLinks(Map<Integer, CorefChain> result) {
List<Pair<IntTuple, IntTuple>> links = new ArrayList<>();
CorefChain.CorefMentionComparator comparator = new CorefChain.CorefMentionComparator();
for (CorefChain c : result.values()) {
List<CorefMention> s = c.getMentionsInTextualOrder();
for (CorefMention m1 : s) {
for (CorefMention m2 : s) {
if (comparator.compare(m1, m2) == 1) {
links.add(new Pair<>(m1.position, m2.position));
}
}
}
}
return links;
}
use of edu.stanford.nlp.coref.data.CorefChain.CorefMention in project CoreNLP by stanfordnlp.
the class DeterministicCorefAnnotatorITest method testDeterministicCorefAnnotator.
public void testDeterministicCorefAnnotator() throws Exception {
// create annotation with text
String text = "Dan Ramage is working for\nMicrosoft. He's in Seattle!\nAt least, he used to be. Ed is not in Seattle.";
Annotation document = new Annotation(text);
// annotate text with pipeline
pipeline.annotate(document);
// test CorefGraphAnnotation
Map<Integer, CorefChain> corefChains = document.get(CorefCoreAnnotations.CorefChainAnnotation.class);
Assert.assertNotNull(corefChains);
// test chainID = m.corefClusterID
for (int chainID : corefChains.keySet()) {
CorefChain c = corefChains.get(chainID);
for (CorefMention m : c.getMentionsInTextualOrder()) {
Assert.assertEquals(m.corefClusterID, chainID);
}
}
// test CorefClusterIdAnnotation
List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
CoreLabel ramageToken = sentences.get(0).get(CoreAnnotations.TokensAnnotation.class).get(1);
CoreLabel heToken = sentences.get(1).get(CoreAnnotations.TokensAnnotation.class).get(0);
Integer ramageClusterId = ramageToken.get(CorefCoreAnnotations.CorefClusterIdAnnotation.class);
Assert.assertNotNull(ramageClusterId);
Assert.assertSame(ramageClusterId, heToken.get(CorefCoreAnnotations.CorefClusterIdAnnotation.class));
}
use of edu.stanford.nlp.coref.data.CorefChain.CorefMention in project CoreNLP by stanfordnlp.
the class DeterministicCorefAnnotatorITest method testSameString.
/**
* Tests named entities with exact string matches (also tests some more pronouns).
* @throws Exception
*/
public void testSameString() throws Exception {
// create annotation with text
String text = "Your mom thinks she lives in Denver, but it's a big city. She actually lives outside of Denver.";
Annotation document = new Annotation(text);
// annotate text with pipeline
pipeline.annotate(document);
// test CorefChainAnnotation
Map<Integer, CorefChain> chains = document.get(CorefCoreAnnotations.CorefChainAnnotation.class);
Assert.assertNotNull(chains);
// test chainID = m.corefClusterID
for (int chainID : chains.keySet()) {
CorefChain c = chains.get(chainID);
for (CorefMention m : c.getMentionsInTextualOrder()) {
Assert.assertEquals(m.corefClusterID, chainID);
}
}
// test CorefClusterIdAnnotation
List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
CoreLabel yourMomsToken = sentences.get(0).get(CoreAnnotations.TokensAnnotation.class).get(1);
CoreLabel sheToken1 = sentences.get(0).get(CoreAnnotations.TokensAnnotation.class).get(3);
CoreLabel sheToken2 = sentences.get(1).get(CoreAnnotations.TokensAnnotation.class).get(0);
CoreLabel denverToken1 = sentences.get(0).get(CoreAnnotations.TokensAnnotation.class).get(6);
CoreLabel denverToken2 = sentences.get(1).get(CoreAnnotations.TokensAnnotation.class).get(5);
Integer yourMomsClusterId = yourMomsToken.get(CorefCoreAnnotations.CorefClusterIdAnnotation.class);
Integer she1ClusterId = sheToken1.get(CorefCoreAnnotations.CorefClusterIdAnnotation.class);
Integer she2ClusterId = sheToken2.get(CorefCoreAnnotations.CorefClusterIdAnnotation.class);
Integer denver1ClusterId = denverToken1.get(CorefCoreAnnotations.CorefClusterIdAnnotation.class);
Integer denver2ClusterId = denverToken2.get(CorefCoreAnnotations.CorefClusterIdAnnotation.class);
Assert.assertNotNull(yourMomsClusterId);
Assert.assertNotNull(she1ClusterId);
Assert.assertNotNull(she2ClusterId);
Assert.assertNotNull(denver1ClusterId);
Assert.assertNotNull(denver2ClusterId);
Assert.assertSame(yourMomsClusterId, she1ClusterId);
Assert.assertSame(yourMomsClusterId, she2ClusterId);
Assert.assertSame(denver1ClusterId, denver2ClusterId);
Assert.assertNotSame(yourMomsClusterId, denver1ClusterId);
// test CorefClusterAnnotation
// Assert.assertEquals(yourMomsToken.get(CorefCoreAnnotations.CorefClusterAnnotation.class), sheToken1.get(CorefCoreAnnotations.CorefClusterAnnotation.class));
// Assert.assertEquals(yourMomsToken.get(CorefCoreAnnotations.CorefClusterAnnotation.class), sheToken2.get(CorefCoreAnnotations.CorefClusterAnnotation.class));
// Assert.assertEquals(denverToken1.get(CorefCoreAnnotations.CorefClusterAnnotation.class), denverToken2.get(CorefCoreAnnotations.CorefClusterAnnotation.class));
}
Aggregations