edu.stanford.nlp.ling.RVFDatum Maven / Gradle / Ivy
package edu.stanford.nlp.ling;
import edu.stanford.nlp.stats.ClassicCounter;
import edu.stanford.nlp.stats.Counter;
import java.util.Collection;
import java.util.Collections;
/**
* A basic implementation of the 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 Jenny Finkel
* [email protected]
* @author Sarah Spikes ([email protected]) [templatized]
*
* @param
* The type of the label of the datum
* @param
* The type of individual features stored in the datum
*/
public class RVFDatum implements Datum {
private static final long serialVersionUID = -255312811814660438L;
/**
* features for this Datum
*/
private final Counter features;
/**
* labels for this Datum. Invariant: always non-null
*/
private L label; // = null;
/**
* Constructs a new RVFDatum with the given features and label.
*/
public RVFDatum(Counter features, L label) {
this.features = features;
setLabel(label);
}
/**
* Constructs a new RVFDatum taking the data from a Datum. Implementation
* note: This constructor allocates its own counter over features, but is
* only guaranteed correct if the label and feature names are immutable.
*
* @param m
* The Datum to copy.
*/
public RVFDatum(Datum m) {
this.features = new ClassicCounter();
for (F key : m.asFeatures()) {
features.incrementCount(key, 1.0);
}
setLabel(m.label());
}
/**
* Constructs a new RVFDatum with the given features and no labels.
*/
public RVFDatum(Counter features) {
this.features = features;
}
/**
* Constructs a new RVFDatum with no features or labels.
*/
public RVFDatum() {
this((ClassicCounter) null);
}
/**
* Returns the Counter of features and values
*/
public Counter asFeaturesCounter() {
return features;
}
/**
* Returns the list of features without values
*/
public Collection asFeatures() {
return features.keySet();
}
/**
* Removes all currently assigned Labels for this Datum then adds the given
* Label. Calling setLabel(null) effectively clears all labels.
*/
public void setLabel(L label) {
this.label = label;
}
/**
* Returns a String representation of this BasicDatum (lists features and
* labels).
*/
@Override
public String toString() {
return "RVFDatum[features=" + asFeaturesCounter() + ",label=" + label() + "]";
}
public L label() {
return label;
}
public Collection labels() {
return Collections.singletonList(label);
}
public double getFeatureCount(F feature) {
return features.getCount(feature);
}
/**
* Returns whether the given RVFDatum contains the same features with the same
* values as this RVFDatum. An RVFDatum can only be equal to another RVFDatum.
* Implementation note: Doesn't check the labels, should we change
* this?
*/
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof RVFDatum)) {
return (false);
}
RVFDatum d = (RVFDatum) o;
return features.equals(d.asFeaturesCounter());
}
/** {@inheritDoc} */
@Override
public int hashCode() {
return features.hashCode();
}
}