From b4416a07948bd3fbf2fe3069e3de03c054c6e040 Mon Sep 17 00:00:00 2001 From: smarthi Date: Mon, 9 Jan 2017 23:02:40 -0500 Subject: [PATCH] OPENNLP-920: Enforce TypeSafety in SequenceTrainer --- .../tools/cmdline/AbstractTypedParamTool.java | 5 ++--- .../opennlp/tools/ml/AbstractSequenceTrainer.java | 6 +++--- .../tools/ml/EventModelSequenceTrainer.java | 6 +++--- .../main/java/opennlp/tools/ml/EventTrainer.java | 8 ++++---- .../java/opennlp/tools/ml/SequenceTrainer.java | 6 +++--- .../parser/treeinsert/AttachContextGenerator.java | 10 +++------- .../opennlp/tools/sentdetect/SentenceModel.java | 14 +++++--------- .../java/opennlp/tools/tokenize/Detokenizer.java | 2 +- 8 files changed, 24 insertions(+), 33 deletions(-) diff --git a/opennlp-tools/src/main/java/opennlp/tools/cmdline/AbstractTypedParamTool.java b/opennlp-tools/src/main/java/opennlp/tools/cmdline/AbstractTypedParamTool.java index 33d87177f..86f0a107f 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/cmdline/AbstractTypedParamTool.java +++ b/opennlp-tools/src/main/java/opennlp/tools/cmdline/AbstractTypedParamTool.java @@ -42,15 +42,14 @@ protected AbstractTypedParamTool(Class sampleType, Class

paramsClass) { public String getHelp(String format) { if ("".equals(format) || StreamFactoryRegistry.DEFAULT_FORMAT.equals(format)) { return getBasicHelp(paramsClass, - StreamFactoryRegistry.getFactory(type, StreamFactoryRegistry.DEFAULT_FORMAT) - .

getParameters()); + StreamFactoryRegistry.getFactory(type, StreamFactoryRegistry.DEFAULT_FORMAT).getParameters()); } else { ObjectStreamFactory factory = StreamFactoryRegistry.getFactory(type, format); if (null == factory) { throw new TerminateToolException(1, "Format " + format + " is not found.\n" + getHelp()); } return "Usage: " + CLI.CMD + " " + getName() + "." + format + " " + - ArgumentParser.createUsage(paramsClass, factory.

getParameters()); + ArgumentParser.createUsage(paramsClass, factory.getParameters()); } } } diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/AbstractSequenceTrainer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/AbstractSequenceTrainer.java index 29862af67..8e5431b46 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ml/AbstractSequenceTrainer.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ml/AbstractSequenceTrainer.java @@ -28,16 +28,16 @@ public abstract class AbstractSequenceTrainer extends AbstractTrainer implements public AbstractSequenceTrainer() { } - public abstract SequenceClassificationModel doTrain(SequenceStream events) + public abstract SequenceClassificationModel doTrain(SequenceStream events) throws IOException; - public final SequenceClassificationModel train(SequenceStream events) throws IOException { + public final SequenceClassificationModel train(SequenceStream events) throws IOException { if (!isValid()) { throw new IllegalArgumentException("trainParams are not valid!"); } - SequenceClassificationModel model = doTrain(events); + SequenceClassificationModel model = doTrain(events); parameters.addToReport(AbstractTrainer.TRAINER_TYPE_PARAM, SequenceTrainer.SEQUENCE_VALUE); return model; } diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/EventModelSequenceTrainer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/EventModelSequenceTrainer.java index 6726ba756..601004132 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ml/EventModelSequenceTrainer.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ml/EventModelSequenceTrainer.java @@ -25,10 +25,10 @@ public interface EventModelSequenceTrainer { - public static final String SEQUENCE_VALUE = "EventModelSequence"; + String SEQUENCE_VALUE = "EventModelSequence"; - public void init(Map trainParams, Map reportMap); + void init(Map trainParams, Map reportMap); - public MaxentModel train(SequenceStream events) throws IOException; + MaxentModel train(SequenceStream events) throws IOException; } diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/EventTrainer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/EventTrainer.java index 11d27b26d..998262bce 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ml/EventTrainer.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ml/EventTrainer.java @@ -27,10 +27,10 @@ public interface EventTrainer { - public static final String EVENT_VALUE = "Event"; + String EVENT_VALUE = "Event"; - public void init(Map trainParams, Map reportMap); + void init(Map trainParams, Map reportMap); - public MaxentModel train(ObjectStream events) throws IOException; - public MaxentModel train(DataIndexer indexer) throws IOException; + MaxentModel train(ObjectStream events) throws IOException; + MaxentModel train(DataIndexer indexer) throws IOException; } diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/SequenceTrainer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/SequenceTrainer.java index 2fc5faebf..7443527a1 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/ml/SequenceTrainer.java +++ b/opennlp-tools/src/main/java/opennlp/tools/ml/SequenceTrainer.java @@ -25,9 +25,9 @@ public interface SequenceTrainer { - public static final String SEQUENCE_VALUE = "Sequence"; + String SEQUENCE_VALUE = "Sequence"; - public void init(Map trainParams, Map reportMap); + void init(Map trainParams, Map reportMap); - public SequenceClassificationModel train(SequenceStream events) throws IOException; + SequenceClassificationModel train(SequenceStream events) throws IOException; } diff --git a/opennlp-tools/src/main/java/opennlp/tools/parser/treeinsert/AttachContextGenerator.java b/opennlp-tools/src/main/java/opennlp/tools/parser/treeinsert/AttachContextGenerator.java index 5613ab3a6..4af1d2903 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/parser/treeinsert/AttachContextGenerator.java +++ b/opennlp-tools/src/main/java/opennlp/tools/parser/treeinsert/AttachContextGenerator.java @@ -20,7 +20,6 @@ import java.util.ArrayList; import java.util.Collection; -import java.util.Iterator; import java.util.List; import java.util.Set; @@ -30,20 +29,18 @@ public class AttachContextGenerator extends AbstractContextGenerator { - public AttachContextGenerator(Set punctSet) { this.punctSet = punctSet; } public String[] getContext(Object o) { Object[] parts = (Object[]) o; - return getContext((Parse[]) parts[0], (Integer) parts[1],(List) parts[2], (Integer) parts[3]); + return getContext((Parse[]) parts[0], (Integer) parts[1],(List) parts[2], (Integer) parts[3]); } private boolean containsPunct(Collection puncts, String punct) { if (puncts != null) { - for (Iterator pi = puncts.iterator(); pi.hasNext();) { - Parse p = pi.next(); + for (Parse p : puncts) { if (p.getType().equals(punct)) { return true; } @@ -121,8 +118,7 @@ public String[] getContext(Parse[] constituents, int index, List rightFro features.add("ps=" + fn.getType() + "->" + fn.getType() + "," + p0.getType()); if (punct_1s != null) { StringBuilder punctBuf = new StringBuilder(5); - for (Iterator pi = punct_1s.iterator(); pi.hasNext();) { - Parse punct = pi.next(); + for (Parse punct : punct_1s) { punctBuf.append(punct.getType()).append(","); } //features.add("ppd="+punctProd+","+punctBuf.toString()+p0.getType()); diff --git a/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceModel.java b/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceModel.java index cdd03464a..992143b3b 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceModel.java +++ b/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceModel.java @@ -20,7 +20,6 @@ import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -92,15 +91,15 @@ public SentenceModel(String languageCode, MaxentModel sentModel, this (languageCode, sentModel, useTokenEnd, abbreviations, null, null); } - public SentenceModel(InputStream in) throws IOException, InvalidFormatException { + public SentenceModel(InputStream in) throws IOException { super(COMPONENT_NAME, in); } - public SentenceModel(File modelFile) throws IOException, InvalidFormatException { + public SentenceModel(File modelFile) throws IOException { super(COMPONENT_NAME, modelFile); } - public SentenceModel(URL modelURL) throws IOException, InvalidFormatException { + public SentenceModel(URL modelURL) throws IOException { super(COMPONENT_NAME, modelURL); } @@ -141,10 +140,7 @@ public Dictionary getAbbreviations() { } public boolean useTokenEnd() { - if (getFactory() != null) { - return getFactory().isUseTokenEnd(); - } - return true; + return getFactory() == null || getFactory().isUseTokenEnd(); } public char[] getEosCharacters() { @@ -154,7 +150,7 @@ public char[] getEosCharacters() { return null; } - public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException { + public static void main(String[] args) throws IOException { if (args.length < 3) { System.err.println("SentenceModel [-abbreviationsDictionary] [-useTokenEnd] languageCode packageName modelName"); System.exit(1); diff --git a/opennlp-tools/src/main/java/opennlp/tools/tokenize/Detokenizer.java b/opennlp-tools/src/main/java/opennlp/tools/tokenize/Detokenizer.java index c962aa4a6..3af8597ed 100644 --- a/opennlp-tools/src/main/java/opennlp/tools/tokenize/Detokenizer.java +++ b/opennlp-tools/src/main/java/opennlp/tools/tokenize/Detokenizer.java @@ -27,7 +27,7 @@ public interface Detokenizer { * This enum contains an operation for every token to merge the * tokens together to their detokenized form. */ - public enum DetokenizationOperation { + enum DetokenizationOperation { /** * The current token should be attached to the begin token on the right side. */