Search in sources :

Example 1 with EvaluationResult

use of de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult in project inception by inception-project.

the class OpenNlpNerRecommender method evaluate.

@Override
public EvaluationResult evaluate(List<CAS> aCasses, DataSplitter aDataSplitter) throws RecommendationException {
    List<NameSample> data = extractNameSamples(aCasses);
    List<NameSample> trainingSet = new ArrayList<>();
    List<NameSample> testSet = new ArrayList<>();
    for (NameSample nameSample : data) {
        switch(aDataSplitter.getTargetSet(nameSample)) {
            case TRAIN:
                trainingSet.add(nameSample);
                break;
            case TEST:
                testSet.add(nameSample);
                break;
            default:
                // Do nothing
                break;
        }
    }
    int testSetSize = testSet.size();
    int trainingSetSize = trainingSet.size();
    double overallTrainingSize = data.size() - testSetSize;
    double trainRatio = (overallTrainingSize > 0) ? trainingSetSize / overallTrainingSize : 0.0;
    final int minTrainingSetSize = 2;
    final int minTestSetSize = 2;
    if (trainingSetSize < minTrainingSetSize || testSetSize < minTestSetSize) {
        if ((getRecommender().getThreshold() <= 0.0d)) {
            return new EvaluationResult(DATAPOINT_UNIT.getSimpleName(), SAMPLE_UNIT.getSimpleName());
        }
        String info = String.format("Not enough evaluation data: training set [%s] sentences, test set [%s] of total [%s]", trainingSetSize, testSetSize, data.size());
        LOG.info(info);
        EvaluationResult result = new EvaluationResult(DATAPOINT_UNIT.getSimpleName(), SAMPLE_UNIT.getSimpleName(), trainingSetSize, testSetSize, trainRatio);
        result.setEvaluationSkipped(true);
        result.setErrorMsg(info);
        return result;
    }
    LOG.info("Training on [{}] sentences, predicting on [{}] of total [{}]", trainingSet.size(), testSet.size(), data.size());
    // Train model
    TokenNameFinderModel model = train(trainingSet, traits.getParameters());
    NameFinderME nameFinder = new NameFinderME(model);
    // Evaluate
    List<LabelPair> labelPairs = new ArrayList<>();
    for (NameSample sample : testSet) {
        // clear adaptive data from feature generators if necessary
        if (sample.isClearAdaptiveDataSet()) {
            nameFinder.clearAdaptiveData();
        }
        // Span contains one NE, Array of them all in one sentence
        String[] sentence = sample.getSentence();
        Span[] predictedNames = nameFinder.find(sentence);
        Span[] goldNames = sample.getNames();
        labelPairs.addAll(determineLabelsForASentence(sentence, predictedNames, goldNames));
    }
    return labelPairs.stream().collect(toEvaluationResult(DATAPOINT_UNIT.getSimpleName(), SAMPLE_UNIT.getSimpleName(), trainingSetSize, testSetSize, trainRatio, NO_NE_TAG));
}
Also used : TokenNameFinderModel(opennlp.tools.namefind.TokenNameFinderModel) NameSample(opennlp.tools.namefind.NameSample) ArrayList(java.util.ArrayList) Span(opennlp.tools.util.Span) EvaluationResult(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult) EvaluationResult.toEvaluationResult(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult.toEvaluationResult) LabelPair(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.LabelPair) NameFinderME(opennlp.tools.namefind.NameFinderME)

Example 2 with EvaluationResult

use of de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult in project inception by inception-project.

the class OpenNlpNerRecommenderTest method thatEvaluationWorks.

@Test
public void thatEvaluationWorks() throws Exception {
    DataSplitter splitStrategy = new PercentageBasedSplitter(0.8, 10);
    OpenNlpNerRecommender sut = new OpenNlpNerRecommender(recommender, traits);
    List<CAS> casList = loadDevelopmentData();
    EvaluationResult result = sut.evaluate(casList, splitStrategy);
    double fscore = result.computeF1Score();
    double accuracy = result.computeAccuracyScore();
    double precision = result.computePrecisionScore();
    double recall = result.computeRecallScore();
    System.out.printf("F1-Score: %f%n", fscore);
    System.out.printf("Accuracy: %f%n", accuracy);
    System.out.printf("Precision: %f%n", precision);
    System.out.printf("Recall: %f%n", recall);
    assertThat(fscore).isStrictlyBetween(0.0, 1.0);
    assertThat(precision).isStrictlyBetween(0.0, 1.0);
    assertThat(recall).isStrictlyBetween(0.0, 1.0);
    assertThat(accuracy).isStrictlyBetween(0.0, 1.0);
}
Also used : DataSplitter(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.DataSplitter) PercentageBasedSplitter(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.PercentageBasedSplitter) CAS(org.apache.uima.cas.CAS) EvaluationResult(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult) Test(org.junit.jupiter.api.Test)

Example 3 with EvaluationResult

use of de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult in project inception by inception-project.

the class OpenNlpPosRecommenderTest method thatEvaluationWorks.

