All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.stanford.nlp.coref.CorefPrinter Maven / Gradle / Ivy

Go to download

Stanford CoreNLP provides a set of natural language analysis tools which can take raw English language text input and give the base forms of words, their parts of speech, whether they are names of companies, people, etc., normalize dates, times, and numeric quantities, mark up the structure of sentences in terms of phrases and word dependencies, and indicate which noun phrases refer to the same entities. It provides the foundational building blocks for higher level text understanding applications.

There is a newer version: 4.5.7
Show newest version
package edu.stanford.nlp.coref;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import edu.stanford.nlp.coref.data.Document;
import edu.stanford.nlp.coref.data.Mention;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.Generics;

/**
 * Class for printing out coreference output.
 * @author Heeyoung Lee
 * @author Kevin Clark
 */
public class CorefPrinter {
  public static String printConllOutput(Document document, boolean gold) {
    return printConllOutput(document, gold, false);
  }

  public static String printConllOutput(Document document, boolean gold, boolean filterSingletons) {
    List> orderedMentions = gold ? document.goldMentions : document.predictedMentions;
    if (filterSingletons) {
      orderedMentions = orderedMentions.stream().map(
          ml -> ml.stream().filter(m -> document.corefClusters.get(m.corefClusterID) != null &&
            document.corefClusters.get(m.corefClusterID).size() > 1)
            .collect(Collectors.toList()))
          .collect(Collectors.toList());
    }
    return CorefPrinter.printConllOutput(document, orderedMentions, gold);
  }

  public static String printConllOutput(Document document,
      List> orderedMentions, boolean gold) {
    Annotation anno = document.annotation;
    List> conllDocSentences = document.conllDoc.sentenceWordLists;
    String docID = anno.get(CoreAnnotations.DocIDAnnotation.class);
    StringBuilder sb = new StringBuilder();
    sb.append("#begin document ").append(docID).append("\n");
    List sentences = anno.get(CoreAnnotations.SentencesAnnotation.class);
    for(int sentNum = 0 ; sentNum < sentences.size() ; sentNum++){
      List sentence = sentences.get(sentNum).get(CoreAnnotations.TokensAnnotation.class);
      List conllSentence = conllDocSentences.get(sentNum);
      Map> mentionBeginOnly = Generics.newHashMap();
      Map> mentionEndOnly = Generics.newHashMap();
      Map> mentionBeginEnd = Generics.newHashMap();

      for(int i=0 ; i());
        mentionEndOnly.put(i, new LinkedHashSet<>());
        mentionBeginEnd.put(i, new LinkedHashSet<>());
      }

      for(Mention m : orderedMentions.get(sentNum)) {
        if(m.startIndex==m.endIndex-1) {
          mentionBeginEnd.get(m.startIndex).add(m);
        } else {
          mentionBeginOnly.get(m.startIndex).add(m);
          mentionEndOnly.get(m.endIndex-1).add(m);
        }
      }

      for(int i=0 ; i 0) {
            sb2.append("|");
          }
          int corefClusterId = (gold)? m.goldCorefClusterID:m.corefClusterID;
          sb2.append("(").append(corefClusterId);
        }
        for(Mention m : mentionBeginEnd.get(i)){
          if (sb2.length() > 0) {
            sb2.append("|");
          }
          int corefClusterId = (gold)? m.goldCorefClusterID:m.corefClusterID;
          sb2.append("(").append(corefClusterId).append(")");
        }
        for(Mention m : mentionEndOnly.get(i)){
          if (sb2.length() > 0) {
            sb2.append("|");
          }
          int corefClusterId = (gold)? m.goldCorefClusterID:m.corefClusterID;
          sb2.append(corefClusterId).append(")");
        }
        if(sb2.length() == 0) sb2.append("-");

        String[] columns = conllSentence.get(i);
        for(int j = 0 ; j < columns.length-1 ; j++){
          String column = columns[j];
          sb.append(column).append("\t");
        }
        sb.append(sb2).append("\n");
      }
      sb.append("\n");
    }

    sb.append("#end document").append("\n");

    return sb.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy