Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.io.IOException;
import java.net.URI;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.analysis_engine.AnalysisEngineDescription;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.cas.Feature;
import org.apache.uima.collection.CollectionReader;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.component.ViewCreatorAnnotator;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.factory.AggregateBuilder;
import org.apache.uima.fit.factory.AnalysisEngineFactory;
import org.apache.uima.fit.pipeline.JCasIterator;
import org.apache.uima.fit.pipeline.SimplePipeline;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.cas.TOP;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.CasCopier;
import org.cleartk.corpus.timeml.PlainTextTlinkGoldAnnotator;
import org.cleartk.corpus.timeml.TempEval2013Writer;
import org.cleartk.corpus.timeml.TimeMlGoldAnnotator;
import org.cleartk.eval.AnnotationStatistics;
import org.cleartk.eval.Evaluation_ImplBase;
import org.cleartk.ml.liblinear.LibLinearStringOutcomeDataWriter;
import org.cleartk.opennlp.tools.ParserAnnotator;
import org.cleartk.opennlp.tools.PosTaggerAnnotator;
import org.cleartk.opennlp.tools.SentenceAnnotator;
import org.cleartk.snowball.DefaultSnowballStemmer;
import org.cleartk.timeml.event.EventAnnotator;
import org.cleartk.timeml.event.EventAspectAnnotator;
import org.cleartk.timeml.event.EventClassAnnotator;
import org.cleartk.timeml.event.EventModalityAnnotator;
import org.cleartk.timeml.event.EventPolarityAnnotator;
import org.cleartk.timeml.event.EventTenseAnnotator;
import org.cleartk.timeml.time.TimeAnnotator;
import org.cleartk.timeml.time.TimeTypeAnnotator;
import org.cleartk.timeml.tlink.TemporalLinkAnnotator_ImplBase;
import org.cleartk.timeml.tlink.TemporalLinkEventToDocumentCreationTimeAnnotator;
import org.cleartk.timeml.tlink.TemporalLinkEventToSameSentenceTimeAnnotator;
import org.cleartk.timeml.tlink.TemporalLinkEventToSubordinatedEventAnnotator;
import org.cleartk.timeml.type.Anchor;
import org.cleartk.timeml.type.DocumentCreationTime;
import org.cleartk.timeml.type.Event;
import org.cleartk.timeml.type.TemporalLink;
import org.cleartk.timeml.type.Text;
import org.cleartk.timeml.type.Time;
import org.cleartk.token.tokenizer.TokenAnnotator;
import org.cleartk.util.ViewUriUtil;
import org.cleartk.util.ae.UriToDocumentTextAnnotator;
import org.cleartk.util.cr.UriCollectionReader;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableTable;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Queues;
import com.google.common.collect.Sets;
import com.google.common.collect.Table;
import com.lexicalscope.jewel.cli.CliFactory;
import com.lexicalscope.jewel.cli.Option;
/**
* Trains and evaluates event, time and temporal relation models on the TempEval 2013 data.
*
*
* Copyright (c) 2013, Regents of the University of Colorado
* All rights reserved.
*
* @author Steven Bethard
*/
public class TempEval2013Evaluation
extends
Evaluation_ImplBase, Model.Params, AnnotationStatistics>> {
interface Options {
@Option(longName = "train-dirs")
List getTrainDirectories();
@Option(longName = "test-dirs", defaultToNull = true)
List getTestDirectories();
@Option(longName = "inferred-tlinks", defaultToNull = true)
List getInferredTLinksDirectories();
@Option(longName = "verb-clause-tlinks")
boolean getVerbClauseTLinks();
@Option(longName = "relations-only")
boolean getRelationsOnly();
@Option(longName = "tune", defaultToNull = true)
String getNameOfModelToTune();
@Option(longName = "train-only")
boolean getTrainOnly();
}
public static void main(String[] args) throws Exception {
Options options = CliFactory.parseArguments(Options.class, args);
List trainFiles = listAllFiles(options.getTrainDirectories());
List testFiles = listAllFiles(options.getTestDirectories());
// map names to models
List> allModels = Lists.> newArrayList(
TIME_EXTENT_MODEL,
TIME_TYPE_MODEL,
EVENT_EXTENT_MODEL,
EVENT_ASPECT_MODEL,
EVENT_CLASS_MODEL,
EVENT_MODALITY_MODEL,
EVENT_POLARITY_MODEL,
EVENT_TENSE_MODEL,
TLINK_EVENT_DOCTIME_MODEL,
TLINK_EVENT_SENTTIME_MODEL,
TLINK_EVENT_SUBORDEVENT_MODEL);
Map> nameToModel = Maps.newHashMap();
for (Model> model : allModels) {
nameToModel.put(model.name, model);
}
// determine which parameters each model should be trained with
ImmutableMultimap.Builder, Model.Params> modelsBuilder = ImmutableMultimap.builder();
String nameOfModelToTune = options.getNameOfModelToTune();
if (nameOfModelToTune == null) {
for (Model> model : allModels) {
if (!options.getRelationsOnly() || model.name.startsWith("tlink")) {
modelsBuilder.put(model, model.bestParams);
}
}
} else {
Model> modelToTune = nameToModel.get(nameOfModelToTune);
if (modelToTune == null) {
throw new IllegalArgumentException("No such model: " + nameOfModelToTune);
}
for (Model> model : getSortedPrerequisites(modelToTune)) {
if (!options.getRelationsOnly() || model.name.startsWith("tlink")) {
modelsBuilder.put(model, model.bestParams);
}
}
for (Model.Params params : modelToTune.paramsToSearch) {
modelsBuilder.put(modelToTune, params);
}
}
ImmutableMultimap, Model.Params> models = modelsBuilder.build();
// create the evaluation manager
File evalDir = new File("target/tempeval2013");
TempEval2013Evaluation evaluation = new TempEval2013Evaluation(
evalDir,
models,
options.getInferredTLinksDirectories(),
options.getVerbClauseTLinks(),
options.getRelationsOnly());
// just train a model
if (options.getTrainOnly()) {
if (!testFiles.isEmpty()) {
throw new IllegalArgumentException("Cannot specify test files when only training");
}
evaluation.train(evaluation.getCollectionReader(trainFiles), Model.DEFAULT_DIRECTORY);
for (Model> model : models.keySet()) {
for (Model.Params params : models.get(model)) {
model.cleanTrainingFiles(Model.DEFAULT_DIRECTORY, params);
}
}
} else {
// run a simple train-and-test
ImmutableTable, Model.Params, AnnotationStatistics> modelStats;
if (!testFiles.isEmpty()) {
modelStats = evaluation.trainAndTest(trainFiles, testFiles);
}
// run a cross-validation
else {
List, Model.Params, AnnotationStatistics>> foldStats;
foldStats = evaluation.crossValidation(trainFiles, 2);
// prepare a table of stats for all models and parameters
ImmutableTable.Builder, Model.Params, AnnotationStatistics> modelStatsBuilder = ImmutableTable.builder();
for (Model> model : models.keySet()) {
for (Model.Params params : models.get(model)) {
modelStatsBuilder.put(model, params, new AnnotationStatistics());
}
}
modelStats = modelStatsBuilder.build();
// combine all fold stats into a single overall stats
for (Table, Model.Params, AnnotationStatistics> foldTable : foldStats) {
for (Table.Cell, Model.Params, AnnotationStatistics> cell : foldTable.cellSet()) {
modelStats.get(cell.getRowKey(), cell.getColumnKey()).addAll(cell.getValue());
}
}
}
// print out all model performance
for (Model> model : models.keySet()) {
for (Model.Params params : modelStats.row(model).keySet()) {
System.err.printf("== %s %s ==\n", model.name, params);
System.err.println(modelStats.get(model, params));
}
}
}
}
private static List listAllFiles(List directories) {
List files = Lists.newArrayList();
if (directories != null) {
for (File dir : directories) {
for (File file : dir.listFiles()) {
if (!file.getName().startsWith(".") && !file.isHidden()) {
files.add(file);
}
}
}
}
return files;
}
private static Set> getPrerequisites(Model> model) {
Set> prereqs = Sets.newLinkedHashSet();
for (Model> prereq : model.prerequisites) {
prereqs.add(prereq);
prereqs.addAll(getPrerequisites(prereq));
}
return prereqs;
}
private static LinkedHashSet> getSortedPrerequisites(Model> model) {
Queue> todo = Queues.newArrayDeque();
Multimap, Model>> following = LinkedHashMultimap.create();
for (Model> prereq : getPrerequisites(model)) {
if (prereq.prerequisites.isEmpty()) {
todo.add(prereq);
} else {
for (Model> preprereq : prereq.prerequisites) {
following.put(preprereq, prereq);
}
}
}
LinkedHashSet> models = Sets.newLinkedHashSet();
while (!todo.isEmpty()) {
Model> next = todo.iterator().next();
todo.remove(next);
models.add(next);
for (Model> prereq : following.removeAll(next)) {
if (!following.containsKey(prereq)) {
todo.add(prereq);
}
}
}
return models;
}
private static Function> TEMPORAL_LINK_TO_SPANS = new Function>() {
@Override
public List apply(TemporalLink temporalLink) {
// order source and target indexes, left-to-right
Anchor source = temporalLink.getSource();
Anchor target = temporalLink.getTarget();
return source.getBegin() < target.getBegin()
? Lists.newArrayList(source.getBegin(), source.getEnd(), target.getBegin(), target.getEnd())
: Lists.newArrayList(target.getBegin(), target.getEnd(), source.getBegin(), source.getEnd());
}
};
private static Function TEMPORAL_LINK_TO_RELATION = new Function() {
@Override
public String apply(TemporalLink temporalLink) {
// match relation with left-to-right ordering of indexes
Anchor source = temporalLink.getSource();
Anchor target = temporalLink.getTarget();
return source.getBegin() < target.getBegin()
? temporalLink.getRelationType()
: TemporalLinkAnnotator_ImplBase.REVERSE_RELATION.get(temporalLink.getRelationType());
}
};
private static List SEQUENCE_CLASSIFIER_PARAM_SEARCH_SPACE = Lists.newArrayList(
// L2-regularized L2-loss support vector classification (dual)
new Model.Params(LibLinearStringOutcomeDataWriter.class, 1, "-c", "0.1", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 1, "-c", "0.5", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 1, "-c", "1", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 1, "-c", "5", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 1, "-c", "10", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 1, "-c", "50", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 2, "-c", "0.1", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 2, "-c", "0.5", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 2, "-c", "1", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 2, "-c", "5", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 2, "-c", "10", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 2, "-c", "50", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 3, "-c", "0.1", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 3, "-c", "0.5", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 3, "-c", "1", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 3, "-c", "5", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 3, "-c", "10", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, 3, "-c", "50", "-s", "1"));
// // default is --iterations 500 --gaussian-variance 10
// new Model.Params(MalletCRFStringOutcomeDataWriter.class),
// new Model.Params(MalletCRFStringOutcomeDataWriter.class, "--forbidden", "O,I"),
// new Model.Params(MalletCRFStringOutcomeDataWriter.class, "--iterations", "100"),
// new Model.Params(MalletCRFStringOutcomeDataWriter.class, "--iterations", "1000"),
// new Model.Params(MalletCRFStringOutcomeDataWriter.class, "--gaussian-variance", "1"),
// new Model.Params(MalletCRFStringOutcomeDataWriter.class, "--gaussian-variance", "100"));
// private static final String priorFlag = "--gaussianPriorVariance";
private static List CLASSIFIER_PARAM_SEARCH_SPACE = Lists.newArrayList(
// // default is --gaussianPriorVariance 1
// new Model.Params(MalletStringOutcomeDataWriter.class, "MaxEnt"),
// new Model.Params(MalletStringOutcomeDataWriter.class, "MaxEnt", priorFlag, "0.1"),
// new Model.Params(MalletStringOutcomeDataWriter.class, "MaxEnt", priorFlag, "10"),
// // default is [iterations cutoff] 100 5
// new Model.Params(MaxentStringOutcomeDataWriter.class),
// new Model.Params(MaxentStringOutcomeDataWriter.class, "100", "10"),
// new Model.Params(MaxentStringOutcomeDataWriter.class, "500", "5"),
// L2-regularized logistic regression (primal)
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "0.1", "-s", "0"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "0.5", "-s", "0"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "1", "-s", "0"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "5", "-s", "0"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "10", "-s", "0"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "50", "-s", "0"),
// L2-regularized L2-loss support vector classification (dual)
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "0.1", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "0.5", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "1", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "5", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "10", "-s", "1"),
new Model.Params(LibLinearStringOutcomeDataWriter.class, "-c", "50", "-s", "1"));
private static final Model