@Test
public void thatEvaluationWorks() throws Exception {
    DataSplitter splitStrategy = new PercentageBasedSplitter(0.8, 10);
    OpenNlpPosRecommender sut = new OpenNlpPosRecommender(recommender, traits);
    List<CAS> casList = loadDevelopmentData();
    EvaluationResult result = sut.evaluate(casList, splitStrategy);
    double fscore = result.computeF1Score();
    double accuracy = result.computeAccuracyScore();
    double precision = result.computePrecisionScore();
    double recall = result.computeRecallScore();
    System.out.printf("F1-Score: %f%n", fscore);
    System.out.printf("Accuracy: %f%n", accuracy);
    System.out.printf("Precision: %f%n", precision);
    System.out.printf("Recall: %f%n", recall);
    assertThat(fscore).isStrictlyBetween(0.0, 1.0);
    assertThat(precision).isStrictlyBetween(0.0, 1.0);
    assertThat(recall).isStrictlyBetween(0.0, 1.0);
    assertThat(accuracy).isStrictlyBetween(0.0, 1.0);
}
Also used : DataSplitter(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.DataSplitter) PercentageBasedSplitter(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.PercentageBasedSplitter) CAS(org.apache.uima.cas.CAS) EvaluationResult(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult) Test(org.junit.jupiter.api.Test)

Example 4 with EvaluationResult

use of de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult in project inception by inception-project.

the class NamedEntityLinker method evaluate.

@Override
public EvaluationResult evaluate(List<CAS> aCasses, DataSplitter aDataSplitter) {
    EvaluationResult result = new EvaluationResult();
    result.setEvaluationSkipped(true);
    result.setErrorMsg("NamedEntityLinker does not support evaluation.");
    return result;
}
Also used : EvaluationResult(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult)

Example 5 with EvaluationResult

use of de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult in project inception by inception-project.

the class DataMajorityNerRecommender method evaluate.

// end::predict2[]
// tag::evaluate[]
@Override
public EvaluationResult evaluate(List<CAS> aCasses, DataSplitter aDataSplitter) throws RecommendationException {
    List<Annotation> data = extractAnnotations(aCasses);
    List<Annotation> trainingData = new ArrayList<>();
    List<Annotation> testData = new ArrayList<>();
    for (Annotation ann : data) {
        switch(aDataSplitter.getTargetSet(ann)) {
            case TRAIN:
                trainingData.add(ann);
                break;
            case TEST:
                testData.add(ann);
                break;
            case IGNORE:
                break;
        }
    }
    int trainingSetSize = trainingData.size();
    int testSetSize = testData.size();
    double overallTrainingSize = data.size() - testSetSize;
    double trainRatio = (overallTrainingSize > 0) ? trainingSetSize / overallTrainingSize : 0.0;
    if (trainingData.size() < 1 || testData.size() < 1) {
        log.info("Not enough data to evaluate, skipping!");
        EvaluationResult result = new EvaluationResult(DATAPOINT_UNIT.getSimpleName(), getRecommender().getLayer().getUiName(), trainingSetSize, testSetSize, trainRatio);
        result.setEvaluationSkipped(true);
        return result;
    }
    DataMajorityModel model = trainModel(trainingData);
    // evaluation: collect predicted and gold labels for evaluation
    EvaluationResult result = testData.stream().map(anno -> new LabelPair(anno.label, model.majorityLabel)).collect(toEvaluationResult(DATAPOINT_UNIT.getSimpleName(), getRecommender().getLayer().getUiName(), trainingSetSize, testSetSize, trainRatio));
    return result;
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) TrainingCapability(de.tudarmstadt.ukp.inception.recommendation.api.recommender.TrainingCapability) EvaluationResult(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult) LoggerFactory(org.slf4j.LoggerFactory) CAS(org.apache.uima.cas.CAS) Feature(org.apache.uima.cas.Feature) HashMap(java.util.HashMap) RecommendationEngine(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine) Range(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Range) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) ArrayList(java.util.ArrayList) Type(org.apache.uima.cas.Type) WebAnnoCasUtil.selectOverlapping(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectOverlapping) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) EvaluationResult.toEvaluationResult(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult.toEvaluationResult) Map(java.util.Map) RecommenderContext(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext) Logger(org.slf4j.Logger) Collection(java.util.Collection) TRAINING_REQUIRED(de.tudarmstadt.ukp.inception.recommendation.api.recommender.TrainingCapability.TRAINING_REQUIRED) Recommender(de.tudarmstadt.ukp.inception.recommendation.api.model.Recommender) DataSplitter(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.DataSplitter) Objects(java.util.Objects) CasUtil(org.apache.uima.fit.util.CasUtil) RecommendationException(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException) List(java.util.List) LabelPair(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.LabelPair) Key(de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext.Key) ArrayList(java.util.ArrayList) LabelPair(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.LabelPair) EvaluationResult(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult) EvaluationResult.toEvaluationResult(de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult.toEvaluationResult)

Aggregations

EvaluationResult (de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult)33 Test (org.junit.jupiter.api.Test)16 EvaluationResult.toEvaluationResult (de.tudarmstadt.ukp.inception.recommendation.api.evaluation.EvaluationResult.toEvaluationResult)14 CAS (org.apache.uima.cas.CAS)12 DataSplitter (de.tudarmstadt.ukp.inception.recommendation.api.evaluation.DataSplitter)11 ArrayList (java.util.ArrayList)11 PercentageBasedSplitter (de.tudarmstadt.ukp.inception.recommendation.api.evaluation.PercentageBasedSplitter)10 LabelPair (de.tudarmstadt.ukp.inception.recommendation.api.evaluation.LabelPair)8 List (java.util.List)6 Recommender (de.tudarmstadt.ukp.inception.recommendation.api.model.Recommender)5 RecommendationEngine (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationEngine)5 RecommendationException (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommendationException)5 Range (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Range)3 WebAnnoCasUtil.selectOverlapping (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectOverlapping)3 RecommenderContext (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext)3 Key (de.tudarmstadt.ukp.inception.recommendation.api.recommender.RecommenderContext.Key)3 IOException (java.io.IOException)3 Collection (java.util.Collection)3 Map (java.util.Map)3 Objects (java.util.Objects)3