You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@joshua.apache.org by to...@apache.org on 2016/08/18 07:59:58 UTC
[2/5] incubator-joshua git commit: JOSHUA-291 - static analysis based
code improvements on decoder package
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Tree.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Tree.java b/src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Tree.java
index 07c7ecd..60e8d20 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Tree.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Tree.java
@@ -83,7 +83,7 @@ public class Tree implements Serializable {
* hand, we can iterate through our store of language model fragments to match them against this,
* following tail nodes if necessary.
*/
- public static HashMap<String, String> rulesToFragmentStrings = new HashMap<String, String>();
+ public static final HashMap<String, String> rulesToFragmentStrings = new HashMap<>();
public Tree(String label, List<Tree> children) {
setLabel(label);
@@ -153,19 +153,19 @@ public class Tree implements Serializable {
}
public List<Tree> getNonterminalYield() {
- List<Tree> yield = new ArrayList<Tree>();
+ List<Tree> yield = new ArrayList<>();
appendNonterminalYield(this, yield);
return yield;
}
public List<Tree> getYield() {
- List<Tree> yield = new ArrayList<Tree>();
+ List<Tree> yield = new ArrayList<>();
appendYield(this, yield);
return yield;
}
public List<Tree> getTerminals() {
- List<Tree> yield = new ArrayList<Tree>();
+ List<Tree> yield = new ArrayList<>();
appendTerminals(this, yield);
return yield;
}
@@ -186,7 +186,7 @@ public class Tree implements Serializable {
* @return a cloned tree
*/
public Tree shallowClone() {
- ArrayList<Tree> newChildren = new ArrayList<Tree>(children.size());
+ ArrayList<Tree> newChildren = new ArrayList<>(children.size());
for (Tree child : children) {
newChildren.add(child.shallowClone());
}
@@ -222,7 +222,7 @@ public class Tree implements Serializable {
}
public List<Tree> getPreTerminalYield() {
- List<Tree> yield = new ArrayList<Tree>();
+ List<Tree> yield = new ArrayList<>();
appendPreTerminalYield(this, yield);
return yield;
}
@@ -250,9 +250,8 @@ public class Tree implements Serializable {
this.numLexicalItems = 1;
else {
this.numLexicalItems = 0;
- for (Tree child : children)
- if (child.isLexicalized())
- this.numLexicalItems += 1;
+ children.stream().filter(child -> child.isLexicalized())
+ .forEach(child -> this.numLexicalItems += 1);
}
}
@@ -283,7 +282,7 @@ public class Tree implements Serializable {
}
public List<Tree> getAtDepth(int depth) {
- List<Tree> yield = new ArrayList<Tree>();
+ List<Tree> yield = new ArrayList<>();
appendAtDepth(depth, this, yield);
return yield;
}
@@ -354,7 +353,7 @@ public class Tree implements Serializable {
* @return the <code>Set</code> of all subtrees in the tree.
*/
public Set<Tree> subTrees() {
- return (Set<Tree>) subTrees(new HashSet<Tree>());
+ return (Set<Tree>) subTrees(new HashSet<>());
}
/**
@@ -364,7 +363,7 @@ public class Tree implements Serializable {
* @return the <code>List</code> of all subtrees in the tree.
*/
public List<Tree> subTreeList() {
- return (List<Tree>) subTrees(new ArrayList<Tree>());
+ return (List<Tree>) subTrees(new ArrayList<>());
}
/**
@@ -397,10 +396,10 @@ public class Tree implements Serializable {
private class TreeIterator implements Iterator<Tree> {
- private List<Tree> treeStack;
+ private final List<Tree> treeStack;
private TreeIterator() {
- treeStack = new ArrayList<Tree>();
+ treeStack = new ArrayList<>();
treeStack.add(Tree.this);
}
@@ -499,8 +498,7 @@ public class Tree implements Serializable {
*/
public static Tree fromString(String ptbStr) {
PennTreeReader reader = new PennTreeReader(new StringReader(ptbStr));
- Tree fragment = reader.next();
- return fragment;
+ return reader.next();
}
public static Tree getFragmentFromYield(String yield) {
@@ -571,11 +569,11 @@ public class Tree implements Serializable {
* to a nonnegative 0-based permutation and store it in tailIndices. This is used to index
* the incoming DerivationState items, which are ordered by the source side.
*/
- ArrayList<Integer> tailIndices = new ArrayList<Integer>();
+ ArrayList<Integer> tailIndices = new ArrayList<>();
int[] englishInts = rule.getEnglish();
- for (int i = 0; i < englishInts.length; i++)
- if (englishInts[i] < 0)
- tailIndices.add(-(englishInts[i] + 1));
+ for (int englishInt : englishInts)
+ if (englishInt < 0)
+ tailIndices.add(-(englishInt + 1));
/*
* We now have the tree's yield. The substitution points on the yield should match the
@@ -643,11 +641,11 @@ public class Tree implements Serializable {
* to a nonnegative 0-based permutation and store it in tailIndices. This is used to index
* the incoming DerivationState items, which are ordered by the source side.
*/
- ArrayList<Integer> tailIndices = new ArrayList<Integer>();
+ ArrayList<Integer> tailIndices = new ArrayList<>();
int[] englishInts = rule.getEnglish();
- for (int i = 0; i < englishInts.length; i++)
- if (englishInts[i] < 0)
- tailIndices.add(-(englishInts[i] + 1));
+ for (int englishInt : englishInts)
+ if (englishInt < 0)
+ tailIndices.add(-(englishInt + 1));
/*
* We now have the tree's yield. The substitution points on the yield should match the
@@ -702,11 +700,11 @@ public class Tree implements Serializable {
if (tree != null && tailNodes != null && tailNodes.size() > 0 && maxDepth > 0) {
List<Tree> frontier = tree.getNonterminalYield();
- ArrayList<Integer> tailIndices = new ArrayList<Integer>();
+ ArrayList<Integer> tailIndices = new ArrayList<>();
int[] englishInts = rule.getEnglish();
- for (int i = 0; i < englishInts.length; i++)
- if (englishInts[i] < 0)
- tailIndices.add(-1 * englishInts[i] - 1);
+ for (int englishInt : englishInts)
+ if (englishInt < 0)
+ tailIndices.add(-1 * englishInt - 1);
/*
* We now have the tree's yield. The substitution points on the yield should match the
@@ -720,7 +718,7 @@ public class Tree implements Serializable {
// String lhs = tailNodes.get(i).getLHS().replaceAll("[\\[\\]]", "");
// System.err.println(String.format(" %d: %s", i, lhs));
try {
- Tree frontierTree = frontier.get(tailIndices.get(i).intValue());
+ Tree frontierTree = frontier.get(tailIndices.get(i));
frontierTree.setBoundary(true);
HyperEdge edge = tailNodes.get(i).bestHyperedge;
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Trees.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Trees.java b/src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Trees.java
index d06388c..211ad20 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Trees.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/fragmentlm/Trees.java
@@ -36,9 +36,9 @@ import org.apache.joshua.corpus.Vocabulary;
public class Trees {
public static class PennTreeReader implements Iterator<Tree> {
- public static String ROOT_LABEL = "ROOT";
+ public static final String ROOT_LABEL = "ROOT";
- PushbackReader in;
+ final PushbackReader in;
Tree nextTree;
public boolean hasNext() {
@@ -115,7 +115,7 @@ public class Trees {
}
private List<Tree> readChildList() throws IOException {
- List<Tree> children = new ArrayList<Tree>();
+ List<Tree> children = new ArrayList<>();
readWhiteSpace();
while (!isRightParen(peek())) {
children.add(readTree(false));
@@ -168,7 +168,7 @@ public class Trees {
}
public PennTreeReader(Reader in) {
- this.in = new PushbackReader((java.io.Reader) in);
+ this.in = new PushbackReader(in);
nextTree = readRootTree();
// System.out.println(nextTree);
}
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/lm/KenLM.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/lm/KenLM.java b/src/main/java/org/apache/joshua/decoder/ff/lm/KenLM.java
index 336189b..93d54ed 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/lm/KenLM.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/lm/KenLM.java
@@ -45,31 +45,31 @@ public class KenLM implements NGramLanguageModel, Comparable<KenLM> {
// inferred from model file (may be larger than ngramOrder)
private final int N;
- private final static native long construct(String file_name);
+ private static native long construct(String file_name);
- private final static native void destroy(long ptr);
+ private static native void destroy(long ptr);
- private final static native int order(long ptr);
+ private static native int order(long ptr);
- private final static native boolean registerWord(long ptr, String word, int id);
+ private static native boolean registerWord(long ptr, String word, int id);
- private final static native float prob(long ptr, int words[]);
+ private static native float prob(long ptr, int words[]);
- private final static native float probForString(long ptr, String[] words);
+ private static native float probForString(long ptr, String[] words);
- private final static native boolean isKnownWord(long ptr, String word);
+ private static native boolean isKnownWord(long ptr, String word);
- private final static native boolean isLmOov(long ptr, int word);
+ private static native boolean isLmOov(long ptr, int word);
- private final static native StateProbPair probRule(long ptr, long pool, long words[]);
+ private static native StateProbPair probRule(long ptr, long pool, long words[]);
- private final static native float estimateRule(long ptr, long words[]);
+ private static native float estimateRule(long ptr, long words[]);
- private final static native float probString(long ptr, int words[], int start);
+ private static native float probString(long ptr, int words[], int start);
- private final static native long createPool();
+ private static native long createPool();
- private final static native void destroyPool(long pointer);
+ private static native void destroyPool(long pointer);
public KenLM(int order, String file_name) {
pointer = initializeSystemLibrary(file_name);
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/lm/LanguageModelFF.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/lm/LanguageModelFF.java b/src/main/java/org/apache/joshua/decoder/ff/lm/LanguageModelFF.java
index 7b0bac8..f5c1cb5 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/lm/LanguageModelFF.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/lm/LanguageModelFF.java
@@ -94,10 +94,10 @@ public class LanguageModelFF extends StatefulFF {
/**
* We cache the weight of the feature since there is only one.
*/
- protected float weight;
- protected float oovWeight;
+ protected final float weight;
+ protected final float oovWeight;
protected String type;
- protected String path;
+ protected final String path;
/** Whether this is a class-based LM */
protected boolean isClassLM;
@@ -137,7 +137,7 @@ public class LanguageModelFF extends StatefulFF {
denseFeatureIndex = index;
oovDenseFeatureIndex = denseFeatureIndex + 1;
- final ArrayList<String> names = new ArrayList<String>(2);
+ final ArrayList<String> names = new ArrayList<>(2);
names.add(name);
if (withOovFeature) {
names.add(oovFeatureName);
@@ -149,13 +149,16 @@ public class LanguageModelFF extends StatefulFF {
* Initializes the underlying language model.
*/
protected void initializeLM() {
- if (type.equals("kenlm")) {
+ switch (type) {
+ case "kenlm":
this.languageModel = new KenLM(ngramOrder, path);
- } else if (type.equals("berkeleylm")) {
+ break;
+ case "berkeleylm":
this.languageModel = new LMGrammarBerkeley(ngramOrder, path);
- } else {
+ break;
+ default:
String msg = String.format("* FATAL: Invalid backend lm_type '%s' for LanguageModel", type)
+ "* Permissible values for 'lm_type' are 'kenlm' and 'berkeleylm'";
throw new RuntimeException(msg);
@@ -329,15 +332,14 @@ public class LanguageModelFF extends StatefulFF {
int[] enWords = getRuleIds(rule);
- List<Integer> words = new ArrayList<Integer>();
+ List<Integer> words = new ArrayList<>();
boolean skipStart = (enWords[0] == startSymbolId);
/*
* Move through the words, accumulating language model costs each time we have an n-gram (n >=
* 2), and resetting the series of words when we hit a nonterminal.
*/
- for (int c = 0; c < enWords.length; c++) {
- int currentWord = enWords[c];
+ for (int currentWord : enWords) {
if (FormatUtils.isNonterminal(currentWord)) {
lmEstimate += scoreChunkLogP(words, considerIncompleteNgrams, skipStart);
words.clear();
@@ -396,9 +398,7 @@ public class LanguageModelFF extends StatefulFF {
float transitionLogP = 0.0f;
int[] left_context = null;
- for (int c = 0; c < enWords.length; c++) {
- int curID = enWords[c];
-
+ for (int curID : enWords) {
if (FormatUtils.isNonterminal(curID)) {
int index = -(curID + 1);
@@ -407,8 +407,8 @@ public class LanguageModelFF extends StatefulFF {
int[] right = state.getRightLMStateWords();
// Left context.
- for (int i = 0; i < left.length; i++) {
- current[ccount++] = left[i];
+ for (int aLeft : left) {
+ current[ccount++] = aLeft;
if (left_context == null && ccount == this.ngramOrder - 1)
left_context = Arrays.copyOf(current, ccount);
@@ -470,17 +470,16 @@ public class LanguageModelFF extends StatefulFF {
// System.err.println(String.format("LanguageModel::computeFinalTransition()"));
float res = 0.0f;
- LinkedList<Integer> currentNgram = new LinkedList<Integer>();
+ LinkedList<Integer> currentNgram = new LinkedList<>();
int[] leftContext = state.getLeftLMStateWords();
int[] rightContext = state.getRightLMStateWords();
- for (int i = 0; i < leftContext.length; i++) {
- int t = leftContext[i];
+ for (int t : leftContext) {
currentNgram.add(t);
if (currentNgram.size() >= 2) { // start from bigram
- float prob = this.languageModel.ngramLogProbability(Support.toArray(currentNgram),
- currentNgram.size());
+ float prob = this.languageModel
+ .ngramLogProbability(Support.toArray(currentNgram), currentNgram.size());
res += prob;
}
if (currentNgram.size() == this.ngramOrder)
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/lm/berkeley_lm/LMGrammarBerkeley.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/lm/berkeley_lm/LMGrammarBerkeley.java b/src/main/java/org/apache/joshua/decoder/ff/lm/berkeley_lm/LMGrammarBerkeley.java
index 5c45520..4bf55b5 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/lm/berkeley_lm/LMGrammarBerkeley.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/lm/berkeley_lm/LMGrammarBerkeley.java
@@ -53,7 +53,7 @@ public class LMGrammarBerkeley extends DefaultNGramLanguageModel {
private int[] vocabIdToMyIdMapping;
- private ThreadLocal<int[]> arrayScratch = new ThreadLocal<int[]>() {
+ private final ThreadLocal<int[]> arrayScratch = new ThreadLocal<int[]>() {
@Override
protected int[] initialValue() {
@@ -117,7 +117,7 @@ public class LMGrammarBerkeley extends DefaultNGramLanguageModel {
public boolean isOov(int id) {
// for Berkeley, we unfortunately have to temporarily convert to String
return lm.getWordIndexer().getIndexPossiblyUnk(Vocabulary.word(id)) <= 0;
- };
+ }
@Override
public float sentenceLogProbability(int[] sentence, int order, int startIndex) {
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/lm/bloomfilter_lm/BloomFilter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/lm/bloomfilter_lm/BloomFilter.java b/src/main/java/org/apache/joshua/decoder/ff/lm/bloomfilter_lm/BloomFilter.java
index a66fa44..06f30e7 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/lm/bloomfilter_lm/BloomFilter.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/lm/bloomfilter_lm/BloomFilter.java
@@ -76,7 +76,7 @@ public class BloomFilter implements Externalizable {
/**
* A random number generator for building hash functions.
*/
- transient private Random RANDOM = new Random();
+ final transient private Random RANDOM = new Random();
/**
* Builds an empty Bloom filter, ready to build hash functions and store objects.
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/lm/bloomfilter_lm/BloomFilterLanguageModel.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/lm/bloomfilter_lm/BloomFilterLanguageModel.java b/src/main/java/org/apache/joshua/decoder/ff/lm/bloomfilter_lm/BloomFilterLanguageModel.java
index 4c56aac..7932364 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/lm/bloomfilter_lm/BloomFilterLanguageModel.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/lm/bloomfilter_lm/BloomFilterLanguageModel.java
@@ -365,7 +365,7 @@ public class BloomFilterLanguageModel extends DefaultNGramLanguageModel implemen
* @param filename path to the statistics file
*/
private void populateBloomFilter(int bloomFilterSize, String filename) {
- HashMap<String, Long> typesAfter = new HashMap<String, Long>();
+ HashMap<String, Long> typesAfter = new HashMap<>();
try {
FileInputStream file_in = new FileInputStream(filename);
FileInputStream file_in_copy = new FileInputStream(filename);
@@ -396,7 +396,6 @@ public class BloomFilterLanguageModel extends DefaultNGramLanguageModel implemen
hist[i] = Vocabulary.id(toks[i]);
add(hist, typesAfter.get(history), typesFuncs);
}
- return;
}
/**
@@ -460,11 +459,10 @@ public class BloomFilterLanguageModel extends DefaultNGramLanguageModel implemen
if (types.get(history) == null)
types.put(history.toString(), 1L);
else {
- long x = (Long) types.get(history);
+ long x = types.get(history);
types.put(history.toString(), x + 1);
}
}
- return;
}
/**
@@ -527,14 +525,14 @@ public class BloomFilterLanguageModel extends DefaultNGramLanguageModel implemen
}
out.writeDouble(numTokens);
out.writeInt(countFuncs.length);
- for (int i = 0; i < countFuncs.length; i++) {
- out.writeLong(countFuncs[i][0]);
- out.writeLong(countFuncs[i][1]);
+ for (long[] countFunc : countFuncs) {
+ out.writeLong(countFunc[0]);
+ out.writeLong(countFunc[1]);
}
out.writeInt(typesFuncs.length);
- for (int i = 0; i < typesFuncs.length; i++) {
- out.writeLong(typesFuncs[i][0]);
- out.writeLong(typesFuncs[i][1]);
+ for (long[] typesFunc : typesFuncs) {
+ out.writeLong(typesFunc[0]);
+ out.writeLong(typesFunc[1]);
}
out.writeDouble(quantizationBase);
bf.writeExternal(out);
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/lm/buildin_lm/TrieLM.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/lm/buildin_lm/TrieLM.java b/src/main/java/org/apache/joshua/decoder/ff/lm/buildin_lm/TrieLM.java
index bb4732f..9bfccb0 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/lm/buildin_lm/TrieLM.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/lm/buildin_lm/TrieLM.java
@@ -23,6 +23,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
@@ -94,14 +95,14 @@ public class TrieLM extends AbstractLM { //DefaultNGramLanguageModel {
* @throws FileNotFoundException if the input file cannot be located
*/
public TrieLM(ArpaFile arpaFile) throws FileNotFoundException {
- super(arpaFile.getVocab().size(), arpaFile.getOrder());
+ super(Vocabulary.size(), arpaFile.getOrder());
int ngramCounts = arpaFile.size();
LOG.debug("ARPA file contains {} n-grams", ngramCounts);
- this.children = new HashMap<Long,Integer>(ngramCounts);
- this.logProbs = new HashMap<Long,Float>(ngramCounts);
- this.backoffs = new HashMap<Integer,Float>(ngramCounts);
+ this.children = new HashMap<>(ngramCounts);
+ this.logProbs = new HashMap<>(ngramCounts);
+ this.backoffs = new HashMap<>(ngramCounts);
int nodeCounter = 0;
@@ -265,8 +266,8 @@ public class TrieLM extends AbstractLM { //DefaultNGramLanguageModel {
Scanner scanner = new Scanner(new File(args[1]));
- LinkedList<String> wordList = new LinkedList<String>();
- LinkedList<String> window = new LinkedList<String>();
+ LinkedList<String> wordList = new LinkedList<>();
+ LinkedList<String> window = new LinkedList<>();
LOG.info("Starting to scan {}", args[1]);
while (scanner.hasNext()) {
@@ -279,15 +280,13 @@ public class TrieLM extends AbstractLM { //DefaultNGramLanguageModel {
wordList.clear();
wordList.add("<s>");
- for (String word : words) {
- wordList.add(word);
- }
+ Collections.addAll(wordList, words);
wordList.add("</s>");
- ArrayList<Integer> sentence = new ArrayList<Integer>();
+ ArrayList<Integer> sentence = new ArrayList<>();
// int[] ids = new int[wordList.size()];
- for (int i=0, size=wordList.size(); i<size; i++) {
- sentence.add(vocab.id(wordList.get(i)));
+ for (String aWordList : wordList) {
+ sentence.add(Vocabulary.id(aWordList));
// ids[i] = ;
}
@@ -310,7 +309,7 @@ public class TrieLM extends AbstractLM { //DefaultNGramLanguageModel {
int i=0;
int[] wordIDs = new int[window.size()];
for (String word : window) {
- wordIDs[i] = vocab.id(word);
+ wordIDs[i] = Vocabulary.id(word);
i++;
}
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/phrase/Distortion.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/phrase/Distortion.java b/src/main/java/org/apache/joshua/decoder/ff/phrase/Distortion.java
index f9e6a29..f37c139 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/phrase/Distortion.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/phrase/Distortion.java
@@ -47,7 +47,7 @@ public class Distortion extends StatelessFF {
public ArrayList<String> reportDenseFeatures(int index) {
denseFeatureIndex = index;
- ArrayList<String> names = new ArrayList<String>();
+ ArrayList<String> names = new ArrayList<>();
names.add(name);
return names;
}
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/similarity/EdgePhraseSimilarityFF.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/similarity/EdgePhraseSimilarityFF.java b/src/main/java/org/apache/joshua/decoder/ff/similarity/EdgePhraseSimilarityFF.java
index 91af58b..e5dcbf9 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/similarity/EdgePhraseSimilarityFF.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/similarity/EdgePhraseSimilarityFF.java
@@ -49,19 +49,17 @@ public class EdgePhraseSimilarityFF extends StatefulFF implements SourceDependen
private static final Logger LOG = LoggerFactory.getLogger(EdgePhraseSimilarityFF.class);
- private static Cache<String, Float> cache = new Cache<String, Float>(100000000);
+ private static final Cache<String, Float> cache = new Cache<>(100000000);
- private String host;
- private int port;
+ private final String host;
+ private final int port;
- private Socket socket;
private PrintWriter serverAsk;
private BufferedReader serverReply;
private int[] source;
private final int MAX_PHRASE_LENGTH = 4;
- private final int GAP = 0;
public EdgePhraseSimilarityFF(FeatureVector weights, String[] args, JoshuaConfiguration config) throws NumberFormatException, UnknownHostException, IOException {
super(weights, "EdgePhraseSimilarity", args, config);
@@ -74,7 +72,7 @@ public class EdgePhraseSimilarityFF extends StatefulFF implements SourceDependen
private void initializeConnection() throws NumberFormatException, IOException {
LOG.info("Opening connection.");
- socket = new Socket(host, port);
+ Socket socket = new Socket(host, port);
serverAsk = new PrintWriter(socket.getOutputStream(), true);
serverReply = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
@@ -109,7 +107,7 @@ public class EdgePhraseSimilarityFF extends StatefulFF implements SourceDependen
lm_state_size += state.getLeftLMStateWords().length + state.getRightLMStateWords().length;
}
- ArrayList<int[]> batch = new ArrayList<int[]>();
+ ArrayList<int[]> batch = new ArrayList<>();
// Build joined target string.
int[] join = new int[target.length + lm_state_size];
@@ -132,6 +130,7 @@ public class EdgePhraseSimilarityFF extends StatefulFF implements SourceDependen
// System.err.println();
for (int w : state.getLeftLMStateWords())
join[idx++] = w;
+ int GAP = 0;
join[idx++] = GAP;
gaps[num_gaps++] = idx;
// System.err.print("RIGHT: ");
@@ -210,7 +209,7 @@ public class EdgePhraseSimilarityFF extends StatefulFF implements SourceDependen
return 0.0f;
}
- private final int[] getSourcePhrase(int anchor) {
+ private int[] getSourcePhrase(int anchor) {
int idx;
int length = Math.min(anchor, MAX_PHRASE_LENGTH - 1)
+ Math.min(source.length - anchor, MAX_PHRASE_LENGTH - 1);
@@ -228,7 +227,7 @@ public class EdgePhraseSimilarityFF extends StatefulFF implements SourceDependen
float similarity = 0.0f;
int count = 0;
StringBuilder query = new StringBuilder();
- List<String> to_cache = new ArrayList<String>();
+ List<String> to_cache = new ArrayList<>();
query.append("xb");
for (int i = 0; i < batch.size(); i += 2) {
int[] source = batch.get(i);
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/state_maintenance/NgramDPState.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/state_maintenance/NgramDPState.java b/src/main/java/org/apache/joshua/decoder/ff/state_maintenance/NgramDPState.java
index b269bd9..ef72b3d 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/state_maintenance/NgramDPState.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/state_maintenance/NgramDPState.java
@@ -57,7 +57,7 @@ public class NgramDPState extends DPState {
return right;
}
- private final void assertLengths() {
+ private void assertLengths() {
if (left.length != right.length)
throw new RuntimeException("Unequal lengths in left and right state: < "
+ Vocabulary.getWords(left) + " | " + Vocabulary.getWords(right) + " >");
@@ -90,10 +90,10 @@ public class NgramDPState extends DPState {
StringBuilder sb = new StringBuilder();
sb.append("<");
for (int id : left)
- sb.append(" " + Vocabulary.word(id));
+ sb.append(" ").append(Vocabulary.word(id));
sb.append(" |");
for (int id : right)
- sb.append(" " + Vocabulary.word(id));
+ sb.append(" ").append(Vocabulary.word(id));
sb.append(" >");
return sb.toString();
}
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/AbstractGrammar.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/AbstractGrammar.java b/src/main/java/org/apache/joshua/decoder/ff/tm/AbstractGrammar.java
index 47a75df..3181bfa 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/AbstractGrammar.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/AbstractGrammar.java
@@ -165,10 +165,12 @@ public abstract class AbstractGrammar implements Grammar {
if (LOG.isDebugEnabled()) {
StringBuilder s = new StringBuilder();
for (Rule r : rules.getSortedRules(models)) {
- s.append("\n\t" + r.getLHS() + " ||| " + Arrays.toString(r.getFrench()) + " ||| "
- + Arrays.toString(r.getEnglish()) + " ||| " + r.getFeatureVector() + " ||| "
- + r.getEstimatedCost() + " " + r.getClass().getName() + "@"
- + Integer.toHexString(System.identityHashCode(r)));
+ s.append("\n\t").append(r.getLHS()).append(" ||| ")
+ .append(Arrays.toString(r.getFrench())).append(" ||| ")
+ .append(Arrays.toString(r.getEnglish())).append(" ||| ")
+ .append(r.getFeatureVector()).append(" ||| ").append(r.getEstimatedCost())
+ .append(" ").append(r.getClass().getName()).append("@")
+ .append(Integer.toHexString(System.identityHashCode(r)));
}
LOG.debug("{}", s);
}
@@ -203,7 +205,7 @@ public abstract class AbstractGrammar implements Grammar {
* Add OOV rules; This should be called after the manual constraints have
* been set up.
*/
- HashSet<Integer> words = new HashSet<Integer>();
+ HashSet<Integer> words = new HashSet<>();
for (Node<Token> node : inputLattice) {
for (Arc<Token> arc : node.getOutgoingArcs()) {
// create a rule, but do not add into the grammar trie
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/BasicRuleCollection.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/BasicRuleCollection.java b/src/main/java/org/apache/joshua/decoder/ff/tm/BasicRuleCollection.java
index 4cffb2f..4d577dc 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/BasicRuleCollection.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/BasicRuleCollection.java
@@ -56,7 +56,7 @@ public class BasicRuleCollection implements RuleCollection {
* @param sourceTokens Sequence of terminals and nonterminals in the source pattern
*/
public BasicRuleCollection(int arity, int[] sourceTokens) {
- this.rules = new ArrayList<Rule>();
+ this.rules = new ArrayList<>();
this.sourceTokens = sourceTokens;
this.arity = arity;
this.sorted = false;
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/CreateGlueGrammar.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/CreateGlueGrammar.java b/src/main/java/org/apache/joshua/decoder/ff/tm/CreateGlueGrammar.java
index ce1e7d1..2424a1e 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/CreateGlueGrammar.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/CreateGlueGrammar.java
@@ -48,7 +48,7 @@ public class CreateGlueGrammar {
private String grammarPath;
@Option(name = "--goal", aliases = {"-goal"}, required = false, usage = "specify custom GOAL symbol. Default: 'GOAL'")
- private String goalSymbol = cleanNonTerminal(new JoshuaConfiguration().goal_symbol);
+ private final String goalSymbol = cleanNonTerminal(new JoshuaConfiguration().goal_symbol);
/* Rule templates */
// [GOAL] ||| <s> ||| <s> ||| 0
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/GrammarReader.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/GrammarReader.java b/src/main/java/org/apache/joshua/decoder/ff/tm/GrammarReader.java
index df00255..a2d80be 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/GrammarReader.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/GrammarReader.java
@@ -39,7 +39,7 @@ public abstract class GrammarReader<R extends Rule> implements Iterable<R>, Iter
protected static String fieldDelimiter;
protected static String description;
- protected String fileName;
+ protected final String fileName;
protected LineReader reader;
protected String lookAhead;
protected int numRulesRead;
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/OwnerMap.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/OwnerMap.java b/src/main/java/org/apache/joshua/decoder/ff/tm/OwnerMap.java
index 16b8bfc..5d5ca9f 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/OwnerMap.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/OwnerMap.java
@@ -37,7 +37,7 @@ import com.google.common.collect.HashBiMap;
public class OwnerMap {
// bi-directional mapping between OwnerId and Owner strings
- private static BiMap<OwnerId, String> map = HashBiMap.create();
+ private static final BiMap<OwnerId, String> map = HashBiMap.create();
public static final OwnerId UNKNOWN_OWNER_ID = new OwnerId(0);
public static final String UNKNOWN_OWNER = "<unowned>";
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/Rule.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/Rule.java b/src/main/java/org/apache/joshua/decoder/ff/tm/Rule.java
index 15fbec1..95717de 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/Rule.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/Rule.java
@@ -107,7 +107,7 @@ public class Rule implements Comparator<Rule>, Comparable<Rule> {
this.arity = arity;
this.owner = owner;
this.target = target;
- this.sparseFeatureStringSupplier = Suppliers.memoize(() -> { return sparseFeatures; });
+ this.sparseFeatureStringSupplier = Suppliers.memoize(() -> sparseFeatures);
this.featuresSupplier = initializeFeatureSupplierFromString();
this.alignmentSupplier = initializeAlignmentSupplier();
}
@@ -127,7 +127,7 @@ public class Rule implements Comparator<Rule>, Comparable<Rule> {
this.arity = arity;
this.owner = owner;
this.target = targetRhs;
- this.featuresSupplier = Suppliers.memoize(() -> { return features; });
+ this.featuresSupplier = Suppliers.memoize(() -> features);
this.sparseFeatureStringSupplier = initializeSparseFeaturesStringSupplier();
this.alignmentSupplier = initializeAlignmentSupplier();
}
@@ -205,9 +205,7 @@ public class Rule implements Comparator<Rule>, Comparable<Rule> {
* If Rule was constructed with a FeatureVector, we lazily populate the sparseFeaturesStringSupplier.
*/
private Supplier<String> initializeSparseFeaturesStringSupplier() {
- return Suppliers.memoize(() -> {
- return getFeatureVector().toString();
- });
+ return Suppliers.memoize(() -> getFeatureVector().toString());
}
// ===============================================================
@@ -240,10 +238,7 @@ public class Rule implements Comparator<Rule>, Comparable<Rule> {
if (!Arrays.equals(getFrench(), other.getFrench())) {
return false;
}
- if (!Arrays.equals(target, other.getEnglish())) {
- return false;
- }
- return true;
+ return Arrays.equals(target, other.getEnglish());
}
public int hashCode() {
@@ -396,17 +391,16 @@ public class Rule implements Comparator<Rule>, Comparable<Rule> {
// ===============================================================
public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append(Vocabulary.word(this.getLHS()));
- sb.append(" ||| ");
- sb.append(getFrenchWords());
- sb.append(" ||| ");
- sb.append(getEnglishWords());
- sb.append(" |||");
- sb.append(" " + getFeatureVector());
- sb.append(String.format(" ||| est=%.3f", getEstimatedCost()));
- sb.append(String.format(" pre=%.3f", getPrecomputableCost()));
- return sb.toString();
+ String sb = Vocabulary.word(this.getLHS()) +
+ " ||| " +
+ getFrenchWords() +
+ " ||| " +
+ getEnglishWords() +
+ " |||" +
+ " " + getFeatureVector() +
+ String.format(" ||| est=%.3f", getEstimatedCost()) +
+ String.format(" pre=%.3f", getPrecomputableCost());
+ return sb;
}
/**
@@ -422,22 +416,24 @@ public class Rule implements Comparator<Rule>, Comparable<Rule> {
int nt = 1;
for (int i = 0; i < getFrench().length; i++) {
if (getFrench()[i] < 0)
- sb.append(" " + Vocabulary.word(getFrench()[i]).replaceFirst("\\]", String.format(",%d]", nt++)));
+ sb.append(" ").append(
+ Vocabulary.word(getFrench()[i]).replaceFirst("\\]", String.format(",%d]", nt++)));
else
- sb.append(" " + Vocabulary.word(getFrench()[i]));
+ sb.append(" ").append(Vocabulary.word(getFrench()[i]));
}
sb.append(" |||");
nt = 1;
for (int i = 0; i < getEnglish().length; i++) {
if (getEnglish()[i] < 0)
- sb.append(" " + Vocabulary.word(getEnglish()[i]).replaceFirst("\\]", String.format(",%d]", nt++)));
+ sb.append(" ").append(
+ Vocabulary.word(getEnglish()[i]).replaceFirst("\\]", String.format(",%d]", nt++)));
else
- sb.append(" " + Vocabulary.word(getEnglish()[i]));
+ sb.append(" ").append(Vocabulary.word(getEnglish()[i]));
}
sb.append(" |||");
- sb.append(" " + getFeatureString());
+ sb.append(" ").append(getFeatureString());
if (getAlignmentString() != null)
- sb.append(" ||| " + getAlignmentString());
+ sb.append(" ||| ").append(getAlignmentString());
return sb.toString();
}
@@ -473,7 +469,7 @@ public class Rule implements Comparator<Rule>, Comparable<Rule> {
StringBuilder sb = new StringBuilder();
for (Integer index : getEnglish()) {
if (index >= 0)
- sb.append(Vocabulary.word(index) + " ");
+ sb.append(Vocabulary.word(index)).append(" ");
else
sb.append(Vocabulary.word(foreignNTs[-index - 1]).replace("]",
String.format(",%d] ", Math.abs(index))));
@@ -527,14 +523,14 @@ public class Rule implements Comparator<Rule>, Comparable<Rule> {
*/
public Map<Integer, List<Integer>> getAlignmentMap() {
byte[] alignmentArray = getAlignment();
- Map<Integer, List<Integer>> alignmentMap = new HashMap<Integer, List<Integer>>();
+ Map<Integer, List<Integer>> alignmentMap = new HashMap<>();
if (alignmentArray != null) {
for (int alignmentIdx = 0; alignmentIdx < alignmentArray.length; alignmentIdx += 2 ) {
int s = alignmentArray[alignmentIdx];
int t = alignmentArray[alignmentIdx + 1];
List<Integer> values = alignmentMap.get(t);
if (values == null)
- alignmentMap.put(t, values = new ArrayList<Integer>());
+ alignmentMap.put(t, values = new ArrayList<>());
values.add(s);
}
}
@@ -603,22 +599,19 @@ public class Rule implements Comparator<Rule>, Comparable<Rule> {
* @return true if there is a match
*/
public boolean matches(Sentence sentence) {
- boolean match = getPattern().matcher(sentence.fullSource()).find();
// System.err.println(String.format("match(%s,%s) = %s", Pattern.quote(getFrenchWords()),
// sentence.annotatedSource(), match));
- return match;
+ return getPattern().matcher(sentence.fullSource()).find();
}
/**
* This comparator is used for sorting the rules during cube pruning. An estimate of the cost
* of each rule is computed and used to sort.
*/
- public static Comparator<Rule> EstimatedCostComparator = new Comparator<Rule>() {
- public int compare(Rule rule1, Rule rule2) {
- float cost1 = rule1.getEstimatedCost();
- float cost2 = rule2.getEstimatedCost();
- return Float.compare(cost2, cost1);
- }
+ public static final Comparator<Rule> EstimatedCostComparator = (rule1, rule2) -> {
+ float cost1 = rule1.getEstimatedCost();
+ float cost2 = rule2.getEstimatedCost();
+ return Float.compare(cost2, cost1);
};
public int compare(Rule rule1, Rule rule2) {
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/SentenceFilteredGrammar.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/SentenceFilteredGrammar.java b/src/main/java/org/apache/joshua/decoder/ff/tm/SentenceFilteredGrammar.java
index 54f68b2..4f545b7 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/SentenceFilteredGrammar.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/SentenceFilteredGrammar.java
@@ -40,10 +40,10 @@ public class SentenceFilteredGrammar extends MemoryBasedBatchGrammar {
private static final Logger LOG = LoggerFactory.getLogger(SentenceFilteredGrammar.class);
- private AbstractGrammar baseGrammar;
- private SentenceFilteredTrie filteredTrie;
- private int[] tokens;
- private Sentence sentence;
+ private final AbstractGrammar baseGrammar;
+ private final SentenceFilteredTrie filteredTrie;
+ private final int[] tokens;
+ private final Sentence sentence;
/**
* Construct a new sentence-filtered grammar. The main work is done in the enclosed trie (obtained
@@ -283,7 +283,7 @@ public class SentenceFilteredGrammar extends MemoryBasedBatchGrammar {
public class SentenceFilteredTrie implements Trie {
/* The underlying unfiltered trie node. */
- private Trie unfilteredTrieNode;
+ private final Trie unfilteredTrieNode;
/* The child nodes in the filtered trie. */
private HashMap<Integer, SentenceFilteredTrie> children = null;
@@ -295,7 +295,7 @@ public class SentenceFilteredGrammar extends MemoryBasedBatchGrammar {
*/
public SentenceFilteredTrie(Trie unfilteredTrieNode) {
this.unfilteredTrieNode = unfilteredTrieNode;
- this.children = new HashMap<Integer, SentenceFilteredTrie>();
+ this.children = new HashMap<>();
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/format/MosesFormatReader.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/format/MosesFormatReader.java b/src/main/java/org/apache/joshua/decoder/ff/tm/format/MosesFormatReader.java
index 7811b3b..39e045f 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/format/MosesFormatReader.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/format/MosesFormatReader.java
@@ -88,7 +88,7 @@ public class MosesFormatReader extends HieroFormatReader {
// alignments
if (fields.length >= 4)
- hieroLine.append(" ||| " + fields[3]);
+ hieroLine.append(" ||| ").append(fields[3]);
return super.parseLine(hieroLine.toString());
}
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/hash_based/ExtensionIterator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/hash_based/ExtensionIterator.java b/src/main/java/org/apache/joshua/decoder/ff/tm/hash_based/ExtensionIterator.java
index ecb355d..a29ad47 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/hash_based/ExtensionIterator.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/hash_based/ExtensionIterator.java
@@ -24,7 +24,7 @@ import java.util.Iterator;
public class ExtensionIterator implements Iterator<Integer> {
private Iterator<Integer> iterator;
- private boolean terminal;
+ private final boolean terminal;
private boolean done;
private int next;
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/hash_based/MemoryBasedBatchGrammar.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/hash_based/MemoryBasedBatchGrammar.java b/src/main/java/org/apache/joshua/decoder/ff/tm/hash_based/MemoryBasedBatchGrammar.java
index 234fe0f..97ac354 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/hash_based/MemoryBasedBatchGrammar.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/hash_based/MemoryBasedBatchGrammar.java
@@ -61,7 +61,7 @@ public class MemoryBasedBatchGrammar extends AbstractGrammar {
private int numDenseFeatures = 0;
/* The trie root. */
- private MemoryBasedTrie root = new MemoryBasedTrie();
+ private final MemoryBasedTrie root = new MemoryBasedTrie();
/* The file containing the grammar. */
private String grammarFile;
@@ -171,9 +171,7 @@ public class MemoryBasedBatchGrammar extends AbstractGrammar {
maxSourcePhraseLength = Math.max(maxSourcePhraseLength, french.length);
- for (int k = 0; k < french.length; k++) {
- int curSymID = french[k];
-
+ for (int curSymID : french) {
/*
* Note that the nonTerminal symbol in the french is not cleaned (i.e., will be sth like
* [X,1]), but the symbol in the Trie has to be cleaned, so that the match does not care about
@@ -186,7 +184,7 @@ public class MemoryBasedBatchGrammar extends AbstractGrammar {
if (null == nextLayer) {
nextLayer = new MemoryBasedTrie();
if (pos.hasExtensions() == false) {
- pos.childrenTbl = new HashMap<Integer, MemoryBasedTrie>();
+ pos.childrenTbl = new HashMap<>();
}
pos.childrenTbl.put(curSymID, nextLayer);
}
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/packed/PackedGrammar.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/packed/PackedGrammar.java b/src/main/java/org/apache/joshua/decoder/ff/tm/packed/PackedGrammar.java
index 37bffb7..f8173b8 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/packed/PackedGrammar.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/packed/PackedGrammar.java
@@ -121,7 +121,7 @@ public class PackedGrammar extends AbstractGrammar {
// Testing shows there's up to ~95% hit rate when cache size is 5000 Trie nodes.
private final Cache<Trie, List<Rule>> cached_rules;
- private String grammarDir;
+ private final String grammarDir;
public PackedGrammar(String grammar_dir, int span_limit, String owner, String type,
JoshuaConfiguration joshuaConfiguration) throws IOException {
@@ -150,7 +150,7 @@ public class PackedGrammar extends AbstractGrammar {
final List<String> listing = Arrays.asList(new File(grammar_dir).list());
sort(listing); // File.list() has arbitrary sort order
- slices = new ArrayList<PackedSlice>();
+ slices = new ArrayList<>();
for (String prefix : listing) {
if (prefix.startsWith("slice_") && prefix.endsWith(".source"))
slices.add(new PackedSlice(grammar_dir + File.separator + prefix.substring(0, 11)));
@@ -210,8 +210,8 @@ public class PackedGrammar extends AbstractGrammar {
byte[] digest = md.digest();
// convert the byte to hex format
StringBuffer sb = new StringBuffer("");
- for (int i = 0; i < digest.length; i++) {
- sb.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1));
+ for (byte aDigest : digest) {
+ sb.append(Integer.toString((aDigest & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
@@ -263,7 +263,7 @@ public class PackedGrammar extends AbstractGrammar {
* packedRoot.match() thus can directly return the result of lookup.get(id);
*/
if (!childTries.containsKey(id)) {
- childTries.put(id, new ArrayList<Trie>(1));
+ childTries.put(id, new ArrayList<>(1));
}
final Trie trie = packedSlice.root().match(id);
childTries.get(id).add(trie);
@@ -376,7 +376,7 @@ public class PackedGrammar extends AbstractGrammar {
alignments = null;
}
- tries = new HashMap<Integer, PackedTrie>();
+ tries = new HashMap<>();
}
/**
@@ -425,12 +425,11 @@ public class PackedGrammar extends AbstractGrammar {
try(FileInputStream fileInputStream = new FileInputStream(file)) {
FileChannel fileChannel = fileInputStream.getChannel();
int size = (int) fileChannel.size();
- MappedByteBuffer result = fileChannel.map(MapMode.READ_ONLY, 0, size);
- return result;
+ return fileChannel.map(MapMode.READ_ONLY, 0, size);
}
}
- private final int[] getTarget(int pointer) {
+ private int[] getTarget(int pointer) {
// Figure out level.
int tgt_length = 1;
while (tgt_length < (targetLookup.length + 1) && targetLookup[tgt_length] <= pointer)
@@ -474,7 +473,7 @@ public class PackedGrammar extends AbstractGrammar {
* @return feature vector
*/
- private final FeatureVector loadFeatureVector(int block_id) {
+ private FeatureVector loadFeatureVector(int block_id) {
int featurePosition = getIntFromByteBuffer(block_id, features);
final int numFeatures = encoding.readId(features, featurePosition);
@@ -508,7 +507,7 @@ public class PackedGrammar extends AbstractGrammar {
* getAlignments calls to PackedRule objects they could alter each other's positions within the
* buffer before calling read on the buffer.
*/
- private synchronized final byte[] getAlignmentArray(int block_id) {
+ private synchronized byte[] getAlignmentArray(int block_id) {
if (alignments == null)
throw new RuntimeException("No alignments available.");
int alignment_position = getIntFromByteBuffer(block_id, alignments);
@@ -530,7 +529,7 @@ public class PackedGrammar extends AbstractGrammar {
return alignment;
}
- private final PackedTrie root() {
+ private PackedTrie root() {
return getTrie(0);
}
@@ -551,7 +550,7 @@ public class PackedGrammar extends AbstractGrammar {
private boolean sorted = false;
- private int[] src;
+ private final int[] src;
private int arity;
private PackedTrie(int position) {
@@ -599,7 +598,7 @@ public class PackedGrammar extends AbstractGrammar {
@Override
public HashMap<Integer, ? extends Trie> getChildren() {
- HashMap<Integer, Trie> children = new HashMap<Integer, Trie>();
+ HashMap<Integer, Trie> children = new HashMap<>();
int num_children = source[position];
for (int i = 0; i < num_children; i++) {
int symbol = source[position + 1 + 2 * i];
@@ -617,7 +616,7 @@ public class PackedGrammar extends AbstractGrammar {
@Override
public ArrayList<? extends Trie> getExtensions() {
int num_children = source[position];
- ArrayList<PackedTrie> tries = new ArrayList<PackedTrie>(num_children);
+ ArrayList<PackedTrie> tries = new ArrayList<>(num_children);
for (int i = 0; i < num_children; i++) {
int symbol = source[position + 1 + 2 * i];
@@ -650,7 +649,7 @@ public class PackedGrammar extends AbstractGrammar {
int rule_position = position + 2 * (num_children + 1);
int num_rules = source[rule_position - 1];
- rules = new ArrayList<Rule>(num_rules);
+ rules = new ArrayList<>(num_rules);
for (int i = 0; i < num_rules; i++) {
rules.add(new PackedRule(rule_position + 3 * i));
}
@@ -691,26 +690,22 @@ public class PackedGrammar extends AbstractGrammar {
precomputable[block_id] = rule.getPrecomputableCost();
}
- Arrays.sort(rules, new Comparator<Integer>() {
- public int compare(Integer a, Integer b) {
- float a_cost = estimated[source[a]];
- float b_cost = estimated[source[b]];
- if (a_cost == b_cost)
- return 0;
- return (a_cost > b_cost ? -1 : 1);
- }
+ Arrays.sort(rules, (a, b) -> {
+ float a_cost = estimated[source[a]];
+ float b_cost = estimated[source[b]];
+ if (a_cost == b_cost)
+ return 0;
+ return (a_cost > b_cost ? -1 : 1);
});
int[] sorted = new int[3 * num_rules];
int j = 0;
- for (int i = 0; i < rules.length; i++) {
- int address = rules[i];
+ for (Integer address : rules) {
sorted[j++] = source[address - 2];
sorted[j++] = source[address - 1];
sorted[j++] = source[address];
}
- for (int i = 0; i < sorted.length; i++)
- source[rule_position + i] = sorted[i];
+ System.arraycopy(sorted, 0, source, rule_position + 0, sorted.length);
// Replace rules in cache with their sorted values on next getRules()
cached_rules.invalidate(this);
@@ -747,7 +742,7 @@ public class PackedGrammar extends AbstractGrammar {
public final class PackedChildIterator implements Iterator<Integer> {
private int current;
- private boolean terminal;
+ private final boolean terminal;
private boolean done;
private int last;
@@ -827,7 +822,7 @@ public class PackedGrammar extends AbstractGrammar {
*/
private Supplier<int[]> initializeEnglishSupplier(){
- Supplier<int[]> result = Suppliers.memoize(() ->{
+ return Suppliers.memoize(() ->{
int[] phrase = getTarget(source[address + 1]);
int[] tgt = new int[phrase.length + 1];
tgt[0] = -1;
@@ -835,11 +830,10 @@ public class PackedGrammar extends AbstractGrammar {
tgt[i+1] = phrase[i];
return tgt;
});
- return result;
}
private Supplier<byte[]> initializeAlignmentSupplier(){
- Supplier<byte[]> result = Suppliers.memoize(() ->{
+ return Suppliers.memoize(() ->{
byte[] raw_alignment = getAlignmentArray(source[address + 2]);
byte[] points = new byte[raw_alignment.length + 2];
points[0] = points[1] = 0;
@@ -847,7 +841,6 @@ public class PackedGrammar extends AbstractGrammar {
points[i + 2] = (byte) (raw_alignment[i] + 1);
return points;
});
- return result;
}
/**
@@ -904,28 +897,25 @@ public class PackedGrammar extends AbstractGrammar {
}
private Supplier<int[]> intializeEnglishSupplier(){
- Supplier<int[]> result = Suppliers.memoize(() ->{
+ return Suppliers.memoize(() ->{
return getTarget(source[address + 1]);
});
- return result;
}
private Supplier<FeatureVector> initializeFeatureVectorSupplier(){
- Supplier<FeatureVector> result = Suppliers.memoize(() ->{
+ return Suppliers.memoize(() ->{
return loadFeatureVector(source[address + 2]);
});
- return result;
}
private Supplier<byte[]> initializeAlignmentsSupplier(){
- Supplier<byte[]> result = Suppliers.memoize(()->{
+ return Suppliers.memoize(()->{
// if no alignments in grammar do not fail
if (alignments == null){
return null;
}
return getAlignmentArray(source[address + 2]);
});
- return result;
}
@Override
@@ -1010,16 +1000,15 @@ public class PackedGrammar extends AbstractGrammar {
@Override
public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append(Vocabulary.word(this.getLHS()));
- sb.append(" ||| ");
- sb.append(getFrenchWords());
- sb.append(" ||| ");
- sb.append(getEnglishWords());
- sb.append(" |||");
- sb.append(" " + getFeatureVector());
- sb.append(String.format(" ||| %.3f", getEstimatedCost()));
- return sb.toString();
+ String sb = Vocabulary.word(this.getLHS()) +
+ " ||| " +
+ getFrenchWords() +
+ " ||| " +
+ getEnglishWords() +
+ " |||" +
+ " " + getFeatureVector() +
+ String.format(" ||| %.3f", getEstimatedCost());
+ return sb;
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/ff/tm/packed/SliceAggregatingTrie.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/ff/tm/packed/SliceAggregatingTrie.java b/src/main/java/org/apache/joshua/decoder/ff/tm/packed/SliceAggregatingTrie.java
index c6d03a6..7ec55ee 100644
--- a/src/main/java/org/apache/joshua/decoder/ff/tm/packed/SliceAggregatingTrie.java
+++ b/src/main/java/org/apache/joshua/decoder/ff/tm/packed/SliceAggregatingTrie.java
@@ -194,7 +194,7 @@ public class SliceAggregatingTrie implements Trie, RuleCollection {
@Override
public boolean hasRules() {
- return trieWithRules == null ? false : trieWithRules.hasRules();
+ return trieWithRules != null && trieWithRules.hasRules();
}
@Override
@@ -215,7 +215,7 @@ public class SliceAggregatingTrie implements Trie, RuleCollection {
@Override
public boolean isSorted() {
- return !hasRules() ? false : trieWithRules.getRuleCollection().isSorted();
+ return hasRules() && trieWithRules.getRuleCollection().isSorted();
}
/*
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/hypergraph/AlignedSourceTokens.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/hypergraph/AlignedSourceTokens.java b/src/main/java/org/apache/joshua/decoder/hypergraph/AlignedSourceTokens.java
index 864b383..c0ca4fa 100644
--- a/src/main/java/org/apache/joshua/decoder/hypergraph/AlignedSourceTokens.java
+++ b/src/main/java/org/apache/joshua/decoder/hypergraph/AlignedSourceTokens.java
@@ -65,7 +65,7 @@ class AlignedSourceTokens extends LinkedList<Integer> {
* returns true if element was added.
*/
public boolean add(Integer x) {
- return isNull ? false : super.add(x);
+ return !isNull && super.add(x);
}
public boolean isNonTerminal() {
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/hypergraph/AllSpansWalker.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/hypergraph/AllSpansWalker.java b/src/main/java/org/apache/joshua/decoder/hypergraph/AllSpansWalker.java
index 1aad06f..3f1c504 100644
--- a/src/main/java/org/apache/joshua/decoder/hypergraph/AllSpansWalker.java
+++ b/src/main/java/org/apache/joshua/decoder/hypergraph/AllSpansWalker.java
@@ -32,10 +32,10 @@ import org.apache.joshua.corpus.Span;
*/
public class AllSpansWalker {
- private Set<Span> visitedSpans;
+ private final Set<Span> visitedSpans;
public AllSpansWalker() {
- visitedSpans = new HashSet<Span>();
+ visitedSpans = new HashSet<>();
}
/**
@@ -47,15 +47,12 @@ public class AllSpansWalker {
* implementation to do the walking
*/
public void walk(HGNode node, final WalkerFunction walker) {
- new ForestWalker().walk(node, new org.apache.joshua.decoder.hypergraph.WalkerFunction() {
- @Override
- public void apply(HGNode node, int index) {
- if (node != null) {
- Span span = new Span(node.i, node.j);
- if (!visitedSpans.contains(span)) {
- walker.apply(node, 0);
- visitedSpans.add(span);
- }
+ new ForestWalker().walk(node, (node1, index) -> {
+ if (node1 != null) {
+ Span span = new Span(node1.i, node1.j);
+ if (!visitedSpans.contains(span)) {
+ walker.apply(node1, 0);
+ visitedSpans.add(span);
}
}
});
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/hypergraph/DefaultInsideOutside.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/hypergraph/DefaultInsideOutside.java b/src/main/java/org/apache/joshua/decoder/hypergraph/DefaultInsideOutside.java
index c6dae77..d53674b 100644
--- a/src/main/java/org/apache/joshua/decoder/hypergraph/DefaultInsideOutside.java
+++ b/src/main/java/org/apache/joshua/decoder/hypergraph/DefaultInsideOutside.java
@@ -40,16 +40,16 @@ public abstract class DefaultInsideOutside {
* a derivation is a multi of all constituents
*/
int ADD_MODE = 0; // 0: sum; 1: viterbi-min, 2: viterbi-max
- int LOG_SEMIRING = 1;
+ final int LOG_SEMIRING = 1;
int SEMIRING = LOG_SEMIRING; // default is in log; or real, or logic
double ZERO_IN_SEMIRING = Double.NEGATIVE_INFINITY;// log-domain
double ONE_IN_SEMIRING = 0;// log-domain
double scaling_factor; // try to scale the original distribution: smooth or winner-take-all
- private HashMap<HGNode, Double> tbl_inside_prob = new HashMap<HGNode, Double>();// remember inside
+ private final HashMap<HGNode, Double> tbl_inside_prob = new HashMap<>();// remember inside
// prob of each
// item:
- private HashMap<HGNode, Double> tbl_outside_prob = new HashMap<HGNode, Double>();// remember
+ private final HashMap<HGNode, Double> tbl_outside_prob = new HashMap<>();// remember
// outside prob
// of each item
double normalizationConstant = ONE_IN_SEMIRING;
@@ -61,7 +61,7 @@ public abstract class DefaultInsideOutside {
* because the outside estimation of the items under its deductions require the item's outside
* value
*/
- private HashMap<HGNode, Integer> tbl_num_parent_deductions = new HashMap<HGNode, Integer>();
+ private final HashMap<HGNode, Integer> tbl_num_parent_deductions = new HashMap<>();
private HashMap<HGNode, Integer> tbl_for_sanity_check = null;
@@ -111,13 +111,13 @@ public abstract class DefaultInsideOutside {
// without normalization
public double getEdgeUnormalizedPosteriorLogProb(HyperEdge dt, HGNode parent) {
// ### outside of parent
- double outside = (Double) tbl_outside_prob.get(parent);
+ double outside = tbl_outside_prob.get(parent);
// ### get inside prob of all my ant-items
double inside = ONE_IN_SEMIRING;
if (dt.getTailNodes() != null) {
for (HGNode ant_it : dt.getTailNodes())
- inside = multi_in_semiring(inside, (Double) tbl_inside_prob.get(ant_it));
+ inside = multi_in_semiring(inside, tbl_inside_prob.get(ant_it));
}
// ### add deduction/rule specific prob
@@ -145,8 +145,8 @@ public abstract class DefaultInsideOutside {
// without normalization
public double getNodeUnnormalizedPosteriorLogProb(HGNode node) {
// ### outside of parent
- double inside = (Double) tbl_inside_prob.get(node);
- double outside = (Double) tbl_outside_prob.get(node);
+ double inside = tbl_inside_prob.get(node);
+ double outside = tbl_outside_prob.get(node);
return multi_in_semiring(inside, outside);
}
@@ -170,7 +170,7 @@ public abstract class DefaultInsideOutside {
* However, this won't work! The sum should be greater than 1.
*/
public void sanityCheckHG(HyperGraph hg) {
- tbl_for_sanity_check = new HashMap<HGNode, Integer>();
+ tbl_for_sanity_check = new HashMap<>();
// System.out.println("num_dts: " + hg.goal_item.l_deductions.size());
sanity_check_item(hg.goalNode);
System.out.println("survied sanity check!!!!");
@@ -196,9 +196,7 @@ public abstract class DefaultInsideOutside {
private void sanity_check_deduction(HyperEdge dt) {
// ### recursive call on each ant item
if (null != dt.getTailNodes()) {
- for (HGNode ant_it : dt.getTailNodes()) {
- sanity_check_item(ant_it);
- }
+ dt.getTailNodes().forEach(this::sanity_check_item);
}
// ### deduction-specific operation
@@ -218,7 +216,7 @@ public abstract class DefaultInsideOutside {
private double inside_estimation_item(HGNode it) {
// ### get number of deductions that point to me
- Integer num_called = (Integer) tbl_num_parent_deductions.get(it);
+ Integer num_called = tbl_num_parent_deductions.get(it);
if (null == num_called) {
tbl_num_parent_deductions.put(it, 1);
} else {
@@ -226,7 +224,7 @@ public abstract class DefaultInsideOutside {
}
if (tbl_inside_prob.containsKey(it)) {
- return (Double) tbl_inside_prob.get(it);
+ return tbl_inside_prob.get(it);
}
double inside_prob = ZERO_IN_SEMIRING;
@@ -269,7 +267,7 @@ public abstract class DefaultInsideOutside {
private void outside_estimation_item(HGNode cur_it, HGNode upper_item, HyperEdge parent_dt,
double parent_deduct_prob) {
- Integer num_called = (Integer) tbl_num_parent_deductions.get(cur_it);
+ Integer num_called = tbl_num_parent_deductions.get(cur_it);
if (null == num_called || 0 == num_called) {
throw new RuntimeException("un-expected call, must be wrong");
}
@@ -277,7 +275,7 @@ public abstract class DefaultInsideOutside {
double old_outside_prob = ZERO_IN_SEMIRING;
if (tbl_outside_prob.containsKey(cur_it)) {
- old_outside_prob = (Double) tbl_outside_prob.get(cur_it);
+ old_outside_prob = tbl_outside_prob.get(cur_it);
}
double additional_outside_prob = ONE_IN_SEMIRING;
@@ -289,13 +287,13 @@ public abstract class DefaultInsideOutside {
if (parent_dt.getTailNodes() != null && parent_dt.getTailNodes().size() > 1)
for (HGNode ant_it : parent_dt.getTailNodes()) {
if (ant_it != cur_it) {
- double inside_prob_item = (Double) tbl_inside_prob.get(ant_it);// inside prob
+ double inside_prob_item = tbl_inside_prob.get(ant_it);// inside prob
additional_outside_prob = multi_in_semiring(additional_outside_prob, inside_prob_item);
}
}
// ### upper item
- double outside_prob_item = (Double) tbl_outside_prob.get(upper_item);// outside prob
+ double outside_prob_item = tbl_outside_prob.get(upper_item);// outside prob
additional_outside_prob = multi_in_semiring(additional_outside_prob, outside_prob_item);
// #### add to old prob
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/hypergraph/ForestWalker.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/hypergraph/ForestWalker.java b/src/main/java/org/apache/joshua/decoder/hypergraph/ForestWalker.java
index e58670a..34eb5b9 100644
--- a/src/main/java/org/apache/joshua/decoder/hypergraph/ForestWalker.java
+++ b/src/main/java/org/apache/joshua/decoder/hypergraph/ForestWalker.java
@@ -31,20 +31,20 @@ import java.util.Set;
*/
public class ForestWalker {
- public static enum TRAVERSAL {
+ public enum TRAVERSAL {
PREORDER, POSTORDER
- };
+ }
- private Set<HGNode> visitedNodes;
+ private final Set<HGNode> visitedNodes;
private TRAVERSAL traversalType = TRAVERSAL.PREORDER;
public ForestWalker() {
- visitedNodes = new HashSet<HGNode>();
+ visitedNodes = new HashSet<>();
}
public ForestWalker(TRAVERSAL traversal) {
this.traversalType = traversal;
- visitedNodes = new HashSet<HGNode>();
+ visitedNodes = new HashSet<>();
}
public void walk(HGNode node, WalkerFunction walker) {
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/hypergraph/GrammarBuilderWalkerFunction.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/hypergraph/GrammarBuilderWalkerFunction.java b/src/main/java/org/apache/joshua/decoder/hypergraph/GrammarBuilderWalkerFunction.java
index c5d2398..ab81162 100644
--- a/src/main/java/org/apache/joshua/decoder/hypergraph/GrammarBuilderWalkerFunction.java
+++ b/src/main/java/org/apache/joshua/decoder/hypergraph/GrammarBuilderWalkerFunction.java
@@ -48,17 +48,17 @@ public class GrammarBuilderWalkerFunction implements WalkerFunction {
private static final Logger LOG = LoggerFactory.getLogger(GrammarBuilderWalkerFunction.class);
- private MemoryBasedBatchGrammar grammar;
- private static HieroFormatReader reader = new HieroFormatReader();
+ private final MemoryBasedBatchGrammar grammar;
+ private static final HieroFormatReader reader = new HieroFormatReader();
private PrintStream outStream;
- private int goalSymbol;
- private HashSet<Rule> rules;
+ private final int goalSymbol;
+ private final HashSet<Rule> rules;
public GrammarBuilderWalkerFunction(String goal,JoshuaConfiguration joshuaConfiguration) {
grammar = new MemoryBasedBatchGrammar(reader, joshuaConfiguration, 1000);
outStream = null;
goalSymbol = Vocabulary.id(goal);
- rules = new HashSet<Rule>();
+ rules = new HashSet<>();
}
public GrammarBuilderWalkerFunction(String goal, PrintStream out,JoshuaConfiguration joshuaConfiguration) {
@@ -104,10 +104,8 @@ public class GrammarBuilderWalkerFunction implements WalkerFunction {
// if this would be unary abstract, getNewSource will be null
if (source == null) return null;
int[] target = getNewTargetFromSource(source);
- Rule result =
- new Rule(headLabel, source, target, edgeRule.getFeatureString(), edgeRule.getArity());
// System.err.printf("new rule is %s\n", result);
- return result;
+ return new Rule(headLabel, source, target, edgeRule.getFeatureString(), edgeRule.getArity());
}
private static int[] getNewSource(boolean isGlue, HyperEdge edge) {
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/hypergraph/HGNode.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/hypergraph/HGNode.java b/src/main/java/org/apache/joshua/decoder/hypergraph/HGNode.java
index 695cad5..23f4247 100644
--- a/src/main/java/org/apache/joshua/decoder/hypergraph/HGNode.java
+++ b/src/main/java/org/apache/joshua/decoder/hypergraph/HGNode.java
@@ -36,10 +36,11 @@ import org.apache.joshua.decoder.ff.state_maintenance.DPState;
public class HGNode {
- public int i, j;
+ public final int i;
+ public final int j;
// this is the symbol like: NP, VP, and so on
- public int lhs;
+ public final int lhs;
// each hyperedge is an "and" node
public List<HyperEdge> hyperedges = null;
@@ -49,7 +50,7 @@ public class HGNode {
// the key is the state id; remember the state required by each model, for example, edge-ngrams
// for LM model
- protected List<DPState> dpStates;
+ protected final List<DPState> dpStates;
private Signature signature = null;
// private int hash = 0;
@@ -99,7 +100,7 @@ public class HGNode {
public void addHyperedgeInNode(HyperEdge hyperEdge) {
if (hyperEdge != null) {
if (null == hyperedges)
- hyperedges = new ArrayList<HyperEdge>();
+ hyperedges = new ArrayList<>();
hyperedges.add(hyperEdge);
// Update the cache of this node's best incoming edge.
semiringPlus(hyperEdge);
@@ -112,8 +113,7 @@ public class HGNode {
* to add to the current HGNode.
*/
public void addHyperedgesInNode(List<HyperEdge> hyperedges) {
- for (HyperEdge hyperEdge : hyperedges)
- addHyperedgeInNode(hyperEdge);
+ hyperedges.forEach(this::addHyperedgeInNode);
}
/**
@@ -273,34 +273,30 @@ public class HGNode {
}
};
- public static Comparator<HGNode> inverseLogPComparator = new Comparator<HGNode>() {
- public int compare(HGNode item1, HGNode item2) {
- float logp1 = item1.score;
- float logp2 = item2.score;
- if (logp1 > logp2) {
- return -1;
- } else if (logp1 == logp2) {
- return 0;
- } else {
- return 1;
- }
+ public static final Comparator<HGNode> inverseLogPComparator = (item1, item2) -> {
+ float logp1 = item1.score;
+ float logp2 = item2.score;
+ if (logp1 > logp2) {
+ return -1;
+ } else if (logp1 == logp2) {
+ return 0;
+ } else {
+ return 1;
}
};
/**
* natural order
* */
- public static Comparator<HGNode> logPComparator = new Comparator<HGNode>() {
- public int compare(HGNode item1, HGNode item2) {
- float logp1 = item1.score;
- float logp2 = item2.score;
- if (logp1 > logp2) {
- return 1;
- } else if (logp1 == logp2) {
- return 0;
- } else {
- return -1;
- }
+ public static Comparator<HGNode> logPComparator = (item1, item2) -> {
+ float logp1 = item1.score;
+ float logp2 = item2.score;
+ if (logp1 > logp2) {
+ return 1;
+ } else if (logp1 == logp2) {
+ return 0;
+ } else {
+ return -1;
}
};
@@ -311,7 +307,7 @@ public class HGNode {
bestHyperedge.getBestDerivationScore()));
if (dpStates != null)
for (DPState state : dpStates)
- sb.append(" <" + state + ">");
+ sb.append(" <").append(state).append(">");
// if (this.hyperedges != null) {
// sb.append(" hyperedges: " + hyperedges.size());
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/hypergraph/HyperEdge.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/hypergraph/HyperEdge.java b/src/main/java/org/apache/joshua/decoder/hypergraph/HyperEdge.java
index b188650..a55dac6 100644
--- a/src/main/java/org/apache/joshua/decoder/hypergraph/HyperEdge.java
+++ b/src/main/java/org/apache/joshua/decoder/hypergraph/HyperEdge.java
@@ -43,7 +43,7 @@ public class HyperEdge {
* */
private float transitionScore;
- private Rule rule;
+ private final Rule rule;
private SourcePath srcPath = null;
@@ -94,8 +94,6 @@ public class HyperEdge {
}
public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(this.rule);
- return sb.toString();
+ return String.valueOf(this.rule);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/hypergraph/HyperGraph.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/hypergraph/HyperGraph.java b/src/main/java/org/apache/joshua/decoder/hypergraph/HyperGraph.java
index 499d4f3..6c59e9b 100644
--- a/src/main/java/org/apache/joshua/decoder/hypergraph/HyperGraph.java
+++ b/src/main/java/org/apache/joshua/decoder/hypergraph/HyperGraph.java
@@ -81,7 +81,7 @@ public class HyperGraph {
this.hg = hg;
this.hg.numNodes = 0;
this.hg.numEdges = 0;
- this.nodesVisited = new HashSet<HGNode>();
+ this.nodesVisited = new HashSet<>();
}
@Override
@@ -102,12 +102,12 @@ public class HyperGraph {
private List<FeatureFunction> model = null;
private PrintWriter out = null;
- private HashMap<HGNode, Integer> nodeMap;
+ private final HashMap<HGNode, Integer> nodeMap;
public HyperGraphDumper(PrintWriter out, List<FeatureFunction> model) {
this.out = out;
this.model = model;
- this.nodeMap = new HashMap<HGNode, Integer>();
+ this.nodeMap = new HashMap<>();
}
@Override
@@ -117,21 +117,19 @@ public class HyperGraph {
if (node.hyperedges.size() != 0 && node.bestHyperedge.getRule() != null) {
out.println(this.node_number);
- for (HyperEdge e: node.hyperedges) {
- if (e.getRule() != null) {
- for (int id: e.getRule().getEnglish()) {
- if (id < 0) {
- out.print(String.format("[%d] ", nodeMap.get(e.getTailNodes().get(-id-1))));
- } else {
- out.print(String.format("%s ", Vocabulary.word(id)));
- }
+ node.hyperedges.stream().filter(e -> e.getRule() != null).forEach(e -> {
+ for (int id : e.getRule().getEnglish()) {
+ if (id < 0) {
+ out.print(String.format("[%d] ", nodeMap.get(e.getTailNodes().get(-id - 1))));
+ } else {
+ out.print(String.format("%s ", Vocabulary.word(id)));
}
-
- FeatureVector edgeFeatures = ComputeNodeResult.computeTransitionFeatures(
- model, e, node.i, node.j, sentence);
- out.println(String.format("||| %s", edgeFeatures));
}
- }
+
+ FeatureVector edgeFeatures = ComputeNodeResult
+ .computeTransitionFeatures(model, e, node.i, node.j, sentence);
+ out.println(String.format("||| %s", edgeFeatures));
+ });
}
this.node_number++;
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/hypergraph/HyperGraphPruning.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/hypergraph/HyperGraphPruning.java b/src/main/java/org/apache/joshua/decoder/hypergraph/HyperGraphPruning.java
index 51bd9d6..8f67f1b 100644
--- a/src/main/java/org/apache/joshua/decoder/hypergraph/HyperGraphPruning.java
+++ b/src/main/java/org/apache/joshua/decoder/hypergraph/HyperGraphPruning.java
@@ -31,7 +31,7 @@ import org.apache.joshua.corpus.Vocabulary;
*/
public class HyperGraphPruning extends TrivialInsideOutside {
- HashMap<HGNode, Boolean> processedNodesTbl = new HashMap<HGNode, Boolean>();
+ final HashMap<HGNode, Boolean> processedNodesTbl = new HashMap<>();
double bestLogProb;// viterbi unnormalized log prob in the hypergraph
boolean ViterbiPruning = false;// Viterbi or Posterior pruning
@@ -147,10 +147,9 @@ public class HyperGraphPruning extends TrivialInsideOutside {
// ### still survive, recursive call all my ant-items
if (null != dt.getTailNodes()) {
- for (HGNode ant_it : dt.getTailNodes()) {
- pruningNode(ant_it); // recursive call on each ant item, note: the ant_it will not be pruned
- // as I need it
- }
+ // recursive call on each ant item, note: the ant_it will not be pruned
+ // as I need it
+ dt.getTailNodes().forEach(this::pruningNode);
}
// ### if get to here, then survive; remember: if I survive, then my upper-item must survive
http://git-wip-us.apache.org/repos/asf/incubator-joshua/blob/029cbbcc/src/main/java/org/apache/joshua/decoder/hypergraph/KBestExtractor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/joshua/decoder/hypergraph/KBestExtractor.java b/src/main/java/org/apache/joshua/decoder/hypergraph/KBestExtractor.java
index 8fc55df..47b2b83 100644
--- a/src/main/java/org/apache/joshua/decoder/hypergraph/KBestExtractor.java
+++ b/src/main/java/org/apache/joshua/decoder/hypergraph/KBestExtractor.java
@@ -96,7 +96,7 @@ import org.apache.joshua.decoder.StructuredTranslationFactory;
public class KBestExtractor {
private final JoshuaConfiguration joshuaConfiguration;
private final String outputFormat;
- private final HashMap<HGNode, VirtualNode> virtualNodesTable = new HashMap<HGNode, VirtualNode>();
+ private final HashMap<HGNode, VirtualNode> virtualNodesTable = new HashMap<>();
// static final String rootSym = JoshuaConfiguration.goal_symbol;
static final String rootSym = "ROOT";
@@ -104,7 +104,7 @@ public class KBestExtractor {
private enum Side {
SOURCE, TARGET
- };
+ }
/* Whether to extract only unique strings */
private final boolean extractUniqueNbest;
@@ -385,7 +385,7 @@ public class KBestExtractor {
HGNode node = null;
// sorted ArrayList of DerivationState, in the paper is: D(^) [v]
- public List<DerivationState> nbests = new ArrayList<DerivationState>();
+ public final List<DerivationState> nbests = new ArrayList<>();
// remember frontier states, best-first; in the paper, it is called cand[v]
private PriorityQueue<DerivationState> candHeap = null;
@@ -490,13 +490,11 @@ public class KBestExtractor {
/* For each tail node, create a new state candidate by "sliding" that item one position. */
for (int i = 0; i < previousState.edge.getTailNodes().size(); i++) {
/* Create a new virtual node that is a copy of the current node */
- HGNode tailNode = (HGNode) previousState.edge.getTailNodes().get(i);
+ HGNode tailNode = previousState.edge.getTailNodes().get(i);
VirtualNode virtualTailNode = kbestExtractor.getVirtualNode(tailNode);
// Copy over the ranks.
int[] newRanks = new int[previousState.ranks.length];
- for (int c = 0; c < newRanks.length; c++) {
- newRanks[c] = previousState.ranks[c];
- }
+ System.arraycopy(previousState.ranks, 0, newRanks, 0, newRanks.length);
// Now increment/slide the current tail node by one
newRanks[i] = previousState.ranks[i] + 1;
@@ -538,7 +536,7 @@ public class KBestExtractor {
*/
private void getCandidates(KBestExtractor kbestExtractor) {
/* The list of candidates extending from this (virtual) node. */
- candHeap = new PriorityQueue<DerivationState>(11, new DerivationStateComparator());
+ candHeap = new PriorityQueue<>(11, new DerivationStateComparator());
/*
* When exploring the cube frontier, there are multiple paths to each candidate. For example,
@@ -549,14 +547,14 @@ public class KBestExtractor {
* TODO: these should really be keyed on the states themselves instead of a string
* representation of them.
*/
- derivationTable = new HashSet<DerivationState>();
+ derivationTable = new HashSet<>();
/*
* A Joshua configuration option allows the decoder to output only unique strings. In that
* case, we keep an list of the frontiers of derivation states extending from this node.
*/
if (extractUniqueNbest) {
- uniqueStringsTable = new HashSet<String>();
+ uniqueStringsTable = new HashSet<>();
}
/*
@@ -629,7 +627,7 @@ public class KBestExtractor {
childVirtualNode.lazyKBestExtractOnNode(kbestExtractor, ranks[i]);
}
}
- cost = (float) hyperEdge.getBestDerivationScore();
+ cost = hyperEdge.getBestDerivationScore();
DerivationState state = new DerivationState(parentNode, hyperEdge, ranks, cost, edgePos);
if (joshuaConfiguration.rescoreForest)
@@ -637,7 +635,7 @@ public class KBestExtractor {
return state;
}
- };
+ }
/**
* A DerivationState describes which path to follow through the hypergraph. For example, it
@@ -651,22 +649,22 @@ public class KBestExtractor {
// each DerivationState roughly corresponds to a hypothesis
public class DerivationState {
/* The edge ("e" in the paper) */
- public HyperEdge edge;
+ public final HyperEdge edge;
/* The edge's parent node */
- public HGNode parentNode;
+ public final HGNode parentNode;
/*
* This state's position in its parent node's list of incoming hyperedges (used in signature
* calculation)
*/
- public int edgePos;
+ public final int edgePos;
/*
* The rank item to select from each of the incoming tail nodes ("j" in the paper, an ArrayList
* of size |e|)
*/
- public int[] ranks;
+ public final int[] ranks;
/*
* The cost of the hypothesis, including a weighted BLEU score, if any.
@@ -746,9 +744,9 @@ public class KBestExtractor {
Vocabulary.word(parentNode.lhs), parentNode.i, parentNode.j, edgePos));
sb.append("ranks=[ ");
if (ranks != null)
- for (int i = 0; i < ranks.length; i++)
- sb.append(ranks[i] + " ");
- sb.append("] ||| " + String.format("%.5f ]]", cost));
+ for (int rank : ranks)
+ sb.append(rank + " ");
+ sb.append("] ||| ").append(String.format("%.5f ]]", cost));
return sb.toString();
}
@@ -1002,7 +1000,7 @@ public class KBestExtractor {
*/
public class DerivationExtractor implements DerivationVisitor {
- StringBuffer sb;
+ final StringBuffer sb;
public DerivationExtractor() {
sb = new StringBuffer();
@@ -1026,16 +1024,17 @@ public class KBestExtractor {
// sb.append(rule).append(" ||| " + features + " ||| " +
// KBestExtractor.this.weights.innerProduct(features));
sb.append(String.format("%d-%d", state.parentNode.i, state.parentNode.j));
- sb.append(" ||| " + Vocabulary.word(rule.getLHS()) + " -> "
- + Vocabulary.getWords(rule.getFrench()) + " /// " + rule.getEnglishWords());
+ sb.append(" ||| ").append(Vocabulary.word(rule.getLHS())).append(" -> ")
+ .append(Vocabulary.getWords(rule.getFrench())).append(" /// ")
+ .append(rule.getEnglishWords());
sb.append(" |||");
for (DPState dpState : state.parentNode.getDPStates()) {
- sb.append(" " + dpState);
+ sb.append(" ").append(dpState);
}
- sb.append(" ||| " + transitionFeatures);
- sb.append(" ||| " + weights.innerProduct(transitionFeatures));
+ sb.append(" ||| ").append(transitionFeatures);
+ sb.append(" ||| ").append(weights.innerProduct(transitionFeatures));
if (rule.getAlignment() != null)
- sb.append(" ||| " + Arrays.toString(rule.getAlignment()));
+ sb.append(" ||| ").append(Arrays.toString(rule.getAlignment()));
sb.append("\n");
}
}