Skip to content

Commit

Permalink
No issue: Fix resource leaks in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reckart committed Aug 3, 2024
1 parent dc04ae6 commit b170e31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
34 changes: 18 additions & 16 deletions dkpro-core-eval-asl/src/main/java/org/dkpro/core/eval/EvalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
*/
package org.dkpro.core.eval;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.uima.fit.pipeline.SimplePipeline.iteratePipeline;
import static org.apache.uima.fit.util.JCasUtil.select;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
Expand All @@ -47,21 +47,21 @@ public class EvalUtil
{
public static <T extends Annotation> List<Span<String>> loadSamples(
CollectionReaderDescription aReader, Class<T> aType)
throws UIMAException, IOException
throws UIMAException, IOException
{
return loadSamples(aReader, aType, null);
}

public static <T extends Annotation> List<Span<String>> loadSamples(
CollectionReaderDescription aReader, Class<T> aType, Function<T, String> aLabelFunction)
throws UIMAException, IOException
throws UIMAException, IOException
{
return loadSamples(iteratePipeline(aReader), aType, aLabelFunction);
}
public static <T extends Annotation> List<Span<String>> loadSamples(
JCasIterable aIterable, Class<T> aType, Function<T, String> aLabelFunction)
throws UIMAException, IOException

public static <T extends Annotation> List<Span<String>> loadSamples(JCasIterable aIterable,
Class<T> aType, Function<T, String> aLabelFunction)
throws UIMAException, IOException
{
List<Span<String>> samples = new ArrayList<>();
for (JCas jcas : aIterable) {
Expand All @@ -74,28 +74,30 @@ public static <T extends Annotation> List<Span<String>> loadSamples(

return samples;
}

public static Result dumpResults(File targetFolder, Collection<? extends Object> aExpected,
Collection<? extends Object> aActual)
throws UnsupportedEncodingException, FileNotFoundException
throws UnsupportedEncodingException, IOException
{
System.out.println("Calculating F-measure");
FMeasure fmeasure = new FMeasure();
fmeasure.process(aExpected, aActual);

System.out.printf("F-score %f%n", fmeasure.getFMeasure());
System.out.printf("Precision %f%n", fmeasure.getPrecision());
System.out.printf("Recall %f%n", fmeasure.getRecall());

Result results = new Result();
results.setFscore(fmeasure.getFMeasure());
results.setPrecision(fmeasure.getPrecision());
results.setRecall(fmeasure.getRecall());

Yaml yaml = new Yaml();
yaml.dump(results, new OutputStreamWriter(
new FileOutputStream(new File(targetFolder, "results.yaml")), "UTF-8"));

try (var os = new OutputStreamWriter(
new FileOutputStream(new File(targetFolder, "results.yaml")), UTF_8)) {
Yaml yaml = new Yaml();
yaml.dump(results, os);
}

return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,33 @@ public void test(@TempDir File targetFolder)

// Train model
System.out.println("Training model from training data");
CollectionReaderDescription trainReader = createReaderDescription(
Conll2000Reader.class,
Conll2000Reader.PARAM_PATTERNS, split.getTrainingFiles(),
CollectionReaderDescription trainReader = createReaderDescription( //
Conll2000Reader.class, //
Conll2000Reader.PARAM_PATTERNS, split.getTrainingFiles(), //
Conll2000Reader.PARAM_LANGUAGE, ds.getLanguage());

AnalysisEngineDescription trainer = createEngineDescription(
OpenNlpChunkerTrainer.class,
OpenNlpChunkerTrainer.PARAM_TARGET_LOCATION, new File(targetFolder, "model.bin"),
AnalysisEngineDescription trainer = createEngineDescription( //
OpenNlpChunkerTrainer.class, //
OpenNlpChunkerTrainer.PARAM_TARGET_LOCATION, new File(targetFolder, "model.bin"), //
// OpenNlpChunkerTrainer.PARAM_ALGORITHM, "PERCEPTRON",
// OpenNlpChunkerTrainer.PARAM_CUTOFF, 0,
OpenNlpChunkerTrainer.PARAM_NUM_THREADS, 2,
OpenNlpChunkerTrainer.PARAM_LANGUAGE, ds.getLanguage(),
OpenNlpChunkerTrainer.PARAM_NUM_THREADS, 2, //
OpenNlpChunkerTrainer.PARAM_LANGUAGE, ds.getLanguage(), //
OpenNlpChunkerTrainer.PARAM_ITERATIONS, 10);

SimplePipeline.runPipeline(trainReader, trainer);

// Apply model and collect labels
System.out.println("Applying model to test data");
CollectionReaderDescription testReader = createReaderDescription(
Conll2000Reader.class,
Conll2000Reader.PARAM_PATTERNS, split.getTestFiles(),
Conll2000Reader.PARAM_READ_CHUNK, false,
CollectionReaderDescription testReader = createReaderDescription( //
Conll2000Reader.class, //
Conll2000Reader.PARAM_PATTERNS, split.getTestFiles(), //
Conll2000Reader.PARAM_READ_CHUNK, false, //
Conll2000Reader.PARAM_LANGUAGE, ds.getLanguage());

AnalysisEngineDescription ner = createEngineDescription(
OpenNlpChunker.class,
OpenNlpChunker.PARAM_PRINT_TAGSET, true,
AnalysisEngineDescription ner = createEngineDescription( //
OpenNlpChunker.class, //
OpenNlpChunker.PARAM_PRINT_TAGSET, true, //
OpenNlpChunker.PARAM_MODEL_LOCATION, new File(targetFolder, "model.bin"));

List<Span<String>> actual = EvalUtil.loadSamples(iteratePipeline(testReader, ner),
Expand Down

0 comments on commit b170e31

Please sign in to comment.