Skip to content

Commit

Permalink
OPENNLP-1483 Apply Java language migration aids (#524)
Browse files Browse the repository at this point in the history
- modernizes existing classes and tests towards Java 8+
- adds `final` modifier where applicable
- adjusts broken or non-compliant JavaDoc
  • Loading branch information
mawiesne committed Mar 25, 2023
1 parent 9b9ceb0 commit 14e0e47
Show file tree
Hide file tree
Showing 78 changed files with 230 additions and 260 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ChunkerEvaluator(Chunker chunker, ChunkerEvaluationMonitor... listeners)

/**
* 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void run(String format, String[] args) {

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void run(String format, String[] args) {

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void run(String format, String[] args) {

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());
}
Expand Down Expand Up @@ -120,7 +120,7 @@ public void run(String format, String[] args) {

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public String getMorphologicalTag() {

/** 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private boolean notNull(Object... objects) {
return true;
}

@Override
public ObjectStream<NameSample> create(String[] args) {

Parameters params = ArgumentParser.parse(args, Parameters.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private ConlluWordLine mergeAnnotation(ConlluWordLine contraction,
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()
Expand All @@ -217,7 +217,7 @@ private ConlluWordLine mergeAnnotation(ConlluWordLine contraction,

String feats = expandedParts.stream()
.filter(p -> !"_".equals(p.getFeats()))
.map(p -> p.getFeats())
.map(ConlluWordLine::getFeats)
.collect(Collectors.joining("+"));

String head = contraction.getHead();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ protected List<List<ComparablePredicate>> compressOutcomes(ComparablePredicate[]
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ public void persist() throws IOException {
// 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
Expand All @@ -67,21 +66,19 @@ public void persist() throws IOException {

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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void index(ObjectStream<Event> eventStream) throws IOException {
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ protected List<List<ComparablePredicate>> compressOutcomes(ComparablePredicate[]
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);
Expand Down Expand Up @@ -198,9 +198,9 @@ public void persist() throws IOException {
}

// 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ public void persist() throws IOException {
}

// 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/**
* 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@
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) {

Expand All @@ -57,12 +56,12 @@ public static List<String> generate(List<String> input, int n, String separator)
}

/**
*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) {

Expand Down
13 changes: 6 additions & 7 deletions opennlp-tools/src/main/java/opennlp/tools/ngram/NGramModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -264,9 +264,9 @@ public void cutoff(int cutoffUnder, int cutoffOver) {
/**
* 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
Expand Down Expand Up @@ -303,9 +303,8 @@ public Dictionary toDictionary(boolean caseSensitive) {
* @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() {
Expand All @@ -320,7 +319,7 @@ public Entry next() {
Attributes attributes = new Attributes();

attributes.setValue(COUNT, Integer.toString(getCount(tokens)));

return new Entry(tokens, attributes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ public static double calculateTrigramLinearInterpolationProbability(String x0, S
* @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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class Constituent {

private String label;
private Span span;
private final Span span;

public Constituent(String label, Span span) {
this.label = label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ public ParserModel(URL modelURL) throws IOException {
}

@Override
protected void createArtifactSerializers(
Map<String, ArtifactSerializer> serializers) {
protected void createArtifactSerializers(Map<String, ArtifactSerializer> serializers) {

super.createArtifactSerializers(serializers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,19 @@ else if ((hr = headRules.get(type)) != null) {
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();
}
}
Expand Down
Loading

0 comments on commit 14e0e47

Please sign in to comment.