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

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

import edu.stanford.nlp.coref.statistical.SimpleLinearClassifier.Loss;

import edu.stanford.nlp.stats.Counter;

/**
 * A max-margin mention-ranking coreference model.
 * @author Kevin Clark
 */
public class MaxMarginMentionRanker extends PairwiseModel {
  public enum ErrorType {
    FN(0), FN_PRON(1), FL(2), WL(3);
    public final int id;
    private ErrorType(int id) {
      this.id = id;
    }
  };

  private final Loss[] losses = new Loss[ErrorType.values().length];
  private final Loss loss;

  public final double[] costs;
  public final boolean multiplicativeCost;

  public static class Builder extends PairwiseModel.Builder {
    private double[] costs = new double[] {1.2, 1.2, 0.5, 1.0};
    private boolean multiplicativeCost = true;

    public Builder(String name, MetaFeatureExtractor meta) {
      super(name, meta);
    }

    public Builder setCosts(double fnCost, double fnPronounCost, double faCost, double wlCost)
      { this.costs = new double[] {fnCost, fnPronounCost, faCost, wlCost}; return this; }
    public Builder multiplicativeCost(boolean multiplicativeCost)
      { this.multiplicativeCost = multiplicativeCost; return this; }

    @Override
    public MaxMarginMentionRanker build() {
      return new MaxMarginMentionRanker(this);
    }
  }

  public static Builder newBuilder(String name, MetaFeatureExtractor meta) {
    return new Builder(name, meta);
  }

  public MaxMarginMentionRanker(Builder builder) {
    super(builder);
    costs = builder.costs;
    multiplicativeCost = builder.multiplicativeCost;
    if (multiplicativeCost) {
      for (ErrorType et : ErrorType.values()) {
        losses[et.id] = SimpleLinearClassifier.maxMargin(builder.costs[et.id]);
      }
    }
    loss = SimpleLinearClassifier.maxMargin(1.0);
  }

  public void learn(Example correct, Example incorrect,
      Map mentionFeatures, Compressor compressor,
      ErrorType errorType) {

    Counter cFeatures = meta.getFeatures(correct, mentionFeatures, compressor);
    Counter iFeatures = meta.getFeatures(incorrect, mentionFeatures, compressor);
    for (Map.Entry e : cFeatures.entrySet()) {
      iFeatures.decrementCount(e.getKey(), e.getValue());
    }
    if (multiplicativeCost) {
      classifier.learn(iFeatures, 1.0, costs[errorType.id], loss);
    } else {
      classifier.learn(iFeatures, 1.0, 1.0, losses[errorType.id]);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy