use of edu.stanford.nlp.dcoref.CorefChain.CorefMention in project CoreNLP by stanfordnlp.
the class DeterministicCorefAnnotator method addObsoleteCoreferenceAnnotations.
// for backward compatibility with a few old things
// TODO: Aim to get rid of this entirely
private static void addObsoleteCoreferenceAnnotations(Annotation annotation, List<List<Mention>> orderedMentions, Map<Integer, CorefChain> result) {
List<Pair<IntTuple, IntTuple>> links = SieveCoreferenceSystem.getLinks(result);
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
//
// cdm 2013: this block didn't seem to be doing anything needed....
// List<List<CoreLabel>> sents = new ArrayList<List<CoreLabel>>();
// for (CoreMap sentence: annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
// List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
// sents.add(tokens);
// }
// 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 = orderedMentions.get(srcSent - 1).get(link.first.get(1) - 1).headIndex + 1;
int dstSent = link.second.get(0);
int dstTok = orderedMentions.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.dcoref.CorefChain.CorefMention in project CoreNLP by stanfordnlp.
the class SieveCoreferenceSystem 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.dcoref.CorefChain.CorefMention in project CoreNLP by stanfordnlp.
the class SieveCoreferenceSystem method corefReturnHybridOutput.
public Map<Integer, edu.stanford.nlp.coref.data.CorefChain> corefReturnHybridOutput(Document document) throws Exception {
// Multi-pass sieve coreference resolution
for (int i = 0; i < sieves.length; i++) {
currentSieve = i;
DeterministicCorefSieve sieve = sieves[i];
// Do coreference resolution using this pass
coreference(document, sieve);
}
// post processing (e.g., removing singletons, appositions for conll)
if ((!Constants.USE_GOLD_MENTIONS && doPostProcessing) || replicateCoNLL)
postProcessing(document);
// coref system output: edu.stanford.nlp.hcoref.data.CorefChain
Map<Integer, edu.stanford.nlp.coref.data.CorefChain> result = Generics.newHashMap();
for (CorefCluster c : document.corefClusters.values()) {
// build mentionsMap and represents
Map<IntPair, Set<edu.stanford.nlp.coref.data.CorefChain.CorefMention>> mentionsMap = Generics.newHashMap();
IntPair keyPair = new IntPair(0, 0);
mentionsMap.put(keyPair, new HashSet<>());
Mention represents = null;
edu.stanford.nlp.coref.data.CorefChain.CorefMention representsHybridVersion = null;
for (Mention mention : c.getCorefMentions()) {
// convert dcoref CorefMention to hcoref CorefMention
//IntPair mentionPosition = new IntPair(mention.sentNum, mention.headIndex);
IntTuple mentionPosition = document.positions.get(mention);
CorefMention dcorefMention = new CorefMention(mention, mentionPosition);
// tokens need the hcoref version of CorefClusterIdAnnotation
mention.headWord.set(edu.stanford.nlp.coref.CorefCoreAnnotations.CorefClusterIdAnnotation.class, mention.corefClusterID);
// drop the dcoref version of CorefClusterIdAnnotation
mention.headWord.remove(CorefCoreAnnotations.CorefClusterIdAnnotation.class);
// make the hcoref mention
edu.stanford.nlp.coref.data.CorefChain.CorefMention hcorefMention = new edu.stanford.nlp.coref.data.CorefChain.CorefMention(edu.stanford.nlp.coref.data.Dictionaries.MentionType.valueOf(dcorefMention.mentionType.name()), edu.stanford.nlp.coref.data.Dictionaries.Number.valueOf(dcorefMention.number.name()), edu.stanford.nlp.coref.data.Dictionaries.Gender.valueOf(dcorefMention.gender.name()), edu.stanford.nlp.coref.data.Dictionaries.Animacy.valueOf(dcorefMention.animacy.name()), dcorefMention.startIndex, dcorefMention.endIndex, dcorefMention.headIndex, dcorefMention.corefClusterID, dcorefMention.mentionID, dcorefMention.sentNum, dcorefMention.position, dcorefMention.mentionSpan);
mentionsMap.get(keyPair).add(hcorefMention);
if (mention.moreRepresentativeThan(represents)) {
represents = mention;
representsHybridVersion = hcorefMention;
}
}
edu.stanford.nlp.coref.data.CorefChain hybridCorefChain = new edu.stanford.nlp.coref.data.CorefChain(c.clusterID, mentionsMap, representsHybridVersion);
result.put(c.clusterID, hybridCorefChain);
}
return result;
}
Aggregations