Search in sources :

Example 11 with IntTuple

use of edu.stanford.nlp.util.IntTuple in project CoreNLP by stanfordnlp.

the class Document method extractGoldLinks.

/** Extract gold coref link information */
protected void extractGoldLinks() {
    //    List<List<Mention>> orderedMentionsBySentence = this.getOrderedMentions();
    List<Pair<IntTuple, IntTuple>> links = new ArrayList<>();
    // position of each mention in the input matrix, by id
    Map<Integer, IntTuple> positions = Generics.newHashMap();
    // positions of antecedents
    Map<Integer, List<IntTuple>> antecedents = Generics.newHashMap();
    for (int i = 0; i < goldMentions.size(); i++) {
        for (int j = 0; j < goldMentions.get(i).size(); j++) {
            Mention m = goldMentions.get(i).get(j);
            int id = m.mentionID;
            IntTuple pos = new IntTuple(2);
            pos.set(0, i);
            pos.set(1, j);
            positions.put(id, pos);
            antecedents.put(id, new ArrayList<>());
        }
    }
    //    SieveCoreferenceSystem.debugPrintMentions(System.err, "", goldOrderedMentionsBySentence);
    for (List<Mention> mentions : goldMentions) {
        for (Mention m : mentions) {
            int id = m.mentionID;
            IntTuple src = positions.get(id);
            assert (src != null);
            if (m.originalRef >= 0) {
                IntTuple dst = positions.get(m.originalRef);
                if (dst == null) {
                    throw new RuntimeException("Cannot find gold mention with ID=" + m.originalRef);
                }
                // to deal with cataphoric annotation
                while (dst.get(0) > src.get(0) || (dst.get(0) == src.get(0) && dst.get(1) > src.get(1))) {
                    Mention dstMention = goldMentions.get(dst.get(0)).get(dst.get(1));
                    m.originalRef = dstMention.originalRef;
                    dstMention.originalRef = id;
                    if (m.originalRef < 0)
                        break;
                    dst = positions.get(m.originalRef);
                }
                if (m.originalRef < 0)
                    continue;
                // A B C: if A<-B, A<-C => make a link B<-C
                for (int k = dst.get(0); k <= src.get(0); k++) {
                    for (int l = 0; l < goldMentions.get(k).size(); l++) {
                        if (k == dst.get(0) && l < dst.get(1))
                            continue;
                        if (k == src.get(0) && l > src.get(1))
                            break;
                        IntTuple missed = new IntTuple(2);
                        missed.set(0, k);
                        missed.set(1, l);
                        if (links.contains(new Pair<>(missed, dst))) {
                            antecedents.get(id).add(missed);
                            links.add(new Pair<>(src, missed));
                        }
                    }
                }
                links.add(new Pair<>(src, dst));
                assert (antecedents.get(id) != null);
                antecedents.get(id).add(dst);
                List<IntTuple> ants = antecedents.get(m.originalRef);
                assert (ants != null);
                for (IntTuple ant : ants) {
                    antecedents.get(id).add(ant);
                    links.add(new Pair<>(src, ant));
                }
            }
        }
    }
    goldLinks = links;
}
Also used : ArrayList(java.util.ArrayList) IntTuple(edu.stanford.nlp.util.IntTuple) List(java.util.List) ArrayList(java.util.ArrayList) Pair(edu.stanford.nlp.util.Pair)

Aggregations

IntTuple (edu.stanford.nlp.util.IntTuple)11 CoreLabel (edu.stanford.nlp.ling.CoreLabel)6 CoreMap (edu.stanford.nlp.util.CoreMap)6 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)5 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)5 IndexedWord (edu.stanford.nlp.ling.IndexedWord)4 GrammaticalRelation (edu.stanford.nlp.trees.GrammaticalRelation)4 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)3 Pair (edu.stanford.nlp.util.Pair)3 BasicDependenciesAnnotation (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.BasicDependenciesAnnotation)2 ArrayList (java.util.ArrayList)2 CorefChain (edu.stanford.nlp.dcoref.CorefChain)1 CorefMention (edu.stanford.nlp.dcoref.CorefChain.CorefMention)1 CorefCoreAnnotations (edu.stanford.nlp.dcoref.CorefCoreAnnotations)1 TokensAnnotation (edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation)1 Tree (edu.stanford.nlp.trees.Tree)1 TreeAnnotation (edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation)1 IntPair (edu.stanford.nlp.util.IntPair)1 List (java.util.List)1