From b170e31029cb902f7935204f40aaf195b4aa0892 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Sat, 3 Aug 2024 19:19:32 -0300 Subject: [PATCH] No issue: Fix resource leaks in tests --- .../java/org/dkpro/core/eval/EvalUtil.java | 34 ++++++++++--------- .../opennlp/OpenNlpChunkerTrainerTest.java | 30 ++++++++-------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/dkpro-core-eval-asl/src/main/java/org/dkpro/core/eval/EvalUtil.java b/dkpro-core-eval-asl/src/main/java/org/dkpro/core/eval/EvalUtil.java index b33ff562ea..f9d3c6bbd6 100644 --- a/dkpro-core-eval-asl/src/main/java/org/dkpro/core/eval/EvalUtil.java +++ b/dkpro-core-eval-asl/src/main/java/org/dkpro/core/eval/EvalUtil.java @@ -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; @@ -47,21 +47,21 @@ public class EvalUtil { public static List> loadSamples( CollectionReaderDescription aReader, Class aType) - throws UIMAException, IOException + throws UIMAException, IOException { return loadSamples(aReader, aType, null); } - + public static List> loadSamples( CollectionReaderDescription aReader, Class aType, Function aLabelFunction) - throws UIMAException, IOException + throws UIMAException, IOException { return loadSamples(iteratePipeline(aReader), aType, aLabelFunction); } - - public static List> loadSamples( - JCasIterable aIterable, Class aType, Function aLabelFunction) - throws UIMAException, IOException + + public static List> loadSamples(JCasIterable aIterable, + Class aType, Function aLabelFunction) + throws UIMAException, IOException { List> samples = new ArrayList<>(); for (JCas jcas : aIterable) { @@ -74,28 +74,30 @@ public static List> loadSamples( return samples; } - + public static Result dumpResults(File targetFolder, Collection aExpected, Collection 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; } } diff --git a/dkpro-core-opennlp-asl/src/test/java/org/dkpro/core/opennlp/OpenNlpChunkerTrainerTest.java b/dkpro-core-opennlp-asl/src/test/java/org/dkpro/core/opennlp/OpenNlpChunkerTrainerTest.java index 7374ba48b5..c56b38968a 100644 --- a/dkpro-core-opennlp-asl/src/test/java/org/dkpro/core/opennlp/OpenNlpChunkerTrainerTest.java +++ b/dkpro-core-opennlp-asl/src/test/java/org/dkpro/core/opennlp/OpenNlpChunkerTrainerTest.java @@ -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> actual = EvalUtil.loadSamples(iteratePipeline(testReader, ner),