org.cleartk.timeml.eval.Model Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cleartk-timeml Show documentation
Show all versions of cleartk-timeml Show documentation
ClearTK annotators for temporal information extraction
/*
* Copyright (c) 2013, Regents of the University of Colorado
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.cleartk.timeml.eval;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.uima.analysis_component.AnalysisComponent;
import org.apache.uima.analysis_engine.AnalysisEngineDescription;
import org.apache.uima.cas.Feature;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.TOP;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
import org.cleartk.classifier.CleartkSequenceAnnotator;
import org.cleartk.classifier.DataWriter;
import org.cleartk.classifier.SequenceDataWriter;
import org.cleartk.classifier.jar.DefaultDataWriterFactory;
import org.cleartk.classifier.jar.DefaultSequenceDataWriterFactory;
import org.cleartk.classifier.jar.DirectoryDataWriterFactory;
import org.cleartk.classifier.jar.GenericJarClassifierFactory;
import org.cleartk.classifier.jar.JarClassifierBuilder;
import org.cleartk.classifier.jar.Train;
import org.cleartk.classifier.viterbi.DefaultOutcomeFeatureExtractor;
import org.cleartk.classifier.viterbi.ViterbiDataWriterFactory;
import org.cleartk.eval.AnnotationStatistics;
import org.cleartk.timeml.type.Anchor;
import org.cleartk.timeml.type.TemporalLink;
import org.uimafit.factory.AnalysisEngineFactory;
import org.uimafit.util.JCasUtil;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
/**
* Stores information needed for training and evaluating a machine learning model
*
*
* Copyright (c) 2013, Regents of the University of Colorado
* All rights reserved.
*
* @author Steven Bethard
*/
class Model {
public static class Params {
public Class> dataWriterClass;
public int nViterbiOutcomes;
public String[] trainingArguments;
public Params(Class> dataWriterClass, String... trainingArguments) {
this(dataWriterClass, 0, trainingArguments);
}
public Params(Class> dataWriterClass, int nViterbiOutcomes, String ... trainingArguments) {
this.dataWriterClass = dataWriterClass;
this.nViterbiOutcomes = nViterbiOutcomes;
this.trainingArguments = trainingArguments;
}
@Override
public String toString() {
Objects.ToStringHelper helper = Objects.toStringHelper(this.getClass());
helper.add("dataWriterClass", this.dataWriterClass.getSimpleName());
if (this.nViterbiOutcomes > 0) {
helper.add("nViterbiOutcomes", this.nViterbiOutcomes);
}
if (this.trainingArguments.length > 0) {
helper.add("trainingArguments", Joiner.on(' ').join(this.trainingArguments));
}
return helper.toString();
}
}
public enum EvaluationType {
NORMAL, GOLD_SPANS, SYSTEM_SPANS, INTERSECTED_SPANS
}
public enum LoggingType {
NONE, SYSTEM_PREDICTIONS
}
public static final File DEFAULT_DIRECTORY = null;
public String name;
public List> prerequisites;
private Class extends AnalysisComponent> annotatorClass;
public Model.Params bestParams;
public List paramsToSearch;
private Model.EvaluationType evaluationType;
private Model.LoggingType loggingType;
private Class annotationClass;
Function annotationToSpan;
private Function annotationToOutcome;
private String featureToRemove;
public Model(
String name,
List> prerequisites,
Class extends AnalysisComponent> annotatorClass,
Model.Params bestParams,
List paramsToSearch,
Model.EvaluationType evaluationType,
Model.LoggingType loggingType,
Class annotationClass,
Function annotationToSpan,
Function annotationToOutcome,
String featureToRemove) {
this.name = name;
this.prerequisites = prerequisites;
this.annotatorClass = annotatorClass;
this.bestParams = bestParams;
this.paramsToSearch = paramsToSearch;
this.evaluationType = evaluationType;
this.loggingType = loggingType;
this.annotationClass = annotationClass;
this.annotationToSpan = annotationToSpan;
this.annotationToOutcome = annotationToOutcome;
this.featureToRemove = featureToRemove;
}
@Override
public String toString() {
Objects.ToStringHelper helper = Objects.toStringHelper(this.getClass());
helper.add("name", this.name);
return helper.toString();
}
public AnalysisEngineDescription getWriterDescription(File directory, Model.Params params)
throws ResourceInitializationException {
AnalysisEngineDescription desc;
if (params.nViterbiOutcomes > 0) {
desc = AnalysisEngineFactory.createPrimitiveDescription(
this.annotatorClass,
CleartkSequenceAnnotator.PARAM_DATA_WRITER_FACTORY_CLASS_NAME,
ViterbiDataWriterFactory.class,
ViterbiDataWriterFactory.PARAM_DELEGATED_DATA_WRITER_FACTORY_CLASS,
DefaultDataWriterFactory.class,
DefaultDataWriterFactory.PARAM_DATA_WRITER_CLASS_NAME,
params.dataWriterClass,
ViterbiDataWriterFactory.PARAM_OUTCOME_FEATURE_EXTRACTOR_NAMES,
DefaultOutcomeFeatureExtractor.class,
DefaultOutcomeFeatureExtractor.PARAM_MOST_RECENT_OUTCOME,
1,
DefaultOutcomeFeatureExtractor.PARAM_LEAST_RECENT_OUTCOME,
params.nViterbiOutcomes,
DirectoryDataWriterFactory.PARAM_OUTPUT_DIRECTORY,
this.getModelDirectory(directory, params));
} else {
String datatWriterParamName;
if (SequenceDataWriter.class.isAssignableFrom(params.dataWriterClass)) {
datatWriterParamName = DefaultSequenceDataWriterFactory.PARAM_DATA_WRITER_CLASS_NAME;
} else if (DataWriter.class.isAssignableFrom(params.dataWriterClass)) {
datatWriterParamName = DefaultDataWriterFactory.PARAM_DATA_WRITER_CLASS_NAME;
} else {
throw new RuntimeException("Invalid data writer class: " + params.dataWriterClass);
}
desc = AnalysisEngineFactory.createPrimitiveDescription(
this.annotatorClass,
datatWriterParamName,
params.dataWriterClass,
DirectoryDataWriterFactory.PARAM_OUTPUT_DIRECTORY,
this.getModelDirectory(directory, params));
}
return desc;
}
public void train(File directory, Model.Params params) throws Exception {
Train.main(this.getModelDirectory(directory, params), params.trainingArguments);
}
public void cleanTrainingFiles(File directory, Model.Params params) {
this.cleanTrainingFiles(this.getModelDirectory(directory, params));
}
private void cleanTrainingFiles(File directory) {
File modelJarFile = JarClassifierBuilder.getModelJarFile(directory);
for (File file : directory.listFiles()) {
if (file.isDirectory()) {
this.cleanTrainingFiles(file);
}
if (!file.equals(modelJarFile)) {
file.delete();
}
}
}
public AnalysisEngineDescription getAnnotatorDescription(File directory, Model.Params params)
throws ResourceInitializationException {
return AnalysisEngineFactory.createPrimitiveDescription(
this.annotatorClass,
GenericJarClassifierFactory.PARAM_CLASSIFIER_JAR_PATH,
JarClassifierBuilder.getModelJarFile(this.getModelDirectory(directory, params)));
}
public void evaluate(JCas goldView, JCas systemView, AnnotationStatistics stats) {
Collection goldAnnotations = this.select(goldView);
Collection systemAnnotations = this.select(systemView);
final Set