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

edu.stanford.nlp.ling.BasicDatum Maven / Gradle / Ivy

package edu.stanford.nlp.ling;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Basic implementation of Datum interface that can be constructed with a
 * Collection of features and one more more labels. The features must be
 * specified
 * at construction, but the labels can be set and/or changed later.
 *
 * @author Joseph Smarr ([email protected])
 * @author Sarah Spikes ([email protected]) (Templatization)
 *
 * @param  The type of the labels in the Dataset
 * @param  The type of the features in the Dataset
 */
public class BasicDatum implements Datum {

  /**
   * features for this Datum
   */
  @SuppressWarnings({"NonSerializableFieldInSerializableClass"})
  private final Collection features;

  /**
   * labels for this Datum. Invariant: always non-null
   */
  @SuppressWarnings({"NonSerializableFieldInSerializableClass"})
  private final List labels = new ArrayList();

  /**
   * Constructs a new BasicDatum with the given features and labels.
   */
  public BasicDatum(Collection features, Collection labels) {
    this(features);
    setLabels(labels);
  }

  /**
   * Constructs a new BasicDatum with the given features and label.
   */
  public BasicDatum(Collection features, LabelType label) {
    this(features);
    setLabel(label);
  }

  /**
   * Constructs a new BasicDatum with the given features and no labels.
   */
  public BasicDatum(Collection features) {
    this.features = features;
  }

  /**
   * Constructs a new BasicDatum with no features or labels.
   */
  public BasicDatum() {
    this(null);
  }

  /**
   * Returns the collection that this BasicDatum was constructed with.
   */
  public Collection asFeatures() {
    return (features);
  }

  /**
   * Returns the first label for this Datum, or null if none have been set.
   */
  public LabelType label() {
    return ((labels.size() > 0) ?  labels.get(0) : null);
  }

  /**
   * Returns the complete List of labels for this Datum, which may be empty.
   */
  public Collection labels() {
    return labels;
  }

  /**
   * Removes all currently assigned Labels for this Datum then adds the
   * given Label.
   * Calling setLabel(null) effectively clears all labels.
   */
  public void setLabel(LabelType label) {
    labels.clear();
    addLabel(label);
  }

  /**
   * Removes all currently assigned labels for this Datum then adds all
   * of the given Labels.
   */
  public void setLabels(Collection labels) {
    this.labels.clear();
    if (labels != null) {
      this.labels.addAll(labels);
    }
  }

  /**
   * Adds the given Label to the List of labels for this Datum if it is not
   * null.
   */
  public void addLabel(LabelType label) {
    if (label != null) {
      labels.add(label);
    }
  }

  /**
   * Returns a String representation of this BasicDatum (lists features and labels).
   */
  @Override
  public String toString() {
    return ("BasicDatum[features=" + asFeatures() + ",labels=" + labels() + "]");
  }


  /**
   * Returns whether the given Datum contains the same features as this Datum.
   * Doesn't check the labels, should we change this?
   */
  @SuppressWarnings("unchecked")
  @Override
  public boolean equals(Object o) {
    if (!(o instanceof Datum)) {
      return (false);
    }

    Datum d = (Datum) o;
    return features.equals(d.asFeatures());
  }

  public int hashCode() {
    return features.hashCode();
  }

  private static final long serialVersionUID = -4857004070061779966L;

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy