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

edu.stanford.nlp.coref.statistical.ClustererDataLoader 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.statistical;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import edu.stanford.nlp.io.IOUtils;
import edu.stanford.nlp.stats.ClassicCounter;
import edu.stanford.nlp.stats.Counter;
import edu.stanford.nlp.util.Pair;

/**
 * Loads the data used to train {@link Clusterer}.
 * @author Kevin Clark
 */
public class ClustererDataLoader {
  public static class ClustererDoc {
    public final int id;
    public final Counter> classificationScores;
    public final Counter> rankingScores;
    public final Counter anaphoricityScores;
    public final List> goldClusters;
    public final Map> mentionToGold;
    public final List mentions;
    public final Map mentionTypes;
    public final Set> positivePairs;
    public final Map mentionIndices;

    public ClustererDoc(int id,
        Counter> classificationScores,
        Counter> rankingScores,
        Counter anaphoricityScores,
        Map, Boolean> labeledPairs,
        List> goldClusters,
        Map mentionTypes) {
      this.id = id;
      this.classificationScores = classificationScores;
      this.rankingScores = rankingScores;
      this.goldClusters = goldClusters;
      this.mentionTypes = mentionTypes;
      this.anaphoricityScores = anaphoricityScores;

      positivePairs = labeledPairs.keySet().stream().filter(p -> labeledPairs.get(p))
          .collect(Collectors.toSet());
      Set mentionsSet = new HashSet<>();
      for (Pair pair : labeledPairs.keySet()) {
        mentionsSet.add(pair.first);
        mentionsSet.add(pair.second);
      }

      mentions = new ArrayList<>(mentionsSet);
      Collections.sort(mentions, (m1, m2) -> {
        Pair p = new Pair<>(m1, m2);
        return m1 == m2 ? 0 : (classificationScores.containsKey(p) ? -1 : 1);
      });
      mentionIndices = new HashMap<>();
      for (int i = 0; i < mentions.size(); i++) {
        mentionIndices.put(mentions.get(i), i);
      }

      mentionToGold = new HashMap<>();
      if (goldClusters != null) {
        for (List gold : goldClusters) {
          for (int m : gold) {
            mentionToGold.put(m, gold);
          }
        }
      }
    }
  }

  public static List loadDocuments(int maxDocs) throws Exception {
    Map, Boolean>> labeledPairs =
        IOUtils.readObjectFromFile(StatisticalCorefTrainer.datasetFile);
    Map> mentionTypes =
        IOUtils.readObjectFromFile(StatisticalCorefTrainer.mentionTypesFile);
    Map>> goldClusters =
        IOUtils.readObjectFromFile(StatisticalCorefTrainer.goldClustersFile);
    Map>> classificationScores =
        IOUtils.readObjectFromFile(StatisticalCorefTrainer.pairwiseModelsPath
            + StatisticalCorefTrainer.CLASSIFICATION_MODEL + "/"
            + StatisticalCorefTrainer.predictionsName + ".ser");
    Map>> rankingScores =
        IOUtils.readObjectFromFile(StatisticalCorefTrainer.pairwiseModelsPath
            + StatisticalCorefTrainer.RANKING_MODEL + "/"
            + StatisticalCorefTrainer.predictionsName + ".ser");
    Map>> anaphoricityScoresLoaded =
        IOUtils.readObjectFromFile(StatisticalCorefTrainer.pairwiseModelsPath
            + StatisticalCorefTrainer.ANAPHORICITY_MODEL + "/"
            + StatisticalCorefTrainer.predictionsName + ".ser");

    Map> anaphoricityScores = new HashMap<>();
    for (Map.Entry>> e : anaphoricityScoresLoaded.entrySet()) {
      Counter scores = new ClassicCounter<>();
      e.getValue().entrySet().forEach(e2 -> {
        scores.incrementCount(e2.getKey().second, e2.getValue());
      });
      anaphoricityScores.put(e.getKey(), scores);
    }

    return labeledPairs.keySet().stream().sorted().limit(maxDocs).map(i -> new ClustererDoc(i,
        classificationScores.get(i), rankingScores.get(i), anaphoricityScores.get(i),
        labeledPairs.get(i), goldClusters.get(i), mentionTypes.get(i)))
        .collect(Collectors.toList());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy