This is an automated email from the ASF dual-hosted git repository. mawiesne pushed a commit to branch OPENNLP-1483_Apply_language_migration_aids in repository https://gitbox.apache.org/repos/asf/opennlp.git
commit 68f630c497ec42296adc810a9f4e0c13b74a753b Author: Martin Wiesner <[email protected]> AuthorDate: Fri Mar 24 23:52:18 2023 +0100 OPENNLP-1483 Apply language migration aids - modernizes existing classes and tests towards Java 8+ - adds `final` modifier where applicable - adjusts broken or non-compliant JavaDoc --- .../opennlp/tools/chunker/ChunkerEvaluator.java | 2 +- .../cmdline/chunker/ChunkerEvaluatorTool.java | 2 +- .../langdetect/LanguageDetectorEvaluatorTool.java | 2 +- .../namefind/TokenNameFinderEvaluatorTool.java | 4 +-- .../opennlp/tools/formats/ad/ADSentenceStream.java | 2 +- .../formats/brat/BratNameSampleStreamFactory.java | 1 + .../tools/formats/brat/SegmenterObjectStream.java | 2 +- .../opennlp/tools/formats/conllu/ConlluStream.java | 4 +-- .../tools/langdetect/LanguageDetectorFactory.java | 2 +- .../opennlp/tools/ml/maxent/io/GISModelWriter.java | 10 ++++---- .../opennlp/tools/ml/maxent/io/QNModelWriter.java | 15 +++++------ .../opennlp/tools/ml/model/TwoPassDataIndexer.java | 2 +- .../tools/ml/naivebayes/NaiveBayesModelWriter.java | 16 ++++++------ .../tools/ml/perceptron/PerceptronModelWriter.java | 6 ++--- .../opennlp/tools/namefind/DocumentNameFinder.java | 2 +- .../java/opennlp/tools/ngram/NGramCharModel.java | 4 +-- .../java/opennlp/tools/ngram/NGramGenerator.java | 15 +++++------ .../main/java/opennlp/tools/ngram/NGramModel.java | 13 +++++----- .../main/java/opennlp/tools/ngram/NGramUtils.java | 6 ++--- .../java/opennlp/tools/parser/Constituent.java | 2 +- .../java/opennlp/tools/parser/ParserModel.java | 3 +-- .../opennlp/tools/parser/lang/en/HeadRules.java | 12 ++++----- .../parser/lang/es/AncoraSpanishHeadRules.java | 20 +++++++-------- .../tools/stemmer/snowball/SnowballProgram.java | 13 +++------- .../tools/stemmer/snowball/greekStemmer.java | 2 +- .../tools/tokenize/lang/en/TokenSampleStream.java | 4 +-- .../java/opennlp/tools/util/ObjectStreamUtils.java | 8 +++--- .../java/opennlp/tools/util/eval/FMeasure.java | 2 +- .../util/featuregen/CustomFeatureGenerator.java | 2 +- .../tools/util/featuregen/GeneratorFactory.java | 10 ++++---- .../featuregen/POSTaggerNameFeatureGenerator.java | 2 +- .../util/featuregen/WindowFeatureGenerator.java | 2 +- .../opennlp/tools/util/model/ArtifactProvider.java | 2 +- .../java/opennlp/tools/util/model/BaseModel.java | 12 ++++----- .../tools/chunker/ChunkerEvaluatorTest.java | 10 +++----- .../java/opennlp/tools/chunker/ChunkerMEIT.java | 6 ++--- .../java/opennlp/tools/chunker/ChunkerMETest.java | 6 ++--- .../tools/chunker/DummyChunkSampleStream.java | 2 +- .../java/opennlp/tools/chunker/DummyChunker.java | 2 +- .../tokenizer/TokenizerTrainerToolTest.java | 4 +-- .../opennlp/tools/dictionary/DictionaryTest.java | 6 ++--- .../opennlp/tools/eval/Conll02NameFinderEval.java | 4 +-- .../leipzig/LeipzigLanguageSampleStreamTest.java | 2 +- .../opennlp/tools/langdetect/DummyFactory.java | 5 +--- .../languagemodel/LanguageModelTestUtils.java | 2 +- .../tools/lemmatizer/DummyLemmaSampleStream.java | 2 +- .../tools/lemmatizer/LemmatizerEvaluatorTest.java | 5 ++-- .../opennlp/tools/lemmatizer/LemmatizerMETest.java | 6 ++--- .../test/java/opennlp/tools/ml/BeamSearchTest.java | 10 ++++---- .../opennlp/tools/ml/maxent/GISIndexingTest.java | 4 +-- .../tools/ml/maxent/URLInputStreamFactory.java | 2 +- .../ml/maxent/quasinewton/LineSearchTest.java | 4 +-- .../ml/maxent/quasinewton/QNMinimizerTest.java | 18 ++++++++----- .../tools/ml/model/SimpleEventStreamBuilder.java | 2 +- .../opennlp/tools/namefind/BilouCodecTest.java | 4 +-- .../BilouNameFinderSequenceValidatorTest.java | 18 ++++++------- .../java/opennlp/tools/namefind/BioCodecTest.java | 2 +- .../DictionaryNameFinderEvaluatorTest.java | 22 +++++----------- .../tools/namefind/DictionaryNameFinderTest.java | 2 +- .../namefind/NameFinderSequenceValidatorTest.java | 12 ++++----- .../tools/namefind/NameSampleDataStreamTest.java | 8 +++--- .../opennlp/tools/namefind/NameSampleTest.java | 4 +-- .../tools/namefind/RegexNameFinderFactoryTest.java | 2 +- .../namefind/TokenNameFinderEvaluatorTest.java | 2 +- .../java/opennlp/tools/parser/ParserTestUtil.java | 4 +-- .../opennlp/tools/postag/POSDictionaryTest.java | 18 +++++-------- .../opennlp/tools/postag/POSEvaluatorTest.java | 8 +++--- .../sentdetect/DefaultSDContextGeneratorTest.java | 2 +- .../sentdetect/DummySentenceDetectorFactory.java | 2 +- .../tools/tokenize/DetokenizerEvaluatorTest.java | 8 +++--- .../tools/tokenize/DummyTokenizerFactory.java | 2 +- .../tools/tokenize/SimpleTokenizerTest.java | 2 +- .../tools/tokenize/TokenizerEvaluatorTest.java | 8 +++--- .../tools/util/AbstractEventStreamTest.java | 2 +- .../src/test/java/opennlp/tools/util/SpanTest.java | 30 +++++++++++----------- .../java/opennlp/tools/util/eval/FMeasureTest.java | 16 ++++++------ .../featuregen/CachedFeatureGeneratorTest.java | 2 +- .../featuregen/WindowFeatureGeneratorTest.java | 2 +- 78 files changed, 230 insertions(+), 260 deletions(-) diff --git a/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkerEvaluator.java b/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkerEvaluator.java index beca5bb4..3b59de17 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkerEvaluator.java +++ b/opennlp-tools/src/main/java/opennlp/tools/chunker/ChunkerEvaluator.java @@ -51,7 +51,7 @@ public class ChunkerEvaluator extends Evaluator<ChunkSample> { /** * Evaluates the given reference {@link ChunkSample} object. - * + * <p> * This is done by finding the phrases with the {@link Chunker} in the sentence from the reference * {@link ChunkSample}. The found phrases are then used to calculate and update the scores. * diff --git a/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerEvaluatorTool.java b/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerEvaluatorTool.java index bd6210ab..5fdcb218 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerEvaluatorTool.java +++ b/opennlp-tools/src/main/java/opennlp/tools/cmdline/chunker/ChunkerEvaluatorTool.java @@ -83,7 +83,7 @@ public final class ChunkerEvaluatorTool final PerformanceMonitor monitor = new PerformanceMonitor("sent"); - try (ObjectStream<ChunkSample> measuredSampleStream = new ObjectStream<ChunkSample>() { + try (ObjectStream<ChunkSample> measuredSampleStream = new ObjectStream<>() { public ChunkSample read() throws IOException { monitor.incrementCounter(); diff --git a/opennlp-tools/src/main/java/opennlp/tools/cmdline/langdetect/LanguageDetectorEvaluatorTool.java b/opennlp-tools/src/main/java/opennlp/tools/cmdline/langdetect/LanguageDetectorEvaluatorTool.java index ef36f481..bacf0100 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/cmdline/langdetect/LanguageDetectorEvaluatorTool.java +++ b/opennlp-tools/src/main/java/opennlp/tools/cmdline/langdetect/LanguageDetectorEvaluatorTool.java @@ -100,7 +100,7 @@ public final class LanguageDetectorEvaluatorTool extends final PerformanceMonitor monitor = new PerformanceMonitor("doc"); - try (ObjectStream<LanguageSample> measuredSampleStream = new ObjectStream<LanguageSample>() { + try (ObjectStream<LanguageSample> measuredSampleStream = new ObjectStream<>() { @Override public LanguageSample read() throws IOException { diff --git a/opennlp-tools/src/main/java/opennlp/tools/cmdline/namefind/TokenNameFinderEvaluatorTool.java b/opennlp-tools/src/main/java/opennlp/tools/cmdline/namefind/TokenNameFinderEvaluatorTool.java index f333d942..889fa34c 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/cmdline/namefind/TokenNameFinderEvaluatorTool.java +++ b/opennlp-tools/src/main/java/opennlp/tools/cmdline/namefind/TokenNameFinderEvaluatorTool.java @@ -81,7 +81,7 @@ public final class TokenNameFinderEvaluatorTool TokenNameFinderModel model = new TokenNameFinderModelLoader().load(params.getModel()); - List<EvaluationMonitor<NameSample>> listeners = new LinkedList<EvaluationMonitor<NameSample>>(); + List<EvaluationMonitor<NameSample>> listeners = new LinkedList<>(); if (params.getMisclassified()) { listeners.add(new NameEvaluationErrorListener()); } @@ -120,7 +120,7 @@ public final class TokenNameFinderEvaluatorTool final PerformanceMonitor monitor = new PerformanceMonitor("sent"); - try (ObjectStream<NameSample> measuredSampleStream = new ObjectStream<NameSample>() { + try (ObjectStream<NameSample> measuredSampleStream = new ObjectStream<>() { @Override public NameSample read() throws IOException { diff --git a/opennlp-tools/src/main/java/opennlp/tools/formats/ad/ADSentenceStream.java b/opennlp-tools/src/main/java/opennlp/tools/formats/ad/ADSentenceStream.java index 14bf7177..7b7ff44d 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/formats/ad/ADSentenceStream.java +++ b/opennlp-tools/src/main/java/opennlp/tools/formats/ad/ADSentenceStream.java @@ -392,7 +392,7 @@ public class ADSentenceStream extends FilterObjectStream<String, ADSentenceStrea /** Represents the AD node */ public static class Node extends TreeElement { - private List<TreeElement> elems = new ArrayList<>(); + private final List<TreeElement> elems = new ArrayList<>(); public void addElement(TreeElement element) { elems.add(element); diff --git a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratNameSampleStreamFactory.java b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratNameSampleStreamFactory.java index fd4582bf..d5e8793e 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratNameSampleStreamFactory.java +++ b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/BratNameSampleStreamFactory.java @@ -92,6 +92,7 @@ public class BratNameSampleStreamFactory return true; } + @Override public ObjectStream<NameSample> create(String[] args) { Parameters params = ArgumentParser.parse(args, Parameters.class); diff --git a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/SegmenterObjectStream.java b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/SegmenterObjectStream.java index 736a9e48..82c4e4aa 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/formats/brat/SegmenterObjectStream.java +++ b/opennlp-tools/src/main/java/opennlp/tools/formats/brat/SegmenterObjectStream.java @@ -27,7 +27,7 @@ import opennlp.tools.util.ObjectStream; public abstract class SegmenterObjectStream<S, T> extends FilterObjectStream<S, T> { - private Iterator<T> sampleIt = Collections.<T>emptySet().iterator(); + private Iterator<T> sampleIt = Collections.emptyIterator(); public SegmenterObjectStream(ObjectStream<S> in) { super(in); diff --git a/opennlp-tools/src/main/java/opennlp/tools/formats/conllu/ConlluStream.java b/opennlp-tools/src/main/java/opennlp/tools/formats/conllu/ConlluStream.java index 67913f32..e4a9d8fa 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/formats/conllu/ConlluStream.java +++ b/opennlp-tools/src/main/java/opennlp/tools/formats/conllu/ConlluStream.java @@ -202,7 +202,7 @@ public class ConlluStream implements ObjectStream<ConlluSentence> { String form = contraction.getForm(); String lemma = expandedParts.stream() .filter(p -> !"_".equals(p.getLemma())) - .map(p -> p.getLemma()) + .map(ConlluWordLine::getLemma) .collect(Collectors.joining("+")); String uPosTag = expandedParts.stream() @@ -217,7 +217,7 @@ public class ConlluStream implements ObjectStream<ConlluSentence> { String feats = expandedParts.stream() .filter(p -> !"_".equals(p.getFeats())) - .map(p -> p.getFeats()) + .map(ConlluWordLine::getFeats) .collect(Collectors.joining("+")); String head = contraction.getHead(); diff --git a/opennlp-tools/src/main/java/opennlp/tools/langdetect/LanguageDetectorFactory.java b/opennlp-tools/src/main/java/opennlp/tools/langdetect/LanguageDetectorFactory.java index 0c947120..2893b634 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/langdetect/LanguageDetectorFactory.java +++ b/opennlp-tools/src/main/java/opennlp/tools/langdetect/LanguageDetectorFactory.java @@ -29,7 +29,7 @@ import opennlp.tools.util.normalizer.UrlCharSequenceNormalizer; /** * Default factory used by {@link LanguageDetector}. - * + * <p> * Extend this class to change the Language Detector behaviour, * such as the {@link LanguageDetectorContextGenerator}. * The default {@link DefaultLanguageDetectorContextGenerator} will use char n-grams of diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/io/GISModelWriter.java b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/io/GISModelWriter.java index d1279da6..78ada10e 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/io/GISModelWriter.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/io/GISModelWriter.java @@ -168,14 +168,14 @@ public abstract class GISModelWriter extends AbstractModelWriter { if (sorted.length > 0) { ComparablePredicate cp = sorted[0]; List<ComparablePredicate> newGroup = new ArrayList<>(); - for (int i = 0; i < sorted.length; i++) { - if (cp.compareTo(sorted[i]) == 0) { - newGroup.add(sorted[i]); + for (ComparablePredicate comparablePredicate : sorted) { + if (cp.compareTo(comparablePredicate) == 0) { + newGroup.add(comparablePredicate); } else { - cp = sorted[i]; + cp = comparablePredicate; outcomePatterns.add(newGroup); newGroup = new ArrayList<>(); - newGroup.add(sorted[i]); + newGroup.add(comparablePredicate); } } outcomePatterns.add(newGroup); diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/io/QNModelWriter.java b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/io/QNModelWriter.java index a4d87bea..017b2c50 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/io/QNModelWriter.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/io/QNModelWriter.java @@ -56,8 +56,7 @@ public abstract class QNModelWriter extends GISModelWriter { // the mapping from outcomes to their integer indexes writeInt(OUTCOME_LABELS.length); - for (int i = 0; i < OUTCOME_LABELS.length; i++) - writeUTF(OUTCOME_LABELS[i]); + for (String outcomeLabel : OUTCOME_LABELS) writeUTF(outcomeLabel); // the mapping from predicates to the outcomes they contributed to. // The sorting is done so that we actually can write this out more @@ -67,21 +66,19 @@ public abstract class QNModelWriter extends GISModelWriter { writeInt(compressed.size()); - for (int i = 0; i < compressed.size(); i++) { - List<ComparablePredicate> a = compressed.get(i); + for (List<ComparablePredicate> a : compressed) { writeUTF(a.size() + a.get(0).toString()); } // the mapping from predicate names to their integer indexes writeInt(PARAMS.length); - for (int i = 0; i < sorted.length; i++) - writeUTF(sorted[i].name); + for (ComparablePredicate predicate : sorted) writeUTF(predicate.name); // write out the parameters - for (int i = 0; i < sorted.length; i++) - for (int j = 0; j < sorted[i].params.length; j++) - writeDouble(sorted[i].params[j]); + for (ComparablePredicate comparablePredicate : sorted) + for (int j = 0; j < comparablePredicate.params.length; j++) + writeDouble(comparablePredicate.params[j]); close(); } diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java index 16d82b9c..dd67dc21 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java @@ -112,7 +112,7 @@ public class TwoPassDataIndexer extends AbstractDataIndexer { * predicates associated with each event are counted and any which * occur at least <tt>cutoff</tt> times are added to the * <tt>predicatesInOut</tt> map along with a unique integer index. - * + * <p> * Protocol: * 1 - (utf string) - Event outcome * 2 - (int) - Event context array length diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModelWriter.java b/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModelWriter.java index 401d4bd5..eb329029 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModelWriter.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModelWriter.java @@ -114,14 +114,14 @@ public abstract class NaiveBayesModelWriter extends AbstractModelWriter { if (sorted.length > 0) { ComparablePredicate cp = sorted[0]; List<ComparablePredicate> newGroup = new ArrayList<>(); - for (int i = 0; i < sorted.length; i++) { - if (cp.compareTo(sorted[i]) == 0) { - newGroup.add(sorted[i]); + for (ComparablePredicate comparablePredicate : sorted) { + if (cp.compareTo(comparablePredicate) == 0) { + newGroup.add(comparablePredicate); } else { - cp = sorted[i]; + cp = comparablePredicate; outcomePatterns.add(newGroup); newGroup = new ArrayList<>(); - newGroup.add(sorted[i]); + newGroup.add(comparablePredicate); } } outcomePatterns.add(newGroup); @@ -198,9 +198,9 @@ public abstract class NaiveBayesModelWriter extends AbstractModelWriter { } // write out the parameters - for (int i = 0; i < sorted.length; i++) - for (int j = 0; j < sorted[i].params.length; j++) - writeDouble(sorted[i].params[j]); + for (ComparablePredicate comparablePredicate : sorted) + for (int j = 0; j < comparablePredicate.params.length; j++) + writeDouble(comparablePredicate.params[j]); close(); } diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelWriter.java b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelWriter.java index 96937423..87950c01 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelWriter.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelWriter.java @@ -189,9 +189,9 @@ public abstract class PerceptronModelWriter extends AbstractModelWriter { } // write out the parameters - for (int i = 0; i < sorted.length; i++) - for (int j = 0; j < sorted[i].params.length; j++) - writeDouble(sorted[i].params[j]); + for (ComparablePredicate comparablePredicate : sorted) + for (int j = 0; j < comparablePredicate.params.length; j++) + writeDouble(comparablePredicate.params[j]); close(); } diff --git a/opennlp-tools/src/main/java/opennlp/tools/namefind/DocumentNameFinder.java b/opennlp-tools/src/main/java/opennlp/tools/namefind/DocumentNameFinder.java index 0f09a986..9143412a 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/namefind/DocumentNameFinder.java +++ b/opennlp-tools/src/main/java/opennlp/tools/namefind/DocumentNameFinder.java @@ -22,7 +22,7 @@ import opennlp.tools.util.Span; /** * Interface for processing an entire document allowing a {@link TokenNameFinder} to use context * from the entire document. - * + * <p> * <strong>EXPERIMENTAL</strong>: * This interface has been added as part of a work in progress and might change without notice. */ diff --git a/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramCharModel.java b/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramCharModel.java index 494b9000..4b6de30f 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramCharModel.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramCharModel.java @@ -27,13 +27,13 @@ import opennlp.tools.util.StringUtil; /** * The {@link NGramCharModel} can be used to create character ngrams. * - * @see {@link NGramModel} + * @see NGramModel */ public class NGramCharModel implements Iterable<String> { protected static final String COUNT = "count"; - private Map<String, Integer> mNGrams = new HashMap<>(); + private final Map<String, Integer> mNGrams = new HashMap<>(); /** * Initializes an empty instance. diff --git a/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramGenerator.java b/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramGenerator.java index d090f293..9e59016b 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramGenerator.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramGenerator.java @@ -21,22 +21,21 @@ import java.util.ArrayList; import java.util.List; /** - * Generates an nGram, with optional separator, and returns the grams as a list + * Generates an nGram, via an optional separator, and returns the grams as a list * of strings */ public class NGramGenerator { - /** - * Creates an ngram separated - * by the separator param value i.e. a,b,c,d with n = 3 and separator = "-" - * would return a-b-c,b-c-d + * Creates nGrams separated + * by the separator param value i.e. {@code a,b,c,d} with {@code n = 3} and a + * separator as {@code "-"} would return {@code a-b-c,b-c-d}. * * @param input the input tokens the output ngrams will be derived from * @param n the number of tokens as the sliding window * @param separator each string in each gram will be separated by this value if desired. * Pass in empty string if no separator is desired - * @return + * @return The generated list of resulting nGrams. */ public static List<String> generate(List<String> input, int n, String separator) { @@ -57,12 +56,12 @@ public class NGramGenerator { } /** - *Generates an nGram based on a char[] input + * Generates nGrams based on a {@code char[]} input. * @param input the array of chars to convert to nGram * @param n The number of grams (chars) that each output gram will consist of * @param separator each char in each gram will be separated by this value if desired. * Pass in empty string if no separator is desired - * @return + * @return The generated list of resulting nGrams. */ public static List<String> generate(char[] input, int n, String separator) { diff --git a/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramModel.java b/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramModel.java index 9047b0ca..467ecfc5 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramModel.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramModel.java @@ -42,7 +42,7 @@ public class NGramModel implements Iterable<StringList> { protected static final String COUNT = "count"; - private Map<StringList, Integer> mNGrams = new LinkedHashMap<>(); + private final Map<StringList, Integer> mNGrams = new LinkedHashMap<>(); /** * Initializes an empty instance. @@ -264,9 +264,9 @@ public class NGramModel implements Iterable<StringList> { /** * Creates a dictionary which contain all {@link StringList} which * are in the current {@link NGramModel}. - * + * <p> * Entries which are only different in the case are merged into one. - * + * <p> * Calling this method is the same as calling {@link #toDictionary(boolean)} with true. * * @return a dictionary of the ngrams @@ -303,9 +303,8 @@ public class NGramModel implements Iterable<StringList> { * @throws IOException if an I/O Error during writing occurs */ public void serialize(OutputStream out) throws IOException { - Iterator<Entry> entryIterator = new Iterator<Entry>() - { - private Iterator<StringList> mDictionaryIterator = NGramModel.this.iterator(); + Iterator<Entry> entryIterator = new Iterator<>() { + private final Iterator<StringList> mDictionaryIterator = NGramModel.this.iterator(); @Override public boolean hasNext() { @@ -320,7 +319,7 @@ public class NGramModel implements Iterable<StringList> { Attributes attributes = new Attributes(); attributes.setValue(COUNT, Integer.toString(getCount(tokens))); - + return new Entry(tokens, attributes); } diff --git a/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramUtils.java b/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramUtils.java index 269fa1bb..a49b9adb 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramUtils.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ngram/NGramUtils.java @@ -143,10 +143,10 @@ public class NGramUtils { * @param set the vocabulary * @return the probability */ - public static double calculateMissingNgramProbabilityMass(StringList ngram, Double discount, + public static double calculateMissingNgramProbabilityMass(StringList ngram, double discount, Iterable<StringList> set) { - Double missingMass = 0d; - Double countWord = count(ngram, set); + double missingMass = 0d; + double countWord = count(ngram, set); for (String word : flatSet(set)) { missingMass += (count(getNPlusOneNgram(ngram, word), set) - discount) / countWord; } diff --git a/opennlp-tools/src/main/java/opennlp/tools/parser/Constituent.java b/opennlp-tools/src/main/java/opennlp/tools/parser/Constituent.java index 2b042090..0225e141 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/parser/Constituent.java +++ b/opennlp-tools/src/main/java/opennlp/tools/parser/Constituent.java @@ -27,7 +27,7 @@ import opennlp.tools.util.Span; public class Constituent { private String label; - private Span span; + private final Span span; public Constituent(String label, Span span) { this.label = label; diff --git a/opennlp-tools/src/main/java/opennlp/tools/parser/ParserModel.java b/opennlp-tools/src/main/java/opennlp/tools/parser/ParserModel.java index 8b892261..89553e49 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/parser/ParserModel.java +++ b/opennlp-tools/src/main/java/opennlp/tools/parser/ParserModel.java @@ -203,8 +203,7 @@ public class ParserModel extends BaseModel { } @Override - protected void createArtifactSerializers( - Map<String, ArtifactSerializer> serializers) { + protected void createArtifactSerializers(Map<String, ArtifactSerializer> serializers) { super.createArtifactSerializers(serializers); diff --git a/opennlp-tools/src/main/java/opennlp/tools/parser/lang/en/HeadRules.java b/opennlp-tools/src/main/java/opennlp/tools/parser/lang/en/HeadRules.java index 5cbb986d..1ccc087f 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/parser/lang/en/HeadRules.java +++ b/opennlp-tools/src/main/java/opennlp/tools/parser/lang/en/HeadRules.java @@ -181,19 +181,19 @@ public class HeadRules implements opennlp.tools.parser.HeadRules, GapLabeler, Se int cl = constituents.length; int tl = tags.length; if (hr.leftToRight) { - for (int ti = 0; ti < tl; ti++) { - for (int ci = 0; ci < cl; ci++) { - if (constituents[ci].getType().equals(tags[ti])) { - return constituents[ci].getHead(); + for (String tag : tags) { + for (Parse constituent : constituents) { + if (constituent.getType().equals(tag)) { + return constituent.getHead(); } } } return constituents[0].getHead(); } else { - for (int ti = 0; ti < tl; ti++) { + for (String tag : tags) { for (int ci = cl - 1; ci >= 0; ci--) { - if (constituents[ci].getType().equals(tags[ti])) { + if (constituents[ci].getType().equals(tag)) { return constituents[ci].getHead(); } } diff --git a/opennlp-tools/src/main/java/opennlp/tools/parser/lang/es/AncoraSpanishHeadRules.java b/opennlp-tools/src/main/java/opennlp/tools/parser/lang/es/AncoraSpanishHeadRules.java index e59e0438..05590bde 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/parser/lang/es/AncoraSpanishHeadRules.java +++ b/opennlp-tools/src/main/java/opennlp/tools/parser/lang/es/AncoraSpanishHeadRules.java @@ -48,15 +48,15 @@ import opennlp.tools.util.model.SerializableArtifact; * Class for storing the Ancora Spanish head rules associated with parsing. In this class * headrules for noun phrases are specified. The rest of the rules are * in opennlp-tools/lang/es/parser/es-head-rules - * + * <p> * NOTE: This class has been adapted from opennlp.tools.parser.lang.en.HeadRules - * + * <p> * The main change is the constituents search direction in the first for loop. - * + * <p> * Note also the change in the return of the getHead() method: * In the lang.en.HeadRules class: return constituents[ci].getHead(); * Now: return constituents[ci]; - * + * <p> * Other changes include removal of deprecated methods. * */ @@ -182,19 +182,19 @@ public class AncoraSpanishHeadRules implements HeadRules, GapLabeler, Serializab int cl = constituents.length; int tl = tags.length; if (hr.leftToRight) { - for (int ti = 0; ti < tl; ti++) { - for (int ci = 0; ci < cl; ci++) { - if (constituents[ci].getType().matches(tags[ti])) { - return constituents[ci]; + for (String tag : tags) { + for (Parse constituent : constituents) { + if (constituent.getType().matches(tag)) { + return constituent; } } } return constituents[0].getHead(); } else { - for (int ti = 0; ti < tl; ti++) { + for (String tag : tags) { for (int ci = cl - 1; ci >= 0; ci--) { - if (constituents[ci].getType().matches(tags[ti])) { + if (constituents[ci].getType().matches(tag)) { return constituents[ci]; } } diff --git a/opennlp-tools/src/main/java/opennlp/tools/stemmer/snowball/SnowballProgram.java b/opennlp-tools/src/main/java/opennlp/tools/stemmer/snowball/SnowballProgram.java index 5d44e446..18fcf11e 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/stemmer/snowball/SnowballProgram.java +++ b/opennlp-tools/src/main/java/opennlp/tools/stemmer/snowball/SnowballProgram.java @@ -283,10 +283,7 @@ class SnowballProgram { Object resobj = w.method.invoke(w.methodobject, new Object[0]); res = resobj.toString().equals("true"); - } catch (InvocationTargetException e) { - res = false; - logger.warn(e.getLocalizedMessage(), e); - } catch (IllegalAccessException e) { + } catch (InvocationTargetException | IllegalAccessException e) { res = false; logger.warn(e.getLocalizedMessage(), e); } @@ -353,13 +350,9 @@ class SnowballProgram { boolean res; try { - Object resobj = w.method.invoke(w.methodobject, - new Object[0]); + Object resobj = w.method.invoke(w.methodobject, new Object[0]); res = resobj.toString().equals("true"); - } catch (InvocationTargetException e) { - res = false; - logger.warn(e.getLocalizedMessage(), e); - } catch (IllegalAccessException e) { + } catch (InvocationTargetException | IllegalAccessException e) { res = false; logger.warn(e.getLocalizedMessage(), e); } diff --git a/opennlp-tools/src/main/java/opennlp/tools/stemmer/snowball/greekStemmer.java b/opennlp-tools/src/main/java/opennlp/tools/stemmer/snowball/greekStemmer.java index ffc7a595..24636194 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/stemmer/snowball/greekStemmer.java +++ b/opennlp-tools/src/main/java/opennlp/tools/stemmer/snowball/greekStemmer.java @@ -1180,7 +1180,7 @@ public class greekStemmer extends AbstractSnowballStemmer { private static final char g_v2[] = {81, 65, 0, 1 }; - private StringBuilder S_s = new StringBuilder(); + private final StringBuilder S_s = new StringBuilder(); private boolean B_test1; diff --git a/opennlp-tools/src/main/java/opennlp/tools/tokenize/lang/en/TokenSampleStream.java b/opennlp-tools/src/main/java/opennlp/tools/tokenize/lang/en/TokenSampleStream.java index 7884f681..e723215a 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/tokenize/lang/en/TokenSampleStream.java +++ b/opennlp-tools/src/main/java/opennlp/tools/tokenize/lang/en/TokenSampleStream.java @@ -40,9 +40,9 @@ import opennlp.tools.util.Span; public class TokenSampleStream implements Iterator<TokenSample> { private static final Logger logger = LoggerFactory.getLogger(TokenSampleStream.class); - private BufferedReader in; + private final BufferedReader in; private String line; - private Pattern alphaNumeric = Pattern.compile("[A-Za-z0-9]"); + private final Pattern alphaNumeric = Pattern.compile("[A-Za-z0-9]"); private boolean evenq = true; public TokenSampleStream(InputStream is) throws IOException { diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/ObjectStreamUtils.java b/opennlp-tools/src/main/java/opennlp/tools/util/ObjectStreamUtils.java index 8d07455c..996bc683 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/util/ObjectStreamUtils.java +++ b/opennlp-tools/src/main/java/opennlp/tools/util/ObjectStreamUtils.java @@ -34,7 +34,7 @@ public class ObjectStreamUtils { @SafeVarargs public static <T> ObjectStream<T> createObjectStream(final T... array) { - return new ObjectStream<T>() { + return new ObjectStream<>() { private int index = 0; @@ -67,7 +67,7 @@ public class ObjectStreamUtils { */ public static <T> ObjectStream<T> createObjectStream(final Collection<T> collection) { - return new ObjectStream<T>() { + return new ObjectStream<>() { private Iterator<T> iterator = collection.iterator(); @@ -109,7 +109,7 @@ public class ObjectStreamUtils { } } - return new ObjectStream<T>() { + return new ObjectStream<>() { private Iterator<ObjectStream<T>> iterator = streams.iterator(); private ObjectStream<T> currentStream = iterator.next(); @@ -165,7 +165,7 @@ public class ObjectStreamUtils { } } - return new ObjectStream<T>() { + return new ObjectStream<>() { private int streamIndex = 0; diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/eval/FMeasure.java b/opennlp-tools/src/main/java/opennlp/tools/util/eval/FMeasure.java index d150f3ff..935a7876 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/util/eval/FMeasure.java +++ b/opennlp-tools/src/main/java/opennlp/tools/util/eval/FMeasure.java @@ -25,7 +25,7 @@ import java.util.List; /** * The {@link FMeasure} is a utility class for {@link Evaluator evaluators} * which measures precision, recall and the resulting f-measure. - * + * <p> * Evaluation results are the arithmetic mean of the precision * scores calculated for each reference sample and * the arithmetic mean of the recall scores calculated for diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/CustomFeatureGenerator.java b/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/CustomFeatureGenerator.java index 688a154e..d8f9c39d 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/CustomFeatureGenerator.java +++ b/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/CustomFeatureGenerator.java @@ -27,7 +27,7 @@ public abstract class CustomFeatureGenerator implements AdaptiveFeatureGenerator /** * Initializes {@link CustomFeatureGenerator} with defined properties and loaded resources. * - * @param properties The {@link Map<String, String>} properties to use. + * @param properties The {@link Map} properties to use. * @param resourceProvider The {@link FeatureGeneratorResourceProvider} to use. */ public abstract void init(Map<String, String> properties, FeatureGeneratorResourceProvider resourceProvider) diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/GeneratorFactory.java b/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/GeneratorFactory.java index fd35bdd5..785577d7 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/GeneratorFactory.java +++ b/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/GeneratorFactory.java @@ -49,7 +49,7 @@ import opennlp.tools.util.model.POSModelSerializer; /** * Creates a set of feature generators based on a provided XML descriptor. - * + * <p> * Example of an XML descriptor: * <p> * <featureGenerators name="namefind"> @@ -83,7 +83,7 @@ import opennlp.tools.util.model.POSModelSerializer; * calls the * {@link GeneratorFactory.XmlFeatureGeneratorFactory#create(Element, FeatureGeneratorResourceProvider)} * method. - * + * <p> * In the example above the generators element is mapped to the * {@link AggregatedFeatureGeneratorFactory} which then * creates all the aggregated {@link AdaptiveFeatureGenerator}s to @@ -528,9 +528,9 @@ public class GeneratorFactory { } else { try { - Class factoryClass = Class.forName(className); + Class<?> factoryClass = Class.forName(className); try { - Constructor constructor = factoryClass.getConstructor(); + Constructor<?> constructor = factoryClass.getConstructor(); AbstractXmlFeatureGeneratorFactory factory = (AbstractXmlFeatureGeneratorFactory)constructor.newInstance(); factory.init(generatorElement, resourceManager); @@ -562,7 +562,7 @@ public class GeneratorFactory { /** * Creates an {@link AdaptiveFeatureGenerator} from an provided XML descriptor. - * + * <p> * Usually this XML descriptor contains a set of nested feature generators * which are then used to generate the features by one of the opennlp * components. diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/POSTaggerNameFeatureGenerator.java b/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/POSTaggerNameFeatureGenerator.java index a9a37b58..cc30ad68 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/POSTaggerNameFeatureGenerator.java +++ b/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/POSTaggerNameFeatureGenerator.java @@ -30,7 +30,7 @@ import opennlp.tools.postag.POSTaggerME; */ public class POSTaggerNameFeatureGenerator implements AdaptiveFeatureGenerator { - private POSTagger posTagger; + private final POSTagger posTagger; private String[] cachedTokens; private String[] cachedTags; diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/WindowFeatureGenerator.java b/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/WindowFeatureGenerator.java index 987aef4b..d6359881 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/WindowFeatureGenerator.java +++ b/opennlp-tools/src/main/java/opennlp/tools/util/featuregen/WindowFeatureGenerator.java @@ -24,7 +24,7 @@ import java.util.List; /** * Generates previous and next features for a given {@link AdaptiveFeatureGenerator}. * The window size can be specified. - * + * <p> * Features: * Current token is always included unchanged * Previous tokens are prefixed with p distance diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/model/ArtifactProvider.java b/opennlp-tools/src/main/java/opennlp/tools/util/model/ArtifactProvider.java index 73e49841..8cbb5042 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/util/model/ArtifactProvider.java +++ b/opennlp-tools/src/main/java/opennlp/tools/util/model/ArtifactProvider.java @@ -42,7 +42,7 @@ public interface ArtifactProvider { /** * Indicates if this provider was loaded from a serialized form. - * + * <p> * It is useful, for example, during the validation of artifacts: * Skip the time-consuming ones if those were already validated during the * serialization process. diff --git a/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java b/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java index dd4d9f2a..ec46919b 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java +++ b/opennlp-tools/src/main/java/opennlp/tools/util/model/BaseModel.java @@ -251,7 +251,7 @@ public abstract class BaseModel implements ArtifactProvider, Serializable { if ("manifest.properties".equals(entry.getName())) { // TODO: Probably better to use the serializer here directly! - ArtifactSerializer factory = artifactSerializers.get("properties"); + ArtifactSerializer<?> factory = artifactSerializers.get("properties"); artifactMap.put(entry.getName(), factory.create(zip)); isSearchingForManifest = false; } @@ -327,7 +327,7 @@ public abstract class BaseModel implements ArtifactProvider, Serializable { String entryName = entry.getName(); String extension = getEntryExtension(entryName); - ArtifactSerializer factory = artifactSerializers.get(extension); + ArtifactSerializer<?> factory = artifactSerializers.get(extension); String artifactSerializerClazzName = getManifestProperty(SERIALIZER_CLASS_NAME_PREFIX + entryName); @@ -373,7 +373,7 @@ public abstract class BaseModel implements ArtifactProvider, Serializable { * {link ArtifactSerializer} for. * @return Retrieves an {@link ArtifactSerializer artifact serialize}. */ - protected ArtifactSerializer getArtifactSerializer(String resourceName) { + protected ArtifactSerializer<?> getArtifactSerializer(String resourceName) { try { return artifactSerializers.get(getEntryExtension(resourceName)); } catch (InvalidFormatException e) { @@ -414,14 +414,12 @@ public abstract class BaseModel implements ArtifactProvider, Serializable { * @param serializers The key of the map is the file extension used to look up * an {@link ArtifactSerializer}. */ - protected void createArtifactSerializers( - Map<String, ArtifactSerializer> serializers) { + protected void createArtifactSerializers(Map<String, ArtifactSerializer> serializers) { if (this.toolFactory != null) serializers.putAll(this.toolFactory.createArtifactSerializersMap()); } - private void createBaseArtifactSerializers( - Map<String, ArtifactSerializer> serializers) { + private void createBaseArtifactSerializers(Map<String, ArtifactSerializer> serializers) { serializers.putAll(createArtifactSerializers()); } diff --git a/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerEvaluatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerEvaluatorTest.java index 1cdbadf4..aa6b8ef2 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerEvaluatorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerEvaluatorTest.java @@ -32,8 +32,6 @@ import opennlp.tools.util.eval.FMeasure; /** * Tests for {@link ChunkerEvaluator}. - * - * @see ChunkerEvaluator */ public class ChunkerEvaluatorTest { @@ -41,11 +39,11 @@ public class ChunkerEvaluatorTest { /** * Checks the evaluator results against the results got using the conlleval, - * available at http://www.cnts.ua.ac.be/conll2000/chunking/output.html - * The output.txt file has only 3 sentences, but can be replaced by the one + * available at <a href="https://www.cnts.ua.ac.be/conll2000/chunking/output.html"> + * https://www.cnts.ua.ac.be/conll2000/chunking/output.html</a>. + * <p> + * The {@code output.txt} file has only 3 sentences, but can be replaced by the one * available at the conll2000 site to validate using a bigger sample. - * - * @throws IOException */ @Test void testEvaluator() throws IOException { diff --git a/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerMEIT.java b/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerMEIT.java index 7384bf3f..f8b84d15 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerMEIT.java +++ b/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerMEIT.java @@ -24,14 +24,14 @@ import org.junit.jupiter.api.Test; public class ChunkerMEIT { - private static String[] toks1 = {"Rockwell", "said", "the", "agreement", "calls", "for", + private static final String[] toks1 = {"Rockwell", "said", "the", "agreement", "calls", "for", "it", "to", "supply", "200", "additional", "so-called", "shipsets", "for", "the", "planes", "."}; - private static String[] tags1 = {"NNP", "VBD", "DT", "NN", "VBZ", "IN", "PRP", "TO", "VB", + private static final String[] tags1 = {"NNP", "VBD", "DT", "NN", "VBZ", "IN", "PRP", "TO", "VB", "CD", "JJ", "JJ", "NNS", "IN", "DT", "NNS", "."}; - private static String[] expect1 = {"B-NP", "B-VP", "B-NP", "I-NP", "B-VP", "B-SBAR", + private static final String[] expect1 = {"B-NP", "B-VP", "B-NP", "I-NP", "B-VP", "B-SBAR", "B-NP", "B-VP", "I-VP", "B-NP", "I-NP", "I-NP", "I-NP", "B-PP", "B-NP", "I-NP", "O"}; diff --git a/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerMETest.java b/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerMETest.java index 374dfdf6..5fc2d123 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerMETest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/chunker/ChunkerMETest.java @@ -54,14 +54,14 @@ public class ChunkerMETest { private Chunker chunker; - private static String[] toks1 = {"Rockwell", "said", "the", "agreement", "calls", "for", + private static final String[] toks1 = {"Rockwell", "said", "the", "agreement", "calls", "for", "it", "to", "supply", "200", "additional", "so-called", "shipsets", "for", "the", "planes", "."}; - private static String[] tags1 = {"NNP", "VBD", "DT", "NN", "VBZ", "IN", "PRP", "TO", "VB", + private static final String[] tags1 = {"NNP", "VBD", "DT", "NN", "VBZ", "IN", "PRP", "TO", "VB", "CD", "JJ", "JJ", "NNS", "IN", "DT", "NNS", "."}; - private static String[] expect1 = {"B-NP", "B-VP", "B-NP", "I-NP", "B-VP", "B-SBAR", + private static final String[] expect1 = {"B-NP", "B-VP", "B-NP", "I-NP", "B-VP", "B-SBAR", "B-NP", "B-VP", "I-VP", "B-NP", "I-NP", "I-NP", "I-NP", "B-PP", "B-NP", "I-NP", "O"}; diff --git a/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java b/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java index a05dd8ac..b4ea3e21 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java +++ b/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunkSampleStream.java @@ -37,7 +37,7 @@ public class DummyChunkSampleStream extends private static final Logger logger = LoggerFactory.getLogger(DummyChunkSampleStream.class); - private boolean mIsPredicted; + private final boolean mIsPredicted; private int count = 0; // the predicted flag sets if the stream will contain the expected or the diff --git a/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunker.java b/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunker.java index 6bacdebc..23250e51 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunker.java +++ b/opennlp-tools/src/test/java/opennlp/tools/chunker/DummyChunker.java @@ -32,7 +32,7 @@ import opennlp.tools.util.Span; */ public class DummyChunker implements Chunker { - private DummyChunkSampleStream mSampleStream; + private final DummyChunkSampleStream mSampleStream; public DummyChunker(DummyChunkSampleStream aSampleStream) { mSampleStream = aSampleStream; diff --git a/opennlp-tools/src/test/java/opennlp/tools/cmdline/tokenizer/TokenizerTrainerToolTest.java b/opennlp-tools/src/test/java/opennlp/tools/cmdline/tokenizer/TokenizerTrainerToolTest.java index dc94b922..80902f58 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/cmdline/tokenizer/TokenizerTrainerToolTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/cmdline/tokenizer/TokenizerTrainerToolTest.java @@ -45,14 +45,14 @@ public class TokenizerTrainerToolTest extends AbstractTempDirTest { private TokenizerTrainerTool tokenizerTrainerTool; - private String sampleSuccessData = + private final String sampleSuccessData = "Pierre Vinken<SPLIT>, 61 years old<SPLIT>, will join the board as a nonexecutive " + "director Nov. 29<SPLIT>.\n" + "Mr. Vinken is chairman of Elsevier N.V.<SPLIT>, the Dutch publishing group<SPLIT>.\n" + "Rudolph Agnew<SPLIT>, 55 years old and former chairman of Consolidated Gold Fields PLC<SPLIT>,\n" + " was named a nonexecutive director of this British industrial conglomerate<SPLIT>.\n"; - private String sampleFailureData = "It is Fail Test Case.\n\nNothing in this sentence."; + private final String sampleFailureData = "It is Fail Test Case.\n\nNothing in this sentence."; @BeforeEach void setUp() { diff --git a/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryTest.java b/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryTest.java index 9046d6ce..9aee19a3 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/dictionary/DictionaryTest.java @@ -25,7 +25,6 @@ import java.io.StringReader; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import opennlp.tools.util.InvalidFormatException; import opennlp.tools.util.StringList; /** @@ -87,8 +86,7 @@ public class DictionaryTest { /** * Tests serialization and deserailization of the {@link Dictionary}. * - * @throws IOException - * @throws InvalidFormatException + * @throws IOException Thrown if IO errors occurred. */ @Test void testSerialization() throws IOException { @@ -115,7 +113,7 @@ public class DictionaryTest { * Tests for the {@link Dictionary#parseOneEntryPerLine(java.io.Reader)} * method. * - * @throws IOException + * @throws IOException Thrown if IO errors occurred. */ @Test void testParseOneEntryPerLine() throws IOException { diff --git a/opennlp-tools/src/test/java/opennlp/tools/eval/Conll02NameFinderEval.java b/opennlp-tools/src/test/java/opennlp/tools/eval/Conll02NameFinderEval.java index 9a171256..5869f236 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/eval/Conll02NameFinderEval.java +++ b/opennlp-tools/src/test/java/opennlp/tools/eval/Conll02NameFinderEval.java @@ -41,10 +41,10 @@ import opennlp.tools.util.model.ModelUtil; * Evaluates the name finder against the Dutch and Spanish CONLL2002 corpus. * <p> * Download the tarball from the CONLL2002 shared task - * <a href="http://www.cnts.ua.ac.be/conll2002/ner/"> site </a> + * <a href="https://www.cnts.ua.ac.be/conll2002/ner/"> site </a> * and decompress it into this directory: $OPENNLP_DATA_DIR/conll2002. * Also decompress the training files. - * + * <p> * TODO: * - Files are provided in gzipped. It would be better if they would not be unpacked by the user. * - Double check the encoding which is used to open the files. Currently that is UTF-8. diff --git a/opennlp-tools/src/test/java/opennlp/tools/formats/leipzig/LeipzigLanguageSampleStreamTest.java b/opennlp-tools/src/test/java/opennlp/tools/formats/leipzig/LeipzigLanguageSampleStreamTest.java index 4d6334c2..3bbc33c9 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/formats/leipzig/LeipzigLanguageSampleStreamTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/formats/leipzig/LeipzigLanguageSampleStreamTest.java @@ -32,7 +32,7 @@ import opennlp.tools.util.InvalidFormatException; public class LeipzigLanguageSampleStreamTest { - private static String testDataPath = LeipzigLanguageSampleStreamTest.class + private static final String testDataPath = LeipzigLanguageSampleStreamTest.class .getClassLoader().getResource("opennlp/tools/formats/leipzig/samples").getPath(); @TempDir diff --git a/opennlp-tools/src/test/java/opennlp/tools/langdetect/DummyFactory.java b/opennlp-tools/src/test/java/opennlp/tools/langdetect/DummyFactory.java index 528bd125..6c102b59 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/langdetect/DummyFactory.java +++ b/opennlp-tools/src/test/java/opennlp/tools/langdetect/DummyFactory.java @@ -20,7 +20,6 @@ package opennlp.tools.langdetect; import java.util.ArrayList; import java.util.Arrays; -import java.util.Iterator; import java.util.List; import opennlp.tools.ngram.NGramModel; @@ -77,10 +76,8 @@ public class DummyFactory extends LanguageDetectorFactory { NGramModel tokenNgramModel = new NGramModel(); if (words.length > 0) { tokenNgramModel.add(new StringList(words), 1, 3); - Iterator<StringList> tokenNgramIterator = tokenNgramModel.iterator(); - while (tokenNgramIterator.hasNext()) { - StringList tokenList = tokenNgramIterator.next(); + for (StringList tokenList : tokenNgramModel) { if (tokenList.size() > 0) { context.add("tg=" + tokenList.toString()); } diff --git a/opennlp-tools/src/test/java/opennlp/tools/languagemodel/LanguageModelTestUtils.java b/opennlp-tools/src/test/java/opennlp/tools/languagemodel/LanguageModelTestUtils.java index fb78bc7a..f025a521 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/languagemodel/LanguageModelTestUtils.java +++ b/opennlp-tools/src/test/java/opennlp/tools/languagemodel/LanguageModelTestUtils.java @@ -34,7 +34,7 @@ import opennlp.tools.ngram.NGramUtils; public class LanguageModelTestUtils { private static final java.math.MathContext CONTEXT = MathContext.DECIMAL128; - private static Random r = new Random(); + private static final Random r = new Random(); private static final char[] chars = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}; diff --git a/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/DummyLemmaSampleStream.java b/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/DummyLemmaSampleStream.java index 5c1cb7c9..dd108343 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/DummyLemmaSampleStream.java +++ b/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/DummyLemmaSampleStream.java @@ -36,7 +36,7 @@ public class DummyLemmaSampleStream extends FilterObjectStream<String, LemmaSample> { private static final Logger logger = LoggerFactory.getLogger(DummyLemmaSampleStream.class); - private boolean mIsPredicted; + private final boolean mIsPredicted; private int count = 0; // the predicted flag sets if the stream will contain the expected or the diff --git a/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerEvaluatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerEvaluatorTest.java index f8a49641..ffbdb796 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerEvaluatorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerEvaluatorTest.java @@ -41,10 +41,11 @@ public class LemmatizerEvaluatorTest { /** * Checks the evaluator results against the results got using the conlleval, - * available at http://www.cnts.ua.ac.be/conll2000/chunking/output.html but + * available at <a href="https://www.cnts.ua.ac.be/conll2000/chunking/output.html"> + * https://www.cnts.ua.ac.be/conll2000/chunking/output.html</a> but * containing lemmas instead of chunks. * - * @throws IOException + * @throws IOException Thrown if IO errors occurred. */ @Test void testEvaluator() throws IOException { diff --git a/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerMETest.java b/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerMETest.java index 40a3757f..152e4b16 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerMETest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/lemmatizer/LemmatizerMETest.java @@ -51,14 +51,14 @@ public class LemmatizerMETest { private LemmatizerME lemmatizer; - private static String[] tokens = {"Rockwell", "said", "the", "agreement", "calls", "for", + private static final String[] tokens = {"Rockwell", "said", "the", "agreement", "calls", "for", "it", "to", "supply", "200", "additional", "so-called", "shipsets", "for", "the", "planes", "."}; - private static String[] postags = {"NNP", "VBD", "DT", "NN", "VBZ", "IN", "PRP", "TO", "VB", + private static final String[] postags = {"NNP", "VBD", "DT", "NN", "VBZ", "IN", "PRP", "TO", "VB", "CD", "JJ", "JJ", "NNS", "IN", "DT", "NNS", "."}; - private static String[] expect = {"rockwell", "say", "the", "agreement", "call", "for", + private static final String[] expect = {"rockwell", "say", "the", "agreement", "call", "for", "it", "to", "supply", "200", "additional", "so-called", "shipset", "for", "the", "plane", "."}; diff --git a/opennlp-tools/src/test/java/opennlp/tools/ml/BeamSearchTest.java b/opennlp-tools/src/test/java/opennlp/tools/ml/BeamSearchTest.java index 2e49d666..bf246b87 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/ml/BeamSearchTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/ml/BeamSearchTest.java @@ -31,7 +31,7 @@ public class BeamSearchTest { static class IdentityFeatureGenerator implements BeamSearchContextGenerator<String> { - private String[] outcomeSequence; + private final String[] outcomeSequence; IdentityFeatureGenerator(String[] outcomeSequence) { this.outcomeSequence = outcomeSequence; @@ -46,12 +46,12 @@ public class BeamSearchTest { static class IdentityModel implements MaxentModel { - private String[] outcomes; + private final String[] outcomes; - private Map<String, Integer> outcomeIndexMap = new HashMap<>(); + private final Map<String, Integer> outcomeIndexMap = new HashMap<>(); - private double bestOutcomeProb = 0.8d; - private double otherOutcomeProb; + private final double bestOutcomeProb = 0.8d; + private final double otherOutcomeProb; IdentityModel(String[] outcomes) { this.outcomes = outcomes; diff --git a/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/GISIndexingTest.java b/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/GISIndexingTest.java index 8398294e..912d30f5 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/GISIndexingTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/GISIndexingTest.java @@ -42,12 +42,12 @@ import opennlp.tools.util.model.ModelUtil; public class GISIndexingTest { - private static String[][] cntx = new String[][] { + private static final String[][] cntx = new String[][] { {"dog", "cat", "mouse"}, {"text", "print", "mouse"}, {"dog", "pig", "cat", "mouse"} }; - private static String[] outputs = new String[] {"A", "B", "A"}; + private static final String[] outputs = new String[] {"A", "B", "A"}; private ObjectStream<Event> createEventStream() { List<Event> events = new ArrayList<>(); diff --git a/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/URLInputStreamFactory.java b/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/URLInputStreamFactory.java index 30c6eb92..bb227859 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/URLInputStreamFactory.java +++ b/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/URLInputStreamFactory.java @@ -26,7 +26,7 @@ import opennlp.tools.util.InputStreamFactory; public class URLInputStreamFactory implements InputStreamFactory { - private URL url; + private final URL url; public URLInputStreamFactory(URL url) { this.url = url; diff --git a/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/LineSearchTest.java b/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/LineSearchTest.java index 75d71ec7..c7a0104d 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/LineSearchTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/LineSearchTest.java @@ -170,7 +170,7 @@ public class LineSearchTest { /** * Quadratic function: f(x) = (x-2)^2 + 4 */ - public class QuadraticFunction1 implements Function { + public static class QuadraticFunction1 implements Function { public double valueAt(double[] x) { // (x-2)^2 + 4; @@ -190,7 +190,7 @@ public class LineSearchTest { /** * Quadratic function: f(x) = x^2 */ - public class QuadraticFunction2 implements Function { + public static class QuadraticFunction2 implements Function { public double valueAt(double[] x) { // x^2; diff --git a/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNMinimizerTest.java b/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNMinimizerTest.java index 57261c79..7411f090 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNMinimizerTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/ml/maxent/quasinewton/QNMinimizerTest.java @@ -49,7 +49,7 @@ public class QNMinimizerTest { /** * Quadratic function: f(x,y) = (x-1)^2 + (y-5)^2 + 10 */ - public class QuadraticFunction implements Function { + public static class QuadraticFunction implements Function { @Override public int getDimension() { @@ -68,14 +68,18 @@ public class QNMinimizerTest { } /** - * Rosenbrock function (http://en.wikipedia.org/wiki/Rosenbrock_function) - * f(x,y) = (1-x)^2 + 100*(y-x^2)^2 - * f(x,y) is non-convex and has global minimum at (x,y) = (1,1) where f(x,y) = 0 + * <a href="https://en.wikipedia.org/wiki/Rosenbrock_function">Rosenbrock function</a>: * <p> - * f_x = -2*(1-x) - 400*(y-x^2)*x - * f_y = 200*(y-x^2) + * {@code f(x,y) = (1-x)^2 + 100*(y-x^2)^2} + * {@code f(x,y)} is non-convex and has global minimum at {@code (x,y) = (1,1)} where {@code f(x,y) = 0}. + * <p> + * with + * <ul> + * <li>{@code f_x = -2*(1-x) - 400*(y-x^2)*x}</li> + * <li>{@code f_y = 200*(y-x^2)}</li> + * </ul> */ - public class Rosenbrock implements Function { + public static class Rosenbrock implements Function { @Override public int getDimension() { diff --git a/opennlp-tools/src/test/java/opennlp/tools/ml/model/SimpleEventStreamBuilder.java b/opennlp-tools/src/test/java/opennlp/tools/ml/model/SimpleEventStreamBuilder.java index f17f554d..cbe453b2 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/ml/model/SimpleEventStreamBuilder.java +++ b/opennlp-tools/src/test/java/opennlp/tools/ml/model/SimpleEventStreamBuilder.java @@ -62,7 +62,7 @@ public class SimpleEventStreamBuilder { } public ObjectStream<Event> build() { - return new ObjectStream<Event>() { + return new ObjectStream<>() { @Override public Event read() { if (eventList.size() <= pos) { diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/BilouCodecTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/BilouCodecTest.java index 1601c484..10510153 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/BilouCodecTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/BilouCodecTest.java @@ -110,7 +110,7 @@ public class BilouCodecTest { @Test void testDecodeEmpty() { Span[] expected = new Span[] {}; - Span[] actual = codec.decode(new ArrayList<String>()); + Span[] actual = codec.decode(new ArrayList<>()); Assertions.assertArrayEquals(expected, actual); } @@ -213,7 +213,7 @@ public class BilouCodecTest { Assertions.assertFalse(codec.areOutcomesCompatible(new String[] {})); } - /** + /* * Singles and singles in combination with other valid type (unit/start+last) */ diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/BilouNameFinderSequenceValidatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/BilouNameFinderSequenceValidatorTest.java index daee648a..403baf61 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/BilouNameFinderSequenceValidatorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/BilouNameFinderSequenceValidatorTest.java @@ -25,19 +25,19 @@ import org.junit.jupiter.api.Test; */ public class BilouNameFinderSequenceValidatorTest { - private static BilouNameFinderSequenceValidator validator = new BilouNameFinderSequenceValidator(); - private static String START_A = "TypeA-" + BilouCodec.START; - private static String CONTINUE_A = "TypeA-" + BilouCodec.CONTINUE; - private static String LAST_A = "TypeA-" + BilouCodec.LAST; - private static String UNIT_A = "TypeA-" + BilouCodec.UNIT; + private static final BilouNameFinderSequenceValidator validator = new BilouNameFinderSequenceValidator(); + private static final String START_A = "TypeA-" + BilouCodec.START; + private static final String CONTINUE_A = "TypeA-" + BilouCodec.CONTINUE; + private static final String LAST_A = "TypeA-" + BilouCodec.LAST; + private static final String UNIT_A = "TypeA-" + BilouCodec.UNIT; - private static String START_B = "TypeB-" + BilouCodec.START; - private static String CONTINUE_B = "TypeB-" + BilouCodec.CONTINUE; - private static String LAST_B = "TypeB-" + BilouCodec.LAST; + private static final String START_B = "TypeB-" + BilouCodec.START; + private static final String CONTINUE_B = "TypeB-" + BilouCodec.CONTINUE; + private static final String LAST_B = "TypeB-" + BilouCodec.LAST; //private static String UNIT = BilouCodec.UNIT; - private static String OTHER = BilouCodec.OTHER; + private static final String OTHER = BilouCodec.OTHER; @Test void testStartAsFirstLabel() { diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/BioCodecTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/BioCodecTest.java index b6c5d34f..3a47c41d 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/BioCodecTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/BioCodecTest.java @@ -118,7 +118,7 @@ public class BioCodecTest { @Test void testDecodeEmpty() { Span[] expected = new Span[] {}; - Span[] actual = codec.decode(new ArrayList<String>()); + Span[] actual = codec.decode(new ArrayList<>()); Assertions.assertArrayEquals(expected, actual); } diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/DictionaryNameFinderEvaluatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/DictionaryNameFinderEvaluatorTest.java index 89a7765f..cf3e6533 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/DictionaryNameFinderEvaluatorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/DictionaryNameFinderEvaluatorTest.java @@ -18,7 +18,6 @@ package opennlp.tools.namefind; import java.io.IOException; -import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -42,9 +41,8 @@ import opennlp.tools.util.eval.FMeasure; public class DictionaryNameFinderEvaluatorTest { @Test - void testEvaluator() throws IOException, URISyntaxException { - DictionaryNameFinder nameFinder = new DictionaryNameFinder( - createDictionary()); + void testEvaluator() throws IOException { + DictionaryNameFinder nameFinder = new DictionaryNameFinder(createDictionary()); TokenNameFinderEvaluator evaluator = new TokenNameFinderEvaluator( nameFinder, new NameEvaluationErrorListener()); ObjectStream<NameSample> sample = createSample(); @@ -58,10 +56,8 @@ public class DictionaryNameFinderEvaluatorTest { } /** - * Creates a NameSample stream using an annotated corpus - * - * @return - * @throws IOException + * @return A {@link NameSample} stream using an annotated corpus + * @throws IOException Thrown if IO errors occurred. */ private static ObjectStream<NameSample> createSample() throws IOException { @@ -73,14 +69,10 @@ public class DictionaryNameFinderEvaluatorTest { } /** - * Creates a dictionary with all names from the sample data. - * - * @return a dictionary - * @throws IOException - * @throws URISyntaxException + * @return A {@link Dictionary} with all names from the sample data. + * @throws IOException Thrown if IO errors occurred. */ - private static Dictionary createDictionary() throws IOException, - URISyntaxException { + private static Dictionary createDictionary() throws IOException { ObjectStream<NameSample> sampleStream = createSample(); NameSample sample = sampleStream.read(); List<String[]> entries = new ArrayList<>(); diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/DictionaryNameFinderTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/DictionaryNameFinderTest.java index d40d9e82..b297dd39 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/DictionaryNameFinderTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/DictionaryNameFinderTest.java @@ -31,7 +31,7 @@ import opennlp.tools.util.StringList; */ public class DictionaryNameFinderTest { - private Dictionary mDictionary = new Dictionary(); + private final Dictionary mDictionary = new Dictionary(); private TokenNameFinder mNameFinder; public DictionaryNameFinderTest() { diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderSequenceValidatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderSequenceValidatorTest.java index 796f15cc..d8fb583e 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderSequenceValidatorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameFinderSequenceValidatorTest.java @@ -24,12 +24,12 @@ import org.junit.jupiter.api.Test; */ public class NameFinderSequenceValidatorTest { - private static NameFinderSequenceValidator validator = new NameFinderSequenceValidator(); - private static String START_A = "TypeA-" + NameFinderME.START; - private static String CONTINUE_A = "TypeA-" + NameFinderME.CONTINUE; - private static String START_B = "TypeB-" + NameFinderME.START; - private static String CONTINUE_B = "TypeB-" + NameFinderME.CONTINUE; - private static String OTHER = NameFinderME.OTHER; + private static final NameFinderSequenceValidator validator = new NameFinderSequenceValidator(); + private static final String START_A = "TypeA-" + NameFinderME.START; + private static final String CONTINUE_A = "TypeA-" + NameFinderME.CONTINUE; + private static final String START_B = "TypeB-" + NameFinderME.START; + private static final String CONTINUE_B = "TypeB-" + NameFinderME.CONTINUE; + private static final String OTHER = NameFinderME.OTHER; @Test void testContinueCannotBeFirstOutcome() { diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleDataStreamTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleDataStreamTest.java index 68f827fa..c3fb4505 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleDataStreamTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleDataStreamTest.java @@ -66,10 +66,10 @@ public class NameSampleDataStreamTest { * Create a NameSampleDataStream from a corpus with entities annotated but * without nameType and validate it. * - * @throws Exception + * @throws IOException Thrown if IO errors occurred. */ @Test - void testWithoutNameTypes() throws Exception { + void testWithoutNameTypes() throws IOException { InputStreamFactory in = new ResourceAsStreamFactory(getClass(), "/opennlp/tools/namefind/AnnotatedSentences.txt"); @@ -164,10 +164,10 @@ public class NameSampleDataStreamTest { * Create a NameSampleDataStream from a corpus with entities annotated * with multiple nameTypes, like person, date, location and organization, and validate it. * - * @throws Exception + * @throws IOException Thrown if IO errors occurred. */ @Test - void testWithNameTypes() throws Exception { + void testWithNameTypes() throws IOException { InputStreamFactory in = new ResourceAsStreamFactory(getClass(), "/opennlp/tools/namefind/voa1.train"); diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTest.java index 0aaf9d32..5cae50bc 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/NameSampleTest.java @@ -203,10 +203,10 @@ public class NameSampleTest { /** * Tests if an additional space is correctly treated as one space. * - * @throws Exception + * @throws IOException Thrown if IO errors occurred. */ @Test - void testParseWithAdditionalSpace() throws Exception { + void testParseWithAdditionalSpace() throws IOException { String line = "<START> M . K . <END> <START> Schwitters <END> ? <START> Heartfield <END> ?"; NameSample test = NameSample.parse(line, false); diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/RegexNameFinderFactoryTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/RegexNameFinderFactoryTest.java index 4ddc4955..80942992 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/RegexNameFinderFactoryTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/RegexNameFinderFactoryTest.java @@ -31,7 +31,7 @@ public class RegexNameFinderFactoryTest { private static RegexNameFinder regexNameFinder; - private static String text = "my email is [email protected] and my phone num is" + + private static final String text = "my email is [email protected] and my phone num is" + " 123-234-5678 and i like" + " https://www.google.com and I visited MGRS 11sku528111 AKA 11S KU 528 111 and" + " DMS 45N 123W AKA" + diff --git a/opennlp-tools/src/test/java/opennlp/tools/namefind/TokenNameFinderEvaluatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/namefind/TokenNameFinderEvaluatorTest.java index 97b2a678..8cc7261f 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/namefind/TokenNameFinderEvaluatorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/namefind/TokenNameFinderEvaluatorTest.java @@ -78,7 +78,7 @@ public class TokenNameFinderEvaluatorTest { } - private static String[] sentence = {"U", ".", "S", ".", "President", "Barack", "Obama", "is", + private static final String[] sentence = {"U", ".", "S", ".", "President", "Barack", "Obama", "is", "considering", "sending", "additional", "American", "forces", "to", "Afghanistan", "."}; diff --git a/opennlp-tools/src/test/java/opennlp/tools/parser/ParserTestUtil.java b/opennlp-tools/src/test/java/opennlp/tools/parser/ParserTestUtil.java index 380c7813..e435b39a 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/parser/ParserTestUtil.java +++ b/opennlp-tools/src/test/java/opennlp/tools/parser/ParserTestUtil.java @@ -49,7 +49,7 @@ public class ParserTestUtil { public static ObjectStream<Parse> openTestTrainingData() throws IOException { - ObjectStream<Parse> resetableSampleStream = new ObjectStream<Parse>() { + ObjectStream<Parse> resetableSampleStream = new ObjectStream<>() { private ObjectStream<Parse> samples; @@ -67,7 +67,7 @@ public class ParserTestUtil { samples.close(); } InputStreamFactory in = new ResourceAsStreamFactory(getClass(), - "/opennlp/tools/parser/parser.train"); + "/opennlp/tools/parser/parser.train"); samples = new ParseSampleStream(new PlainTextByLineStream(in, StandardCharsets.UTF_8)); } catch (UnsupportedEncodingException e) { // Should never happen diff --git a/opennlp-tools/src/test/java/opennlp/tools/postag/POSDictionaryTest.java b/opennlp-tools/src/test/java/opennlp/tools/postag/POSDictionaryTest.java index ee581365..7d8228c1 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/postag/POSDictionaryTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/postag/POSDictionaryTest.java @@ -35,20 +35,14 @@ public class POSDictionaryTest { } private static POSDictionary serializeDeserializeDict(POSDictionary dict) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - - try { + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { dict.serialize(out); - } finally { - out.close(); - } - - POSDictionary serializedDictionary; - try (InputStream in = new ByteArrayInputStream(out.toByteArray())) { - serializedDictionary = POSDictionary.create(in); + POSDictionary serializedDictionary; + try (InputStream in = new ByteArrayInputStream(out.toByteArray())) { + serializedDictionary = POSDictionary.create(in); + } + return serializedDictionary; } - - return serializedDictionary; } @Test diff --git a/opennlp-tools/src/test/java/opennlp/tools/postag/POSEvaluatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/postag/POSEvaluatorTest.java index 482b4c31..6a056d6b 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/postag/POSEvaluatorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/postag/POSEvaluatorTest.java @@ -37,7 +37,7 @@ public class POSEvaluatorTest { POSTaggerEvaluationMonitor listener = new POSEvaluationErrorListener(stream); POSEvaluator eval = new POSEvaluator(new DummyPOSTagger( - POSSampleTest.createGoldSample()), listener); + POSSampleTest.createGoldSample()), listener); eval.evaluateSample(POSSampleTest.createGoldSample()); Assertions.assertEquals(1.0, eval.getWordAccuracy(), 0.0); @@ -50,16 +50,16 @@ public class POSEvaluatorTest { POSTaggerEvaluationMonitor listener = new POSEvaluationErrorListener(stream); POSEvaluator eval = new POSEvaluator( - new DummyPOSTagger(POSSampleTest.createGoldSample()), listener); + new DummyPOSTagger(POSSampleTest.createGoldSample()), listener); eval.evaluateSample(POSSampleTest.createPredSample()); Assertions.assertEquals(.7, eval.getWordAccuracy(), .1d); Assertions.assertNotSame(0, stream.toString().length()); } - class DummyPOSTagger implements POSTagger { + static class DummyPOSTagger implements POSTagger { - private POSSample sample; + private final POSSample sample; public DummyPOSTagger(POSSample sample) { this.sample = sample; diff --git a/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DefaultSDContextGeneratorTest.java b/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DefaultSDContextGeneratorTest.java index b5c8f846..83d8dac5 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DefaultSDContextGeneratorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DefaultSDContextGeneratorTest.java @@ -31,7 +31,7 @@ public class DefaultSDContextGeneratorTest { @Test void testGetContext() { SDContextGenerator sdContextGenerator = - new DefaultSDContextGenerator(Collections.<String>emptySet(), Factory.defaultEosCharacters); + new DefaultSDContextGenerator(Collections.emptySet(), Factory.defaultEosCharacters); String[] context = sdContextGenerator.getContext( "Mr. Smith joined RONDHUIT Inc. as a manager of sales department.", 2); diff --git a/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java b/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java index 8873d86a..9a88f2e5 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java +++ b/opennlp-tools/src/test/java/opennlp/tools/sentdetect/DummySentenceDetectorFactory.java @@ -95,7 +95,7 @@ public class DummySentenceDetectorFactory extends SentenceDetectorFactory { } public static class DummyDictionary extends Dictionary { - private Dictionary indict; + private final Dictionary indict; public DummyDictionary(Dictionary dict) { this.indict = dict; diff --git a/opennlp-tools/src/test/java/opennlp/tools/tokenize/DetokenizerEvaluatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/tokenize/DetokenizerEvaluatorTest.java index 2310aa79..4eb4c918 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/tokenize/DetokenizerEvaluatorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/tokenize/DetokenizerEvaluatorTest.java @@ -32,7 +32,7 @@ public class DetokenizerEvaluatorTest { DetokenEvaluationErrorListener listener = new DetokenEvaluationErrorListener(stream); DetokenizerEvaluator eval = new DetokenizerEvaluator(new DummyDetokenizer( - TokenSampleTest.createGoldSample()), listener); + TokenSampleTest.createGoldSample()), listener); eval.evaluateSample(TokenSampleTest.createGoldSample()); @@ -48,7 +48,7 @@ public class DetokenizerEvaluatorTest { stream); DetokenizerEvaluator eval = new DetokenizerEvaluator(new DummyDetokenizer( - TokenSampleTest.createGoldSample()), listener); + TokenSampleTest.createGoldSample()), listener); eval.evaluateSample(TokenSampleTest.createPredSilverSample()); @@ -60,9 +60,9 @@ public class DetokenizerEvaluatorTest { /** * a dummy tokenizer that always return something expected */ - class DummyDetokenizer implements Detokenizer { + static class DummyDetokenizer implements Detokenizer { - private TokenSample sample; + private final TokenSample sample; public DummyDetokenizer(TokenSample sample) { this.sample = sample; diff --git a/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java b/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java index 937930db..a4c23e74 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java +++ b/opennlp-tools/src/test/java/opennlp/tools/tokenize/DummyTokenizerFactory.java @@ -93,7 +93,7 @@ public class DummyTokenizerFactory extends TokenizerFactory { } public static class DummyDictionary extends Dictionary { - private Dictionary indict; + private final Dictionary indict; public DummyDictionary(Dictionary dict) { this.indict = dict; diff --git a/opennlp-tools/src/test/java/opennlp/tools/tokenize/SimpleTokenizerTest.java b/opennlp-tools/src/test/java/opennlp/tools/tokenize/SimpleTokenizerTest.java index a117a5ad..3748c91a 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/tokenize/SimpleTokenizerTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/tokenize/SimpleTokenizerTest.java @@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test; public class SimpleTokenizerTest { // The SimpleTokenizer is thread safe - private SimpleTokenizer mTokenizer = SimpleTokenizer.INSTANCE; + private final SimpleTokenizer mTokenizer = SimpleTokenizer.INSTANCE; /** * Tests if it can tokenize whitespace separated tokens. diff --git a/opennlp-tools/src/test/java/opennlp/tools/tokenize/TokenizerEvaluatorTest.java b/opennlp-tools/src/test/java/opennlp/tools/tokenize/TokenizerEvaluatorTest.java index e2a3769b..c9ac7832 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/tokenize/TokenizerEvaluatorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/tokenize/TokenizerEvaluatorTest.java @@ -34,7 +34,7 @@ public class TokenizerEvaluatorTest { TokenizerEvaluationMonitor listener = new TokenEvaluationErrorListener(stream); TokenizerEvaluator eval = new TokenizerEvaluator(new DummyTokenizer( - TokenSampleTest.createGoldSample()), listener); + TokenSampleTest.createGoldSample()), listener); eval.evaluateSample(TokenSampleTest.createGoldSample()); @@ -50,7 +50,7 @@ public class TokenizerEvaluatorTest { stream); TokenizerEvaluator eval = new TokenizerEvaluator(new DummyTokenizer( - TokenSampleTest.createGoldSample()), listener); + TokenSampleTest.createGoldSample()), listener); eval.evaluateSample(TokenSampleTest.createPredSample()); @@ -62,9 +62,9 @@ public class TokenizerEvaluatorTest { /** * a dummy tokenizer that always return something expected */ - class DummyTokenizer implements Tokenizer { + static class DummyTokenizer implements Tokenizer { - private TokenSample sample; + private final TokenSample sample; public DummyTokenizer(TokenSample sample) { this.sample = sample; diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/AbstractEventStreamTest.java b/opennlp-tools/src/test/java/opennlp/tools/util/AbstractEventStreamTest.java index 131cb4a5..1500bfb6 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/util/AbstractEventStreamTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/util/AbstractEventStreamTest.java @@ -89,7 +89,7 @@ public class AbstractEventStreamTest { * testing the {@link AbstractEventStream#hasNext()} * and {@link AbstractEventStream#next()} methods. */ - class TestEventStream extends AbstractEventStream<RESULT> { + static class TestEventStream extends AbstractEventStream<RESULT> { public TestEventStream(ObjectStream<RESULT> samples) { diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/SpanTest.java b/opennlp-tools/src/test/java/opennlp/tools/util/SpanTest.java index 2f95e2fd..bd7dfd56 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/util/SpanTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/util/SpanTest.java @@ -161,7 +161,7 @@ public class SpanTest { } /** - * Test for {@link Span#compareTo(Object)}. + * Test for {@link Span#compareTo(Span)}. */ @Test void testCompareToLower() { @@ -171,7 +171,7 @@ public class SpanTest { } /** - * Test for {@link Span#compareTo(Object)}. + * Test for {@link Span#compareTo(Span)}. */ @Test void testCompareToHigher() { @@ -181,65 +181,65 @@ public class SpanTest { } /** - * Test for {@link Span#compareTo(Object)}. + * Test for {@link Span#compareTo(Span)}. */ @Test void testCompareToEquals() { Span a = new Span(30, 1000); Span b = new Span(30, 1000); - Assertions.assertTrue(a.compareTo(b) == 0); + Assertions.assertEquals(0, a.compareTo(b)); } /// /** - * Test for {@link Span#compareTo(Object)}. + * Test for {@link Span#compareTo(Span)}. */ @Test void testCompareToEqualsSameType() { Span a = new Span(30, 1000, "a"); Span b = new Span(30, 1000, "a"); - Assertions.assertTrue(a.compareTo(b) == 0); + Assertions.assertEquals(0, a.compareTo(b)); } /** - * Test for {@link Span#compareTo(Object)}. + * Test for {@link Span#compareTo(Span)}. */ @Test void testCompareToEqualsDiffType1() { Span a = new Span(30, 1000, "a"); Span b = new Span(30, 1000, "b"); - Assertions.assertTrue(a.compareTo(b) == -1); + Assertions.assertEquals(-1, a.compareTo(b)); } /** - * Test for {@link Span#compareTo(Object)}. + * Test for {@link Span#compareTo(Span)}. */ @Test void testCompareToEqualsDiffType2() { Span a = new Span(30, 1000, "b"); Span b = new Span(30, 1000, "a"); - Assertions.assertTrue(a.compareTo(b) == 1); + Assertions.assertEquals(1, a.compareTo(b)); } /** - * Test for {@link Span#compareTo(Object)}. + * Test for {@link Span#compareTo(Span)}. */ @Test void testCompareToEqualsNullType1() { Span a = new Span(30, 1000); Span b = new Span(30, 1000, "b"); - Assertions.assertTrue(a.compareTo(b) == 1); + Assertions.assertEquals(1, a.compareTo(b)); } /** - * Test for {@link Span#compareTo(Object)}. + * Test for {@link Span#compareTo(Span)}. */ @Test void testCompareToEqualsNullType2() { Span a = new Span(30, 1000, "b"); Span b = new Span(30, 1000); - Assertions.assertTrue(a.compareTo(b) == -1); + Assertions.assertEquals(-1, a.compareTo(b)); } /** @@ -256,7 +256,7 @@ public class SpanTest { @Test void testEqualsWithNull() { Span a = new Span(0, 0); - Assertions.assertFalse(a.equals(null)); + Assertions.assertNotNull(a); } /** diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/eval/FMeasureTest.java b/opennlp-tools/src/test/java/opennlp/tools/util/eval/FMeasureTest.java index d15730ad..477f533e 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/util/eval/FMeasureTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/util/eval/FMeasureTest.java @@ -29,7 +29,7 @@ public class FMeasureTest { private static final double DELTA = 1.0E-9d; - private Span[] gold = { + private final Span[] gold = { new Span(8, 9), new Span(9, 10), new Span(10, 12), @@ -38,7 +38,7 @@ public class FMeasureTest { new Span(15, 16) }; - private Span[] predicted = { + private final Span[] predicted = { new Span(14, 15), new Span(15, 16), new Span(100, 120), @@ -46,7 +46,7 @@ public class FMeasureTest { new Span(220, 230) }; - private Span[] predictedCompletelyDistinct = { + private final Span[] predictedCompletelyDistinct = { new Span(100, 120), new Span(210, 220), new Span(211, 220), @@ -54,7 +54,7 @@ public class FMeasureTest { new Span(220, 230) }; - private Span[] goldToMerge = { + private final Span[] goldToMerge = { new Span(8, 9), new Span(9, 10), new Span(11, 11), @@ -64,7 +64,7 @@ public class FMeasureTest { new Span(18, 19), }; - private Span[] predictedToMerge = { + private final Span[] predictedToMerge = { new Span(8, 9), new Span(14, 15), new Span(15, 16), @@ -75,7 +75,7 @@ public class FMeasureTest { /** - * Test for the {@link EvaluatorUtil#countTruePositives(Span[], Span[])} method. + * Test for the {@link FMeasure#countTruePositives(Object[], Object[])} method. */ @Test public void testCountTruePositives() { @@ -86,7 +86,7 @@ public class FMeasureTest { } /** - * Test for the {@link EvaluatorUtil#precision(Span[], Span[])} method. + * Test for the {@link FMeasure#precision(Object[], Object[])} method. */ @Test public void testPrecision() { @@ -98,7 +98,7 @@ public class FMeasureTest { } /** - * Test for the {@link EvaluatorUtil#recall(Span[], Span[])} method. + * Test for the {@link FMeasure#recall(Object[], Object[])} method. */ @Test public void testRecall() { diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/CachedFeatureGeneratorTest.java b/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/CachedFeatureGeneratorTest.java index 5b7f8672..1f8ed485 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/CachedFeatureGeneratorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/CachedFeatureGeneratorTest.java @@ -29,7 +29,7 @@ import org.junit.jupiter.api.Test; */ public class CachedFeatureGeneratorTest { - private AdaptiveFeatureGenerator[] identityGenerator = new AdaptiveFeatureGenerator[] { + private final AdaptiveFeatureGenerator[] identityGenerator = new AdaptiveFeatureGenerator[] { new IdentityFeatureGenerator()}; private String[] testSentence1; diff --git a/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/WindowFeatureGeneratorTest.java b/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/WindowFeatureGeneratorTest.java index 5b9edcfe..ce8a4d67 100644 --- a/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/WindowFeatureGeneratorTest.java +++ b/opennlp-tools/src/test/java/opennlp/tools/util/featuregen/WindowFeatureGeneratorTest.java @@ -29,7 +29,7 @@ import org.junit.jupiter.api.Test; */ public class WindowFeatureGeneratorTest { - private String[] testSentence = new String[] {"a", "b", "c", "d", + private final String[] testSentence = new String[] {"a", "b", "c", "d", "e", "f", "g", "h"}; private List<String> features;
