gate.plugin.learningframework.stats.Stats Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of learningframework Show documentation
Show all versions of learningframework Show documentation
A GATE plugin that provides many different machine learning
algorithms for a wide range of NLP-related machine learning tasks like
text classification, tagging, or chunking.
package gate.plugin.learningframework.stats;
import gate.util.GateRuntimeException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
/**
* Simple wrapper around either SummaryStatistics or our own thing for strings.
*
* The kind of backing object is determined at construction time when
* the (first) object to be added already needs to get passed.
*
* @author Johann Petrak
*/
public class Stats {
protected SummaryStatistics numStats;
protected Map stringStats;
protected long n = 0;
public Stats(Object firstValue) {
if(firstValue instanceof String) {
stringStats = new HashMap<>();
} else if(firstValue instanceof String[]) {
numStats = new SummaryStatistics();
stringStats = new HashMap<>();
} else {
numStats = new SummaryStatistics();
}
}
private void addStringValue(Object value) {
String val = (String)value;
if(stringStats.containsKey(val)) {
stringStats.put(val,stringStats.get(val)+1);
} else {
stringStats.put(val,1l);
}
}
/**
* Add a value to the stats object.
* @param value value to add
*/
public void addValue(Object value) {
if(value instanceof String) {
if(stringStats == null) {
throw new GateRuntimeException("Stats object did not expect a String");
}
addStringValue(value);
} else {
if(numStats == null) {
throw new GateRuntimeException("Stats object did not expect a non-String");
}
if(value instanceof Double) {
numStats.addValue((Double)value);
} else if(value instanceof Number) {
numStats.addValue(((Number) value).doubleValue());
} else if(value instanceof String) {
// none for now
} else if(value instanceof Boolean) {
numStats.addValue(((Boolean)value) ? 1.0 : 0.0);
} else if(value instanceof List) {
@SuppressWarnings("unchecked")
List