You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opennlp.apache.org by jo...@apache.org on 2017/01/06 21:07:35 UTC
[6/9] opennlp git commit: Correct indentation and white spaces
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ArrayMath.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ArrayMath.java b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ArrayMath.java
index dd4e03d..5cd70e6 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ArrayMath.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ArrayMath.java
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package opennlp.tools.ml.maxent.quasinewton;
import java.util.List;
@@ -86,7 +87,7 @@ public class ArrayMath {
* Find index of maximum element in the vector x
* @param x input vector
* @return index of the maximum element. Index of the first
- * maximum element is returned if multiple maximums are found.
+ * maximum element is returned if multiple maximums are found.
*/
public static int maxIdx(double[] x) {
if (x == null || x.length == 0) {
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/Function.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/Function.java b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/Function.java
index 1106200..c7ac53f 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/Function.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/Function.java
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package opennlp.tools.ml.maxent.quasinewton;
/**
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/LineSearch.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/LineSearch.java b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/LineSearch.java
index 80c839b..6ce817b 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/LineSearch.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/LineSearch.java
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package opennlp.tools.ml.maxent.quasinewton;
/**
@@ -101,7 +102,7 @@ public class LineSearch {
// New sign vector
for (int i = 0; i < dimension; i++) {
- signX[i] = x[i] == 0? -pseudoGradAtX[i] : x[i];
+ signX[i] = x[i] == 0 ? -pseudoGradAtX[i] : x[i];
}
while (true) {
@@ -176,7 +177,7 @@ public class LineSearch {
int fctEvalCount)
{
setAll(stepSize, valueAtCurr, valueAtNext, gradAtCurr, gradAtNext,
- currPoint, nextPoint, fctEvalCount);
+ currPoint, nextPoint, fctEvalCount);
}
/**
@@ -195,7 +196,7 @@ public class LineSearch {
int fctEvalCount)
{
setAll(stepSize, valueAtCurr, valueAtNext, gradAtCurr, gradAtNext,
- pseudoGradAtNext, currPoint, nextPoint, signVector, fctEvalCount);
+ pseudoGradAtNext, currPoint, nextPoint, signVector, fctEvalCount);
}
/**
@@ -249,6 +250,7 @@ public class LineSearch {
public double getStepSize() {
return stepSize;
}
+
public void setStepSize(double stepSize) {
this.stepSize = stepSize;
}
@@ -256,6 +258,7 @@ public class LineSearch {
public double getValueAtCurr() {
return valueAtCurr;
}
+
public void setValueAtCurr(double valueAtCurr) {
this.valueAtCurr = valueAtCurr;
}
@@ -263,6 +266,7 @@ public class LineSearch {
public double getValueAtNext() {
return valueAtNext;
}
+
public void setValueAtNext(double valueAtNext) {
this.valueAtNext = valueAtNext;
}
@@ -270,6 +274,7 @@ public class LineSearch {
public double[] getGradAtCurr() {
return gradAtCurr;
}
+
public void setGradAtCurr(double[] gradAtCurr) {
this.gradAtCurr = gradAtCurr;
}
@@ -277,6 +282,7 @@ public class LineSearch {
public double[] getGradAtNext() {
return gradAtNext;
}
+
public void setGradAtNext(double[] gradAtNext) {
this.gradAtNext = gradAtNext;
}
@@ -284,6 +290,7 @@ public class LineSearch {
public double[] getPseudoGradAtNext() {
return pseudoGradAtNext;
}
+
public void setPseudoGradAtNext(double[] pseudoGradAtNext) {
this.pseudoGradAtNext = pseudoGradAtNext;
}
@@ -291,6 +298,7 @@ public class LineSearch {
public double[] getCurrPoint() {
return currPoint;
}
+
public void setCurrPoint(double[] currPoint) {
this.currPoint = currPoint;
}
@@ -298,6 +306,7 @@ public class LineSearch {
public double[] getNextPoint() {
return nextPoint;
}
+
public void setNextPoint(double[] nextPoint) {
this.nextPoint = nextPoint;
}
@@ -305,6 +314,7 @@ public class LineSearch {
public double[] getSignVector() {
return signVector;
}
+
public void setSignVector(double[] signVector) {
this.signVector = signVector;
}
@@ -312,12 +322,13 @@ public class LineSearch {
public int getFctEvalCount() {
return fctEvalCount;
}
+
public void setFctEvalCount(int fctEvalCount) {
this.fctEvalCount = fctEvalCount;
}
/**
- * Initial linear search object
+ * Initial linear search object.
*/
public static LineSearchResult getInitialObject(
double valueAtX,
@@ -328,7 +339,7 @@ public class LineSearch {
}
/**
- * Initial linear search object for L1-regularization
+ * Initial linear search object for L1-regularization.
*/
public static LineSearchResult getInitialObjectForL1(
double valueAtX,
@@ -345,8 +356,7 @@ public class LineSearch {
double[] pseudoGradAtX,
double[] x,
double[] signX,
- int fctEvalCount)
- {
+ int fctEvalCount) {
return new LineSearchResult(0.0, 0.0, valueAtX, new double[x.length], gradAtX,
pseudoGradAtX, new double[x.length], x, signX, fctEvalCount);
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihood.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihood.java b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihood.java
index dbe8baf..1c20369 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihood.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/NegLogLikelihood.java
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package opennlp.tools.ml.maxent.quasinewton;
import java.util.Arrays;
@@ -94,7 +95,7 @@ public class NegLogLikelihood implements Function {
tempSums[oi] = 0;
for (ai = 0; ai < contexts[ci].length; ai++) {
vectorIndex = indexOf(oi, contexts[ci][ai]);
- predValue = values != null? values[ci][ai] : 1.0;
+ predValue = values != null ? values[ci][ai] : 1.0;
tempSums[oi] += predValue * x[vectorIndex];
}
}
@@ -129,7 +130,7 @@ public class NegLogLikelihood implements Function {
expectation[oi] = 0;
for (ai = 0; ai < contexts[ci].length; ai++) {
vectorIndex = indexOf(oi, contexts[ci][ai]);
- predValue = values != null? values[ci][ai] : 1.0;
+ predValue = values != null ? values[ci][ai] : 1.0;
expectation[oi] += predValue * x[vectorIndex];
}
}
@@ -141,10 +142,10 @@ public class NegLogLikelihood implements Function {
}
for (oi = 0; oi < numOutcomes; oi++) {
- empirical = outcomeList[ci] == oi? 1 : 0;
+ empirical = outcomeList[ci] == oi ? 1 : 0;
for (ai = 0; ai < contexts[ci].length; ai++) {
vectorIndex = indexOf(oi, contexts[ci][ai]);
- predValue = values != null? values[ci][ai] : 1.0;
+ predValue = values != null ? values[ci][ai] : 1.0;
gradient[vectorIndex] +=
predValue * (expectation[oi] - empirical) * numTimesEventsSeen[ci];
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ParallelNegLogLikelihood.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ParallelNegLogLikelihood.java b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ParallelNegLogLikelihood.java
index bbe8d1b..5ae7c71 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ParallelNegLogLikelihood.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/ParallelNegLogLikelihood.java
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package opennlp.tools.ml.maxent.quasinewton;
import java.lang.reflect.Constructor;
@@ -117,10 +118,10 @@ public class ParallelNegLogLikelihood extends NegLogLikelihood {
for (int i = 0; i < threads; i++) {
if (i != threads - 1)
futures.add(executor.submit(
- cons.newInstance(this, i, i*taskSize, taskSize, x)));
+ cons.newInstance(this, i, i * taskSize, taskSize, x)));
else
futures.add(executor.submit(
- cons.newInstance(this, i, i*taskSize, taskSize + leftOver, x)));
+ cons.newInstance(this, i, i * taskSize, taskSize + leftOver, x)));
}
for (Future<?> future: futures)
@@ -179,7 +180,7 @@ public class ParallelNegLogLikelihood extends NegLogLikelihood {
tempSums[oi] = 0;
for (ai = 0; ai < contexts[ci].length; ai++) {
vectorIndex = indexOf(oi, contexts[ci][ai]);
- predValue = values != null? values[ci][ai] : 1.0;
+ predValue = values != null ? values[ci][ai] : 1.0;
tempSums[oi] += predValue * x[vectorIndex];
}
}
@@ -221,7 +222,7 @@ public class ParallelNegLogLikelihood extends NegLogLikelihood {
expectation[oi] = 0;
for (ai = 0; ai < contexts[ci].length; ai++) {
vectorIndex = indexOf(oi, contexts[ci][ai]);
- predValue = values != null? values[ci][ai] : 1.0;
+ predValue = values != null ? values[ci][ai] : 1.0;
expectation[oi] += predValue * x[vectorIndex];
}
}
@@ -233,10 +234,10 @@ public class ParallelNegLogLikelihood extends NegLogLikelihood {
}
for (oi = 0; oi < numOutcomes; oi++) {
- empirical = outcomeList[ci] == oi? 1 : 0;
+ empirical = outcomeList[ci] == oi ? 1 : 0;
for (ai = 0; ai < contexts[ci].length; ai++) {
vectorIndex = indexOf(oi, contexts[ci][ai]);
- predValue = values != null? values[ci][ai] : 1.0;
+ predValue = values != null ? values[ci][ai] : 1.0;
gradientThread[threadIndex][vectorIndex] +=
predValue * (expectation[oi] - empirical) * numTimesEventsSeen[ci];
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNMinimizer.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNMinimizer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNMinimizer.java
index 821faed..f957a02 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNMinimizer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNMinimizer.java
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package opennlp.tools.ml.maxent.quasinewton;
import opennlp.tools.ml.maxent.quasinewton.LineSearch.LineSearchResult;
@@ -165,6 +166,7 @@ public class QNMinimizer {
public Evaluator getEvaluator() {
return evaluator;
}
+
public void setEvaluator(Evaluator evaluator) {
this.evaluator = evaluator;
}
@@ -218,9 +220,9 @@ public class QNMinimizer {
long startTime = System.currentTimeMillis();
// Initial step size for the 1st iteration
- double initialStepSize = l1Cost > 0?
+ double initialStepSize = l1Cost > 0 ?
ArrayMath.invL2norm(lsr.getPseudoGradAtNext()) :
- ArrayMath.invL2norm(lsr.getGradAtNext());
+ ArrayMath.invL2norm(lsr.getGradAtNext());
for (int iter = 1; iter <= iterations; iter++) {
// Find direction
@@ -260,12 +262,11 @@ public class QNMinimizer {
display(iter + ": ");
if (evaluator != null) {
- display("\t" + lsr.getValueAtNext()
- + "\t" + lsr.getFuncChangeRate()
- + "\t" + evaluator.evaluate(lsr.getNextPoint()) + "\n");
+ display("\t" + lsr.getValueAtNext() + "\t" + lsr.getFuncChangeRate()
+ + "\t" + evaluator.evaluate(lsr.getNextPoint()) + "\n");
} else {
display("\t " + lsr.getValueAtNext() +
- "\t" + lsr.getFuncChangeRate() + "\n");
+ "\t" + lsr.getFuncChangeRate() + "\n");
}
}
if (isConverged(lsr))
@@ -370,13 +371,13 @@ public class QNMinimizer {
if (lsr.getFuncChangeRate() < CONVERGE_TOLERANCE) {
if (verbose)
display("Function change rate is smaller than the threshold "
- + CONVERGE_TOLERANCE + ".\nTraining will stop.\n\n");
+ + CONVERGE_TOLERANCE + ".\nTraining will stop.\n\n");
return true;
}
// Check gradient's norm using the criteria: ||g(x)|| / max(1, ||x||) < threshold
double xNorm = Math.max(1, ArrayMath.l2norm(lsr.getNextPoint()));
- double gradNorm = l1Cost > 0?
+ double gradNorm = l1Cost > 0 ?
ArrayMath.l2norm(lsr.getPseudoGradAtNext()) : ArrayMath.l2norm(lsr.getGradAtNext());
if (gradNorm / xNorm < REL_GRAD_NORM_TOL) {
if (verbose)
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNModel.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNModel.java b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNModel.java
index 40256c9..2725793 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNModel.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNModel.java
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package opennlp.tools.ml.maxent.quasinewton;
import opennlp.tools.ml.model.AbstractModel;
@@ -24,7 +25,7 @@ import opennlp.tools.ml.model.Context;
public class QNModel extends AbstractModel {
public QNModel(Context[] params, String[] predLabels, String[] outcomeNames) {
- super(params, predLabels, outcomeNames);
+ super(params, predLabels, outcomeNames);
this.modelType = ModelType.MaxentQn;
}
@@ -45,7 +46,7 @@ public class QNModel extends AbstractModel {
}
public double[] eval(String[] context, float[] values) {
- return eval(context, values, new double[evalParams.getNumOutcomes()]);
+ return eval(context, values, new double[evalParams.getNumOutcomes()]);
}
/**
@@ -81,7 +82,7 @@ public class QNModel extends AbstractModel {
double logSumExp = ArrayMath.logSumOfExps(probs);
for (int oi = 0; oi < outcomeNames.length; oi++) {
- probs[oi] = Math.exp(probs[oi] - logSumExp);
+ probs[oi] = Math.exp(probs[oi] - logSumExp);
}
return probs;
}
@@ -109,7 +110,7 @@ public class QNModel extends AbstractModel {
for (int i = 0; i < context.length; i++) {
int predIdx = context[i];
- double predValue = values != null? values[i] : 1.0;
+ double predValue = values != null ? values[i] : 1.0;
for (int oi = 0; oi < nOutcomes; oi++) {
probs[oi] += predValue * parameters[oi * nPredLabels + predIdx];
}
@@ -142,7 +143,7 @@ public class QNModel extends AbstractModel {
for (String pred : pmap.keySet()) {
pmapArray[pmap.get(pred)] = pred;
}
-
+
for (int i = 0; i < this.pmap.size(); i++) {
if (i != objModel.pmap.get(pmapArray[i]))
return false;
@@ -156,15 +157,15 @@ public class QNModel extends AbstractModel {
if (this.evalParams.getParams()[i].getOutcomes().length != contextComparing[i].getOutcomes().length)
return false;
for (int j = 0; i < this.evalParams.getParams()[i].getOutcomes().length; i++) {
- if (this.evalParams.getParams()[i].getOutcomes()[j] != contextComparing[i].getOutcomes()[j])
- return false;
+ if (this.evalParams.getParams()[i].getOutcomes()[j] != contextComparing[i].getOutcomes()[j])
+ return false;
}
if (this.evalParams.getParams()[i].getParameters().length != contextComparing[i].getParameters().length)
return false;
for (int j = 0; i < this.evalParams.getParams()[i].getParameters().length; i++) {
- if (this.evalParams.getParams()[i].getParameters()[j] != contextComparing[i].getParameters()[j])
- return false;
+ if (this.evalParams.getParams()[i].getParameters()[j] != contextComparing[i].getParameters()[j])
+ return false;
}
}
return true;
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNTrainer.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNTrainer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNTrainer.java
index dbcc331..b40a00b 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNTrainer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/quasinewton/QNTrainer.java
@@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
+
package opennlp.tools.ml.maxent.quasinewton;
import java.io.IOException;
@@ -84,8 +85,8 @@ public class QNTrainer extends AbstractEventTrainer {
// For testing purpose
public QNTrainer(int m, int maxFctEval, boolean verbose) {
this.verbose = verbose;
- this.m = m < 0? M_DEFAULT: m;
- this.maxFctEval = maxFctEval < 0? MAX_FCT_EVAL_DEFAULT: maxFctEval;
+ this.m = m < 0 ? M_DEFAULT : m;
+ this.maxFctEval = maxFctEval < 0 ? MAX_FCT_EVAL_DEFAULT : maxFctEval;
this.threads = THREADS_DEFAULT;
this.l1Cost = L1COST_DEFAULT;
this.l2Cost = L2COST_DEFAULT;
@@ -223,7 +224,7 @@ public class QNTrainer extends AbstractEventTrainer {
for (int ei = 0; ei < contexts.length; ei++) {
int[] context = contexts[ei];
- float[] value = values == null? null: values[ei];
+ float[] value = values == null ? null : values[ei];
double[] probs = new double[nOutcomes];
QNModel.eval(context, value, probs, nOutcomes, nPredLabels, parameters);
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
index 8abcecc..caebbd7 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractDataIndexer.java
@@ -89,7 +89,7 @@ public abstract class AbstractDataIndexer implements DataIndexer {
if (sort && eventsToCompare.size() > 0) {
Collections.sort(eventsToCompare);
-
+
ComparableEvent ce = eventsToCompare.get(0);
for (int i = 1; i < numEvents; i++) {
ComparableEvent ce2 = eventsToCompare.get(i);
@@ -108,11 +108,11 @@ public abstract class AbstractDataIndexer implements DataIndexer {
else {
numUniqueEvents = eventsToCompare.size();
}
-
- if(numUniqueEvents == 0) {
+
+ if (numUniqueEvents == 0) {
throw new InsufficientTrainingDataException("Insufficient training data to create model.");
}
-
+
if (sort) System.out.println("done. Reduced " + numEvents + " events to " + numUniqueEvents + ".");
contexts = new int[numUniqueEvents][];
@@ -144,19 +144,19 @@ public abstract class AbstractDataIndexer implements DataIndexer {
* @param counter The predicate counters.
* @param cutoff The cutoff which determines whether a predicate is included.
*/
- protected static void update(String[] ec, Set<String> predicateSet, Map<String,Integer> counter, int cutoff) {
- for (String s : ec) {
- Integer i = counter.get(s);
- if (i == null) {
- counter.put(s, 1);
- }
- else {
- counter.put(s, i + 1);
- }
- if (!predicateSet.contains(s) && counter.get(s) >= cutoff) {
- predicateSet.add(s);
- }
- }
+ protected static void update(String[] ec, Set<String> predicateSet, Map<String,Integer> counter, int cutoff) {
+ for (String s : ec) {
+ Integer i = counter.get(s);
+ if (i == null) {
+ counter.put(s, 1);
+ }
+ else {
+ counter.put(s, i + 1);
+ }
+ if (!predicateSet.contains(s) && counter.get(s) >= cutoff) {
+ predicateSet.add(s);
+ }
+ }
}
/**
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModel.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModel.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModel.java
index 3c03c22..3c10fad 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModel.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModel.java
@@ -34,7 +34,12 @@ public abstract class AbstractModel implements MaxentModel {
/** Prior distribution for this model. */
protected Prior prior;
- public enum ModelType {Maxent,Perceptron,MaxentQn,NaiveBayes}
+ public enum ModelType {
+ Maxent,
+ Perceptron,
+ MaxentQn,
+ NaiveBayes
+ }
/** The type of the model. */
protected ModelType modelType;
@@ -55,13 +60,13 @@ public abstract class AbstractModel implements MaxentModel {
this.evalParams = new EvalParameters(params,correctionParam,correctionConstant,outcomeNames.length);
}
- private void init(String[] predLabels, String[] outcomeNames){
+ private void init(String[] predLabels, String[] outcomeNames) {
this.pmap = new HashMap<String, Integer>(predLabels.length);
-
+
for (int i = 0; i < predLabels.length; i++) {
pmap.put(predLabels[i], i);
}
-
+
this.outcomeNames = outcomeNames;
}
@@ -75,13 +80,13 @@ public abstract class AbstractModel implements MaxentModel {
* @return The name of the most likely outcome.
*/
public final String getBestOutcome(double[] ocs) {
- int best = 0;
- for (int i = 1; i<ocs.length; i++)
- if (ocs[i] > ocs[best]) best = i;
- return outcomeNames[best];
+ int best = 0;
+ for (int i = 1; i < ocs.length; i++)
+ if (ocs[i] > ocs[best]) best = i;
+ return outcomeNames[best];
}
- public ModelType getModelType(){
+ public ModelType getModelType() {
return modelType;
}
@@ -98,18 +103,18 @@ public abstract class AbstractModel implements MaxentModel {
* for each one.
*/
public final String getAllOutcomes(double[] ocs) {
- if (ocs.length != outcomeNames.length) {
- return "The double array sent as a parameter to GISModel.getAllOutcomes() must not have been produced by this model.";
- }
- else {
- DecimalFormat df = new DecimalFormat("0.0000");
- StringBuilder sb = new StringBuilder(ocs.length * 2);
- sb.append(outcomeNames[0]).append("[").append(df.format(ocs[0])).append("]");
- for (int i = 1; i<ocs.length; i++) {
- sb.append(" ").append(outcomeNames[i]).append("[").append(df.format(ocs[i])).append("]");
- }
- return sb.toString();
+ if (ocs.length != outcomeNames.length) {
+ return "The double array sent as a parameter to GISModel.getAllOutcomes() must not have been produced by this model.";
+ }
+ else {
+ DecimalFormat df = new DecimalFormat("0.0000");
+ StringBuilder sb = new StringBuilder(ocs.length * 2);
+ sb.append(outcomeNames[0]).append("[").append(df.format(ocs[0])).append("]");
+ for (int i = 1; i < ocs.length; i++) {
+ sb.append(" ").append(outcomeNames[i]).append("[").append(df.format(ocs[i])).append("]");
}
+ return sb.toString();
+ }
}
/**
@@ -119,7 +124,7 @@ public abstract class AbstractModel implements MaxentModel {
* @return The name of the outcome associated with that id.
*/
public final String getOutcome(int i) {
- return outcomeNames[i];
+ return outcomeNames[i];
}
/**
@@ -128,14 +133,14 @@ public abstract class AbstractModel implements MaxentModel {
* @param outcome the String name of the outcome for which the
* index is desired
* @return the index if the given outcome label exists for this
- * model, -1 if it does not.
+ * model, -1 if it does not.
**/
public int getIndex(String outcome) {
- for (int i=0; i<outcomeNames.length; i++) {
- if (outcomeNames[i].equals(outcome))
- return i;
- }
- return -1;
+ for (int i = 0; i < outcomeNames.length; i++) {
+ if (outcomeNames[i].equals(outcome))
+ return i;
+ }
+ return -1;
}
public int getNumOutcomes() {
@@ -154,7 +159,7 @@ public abstract class AbstractModel implements MaxentModel {
* to unique integers
* <li>index 2: java.lang.String[] containing the names of the outcomes,
* stored in the index of the array which represents their
- * unique ids in the model.
+ * unique ids in the model.
* <li>index 3: java.lang.Integer containing the value of the models
* correction constant
* <li>index 4: java.lang.Double containing the value of the models
@@ -164,12 +169,12 @@ public abstract class AbstractModel implements MaxentModel {
* @return An Object[] with the values as described above.
*/
public final Object[] getDataStructures() {
- Object[] data = new Object[5];
- data[0] = evalParams.getParams();
- data[1] = pmap;
- data[2] = outcomeNames;
- data[3] = (int) evalParams.getCorrectionConstant();
- data[4] = evalParams.getCorrectionParam();
- return data;
+ Object[] data = new Object[5];
+ data[0] = evalParams.getParams();
+ data[1] = pmap;
+ data[2] = outcomeNames;
+ data[3] = (int) evalParams.getCorrectionConstant();
+ data[4] = evalParams.getCorrectionParam();
+ return data;
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModelReader.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModelReader.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModelReader.java
index 6c26214..bb44d8b 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModelReader.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/AbstractModelReader.java
@@ -41,7 +41,7 @@ public abstract class AbstractModelReader {
// handle the zipped/not zipped distinction
if (filename.endsWith(".gz")) {
input = new GZIPInputStream(new FileInputStream(f));
- filename = filename.substring(0,filename.length()-3);
+ filename = filename.substring(0,filename.length() - 3);
}
else {
input = new FileInputStream(f);
@@ -92,56 +92,56 @@ public abstract class AbstractModelReader {
public abstract AbstractModel constructModel() throws java.io.IOException;
protected String[] getOutcomes() throws java.io.IOException {
- int numOutcomes = readInt();
- String[] outcomeLabels = new String[numOutcomes];
- for (int i=0; i<numOutcomes; i++) outcomeLabels[i] = readUTF();
- return outcomeLabels;
+ int numOutcomes = readInt();
+ String[] outcomeLabels = new String[numOutcomes];
+ for (int i = 0; i < numOutcomes; i++) outcomeLabels[i] = readUTF();
+ return outcomeLabels;
}
protected int[][] getOutcomePatterns() throws java.io.IOException {
- int numOCTypes = readInt();
- int[][] outcomePatterns = new int[numOCTypes][];
- for (int i=0; i<numOCTypes; i++) {
- StringTokenizer tok = new StringTokenizer(readUTF(), " ");
- int[] infoInts = new int[tok.countTokens()];
- for (int j = 0; tok.hasMoreTokens(); j++) {
- infoInts[j] = Integer.parseInt(tok.nextToken());
- }
- outcomePatterns[i] = infoInts;
+ int numOCTypes = readInt();
+ int[][] outcomePatterns = new int[numOCTypes][];
+ for (int i = 0; i < numOCTypes; i++) {
+ StringTokenizer tok = new StringTokenizer(readUTF(), " ");
+ int[] infoInts = new int[tok.countTokens()];
+ for (int j = 0; tok.hasMoreTokens(); j++) {
+ infoInts[j] = Integer.parseInt(tok.nextToken());
}
- return outcomePatterns;
+ outcomePatterns[i] = infoInts;
+ }
+ return outcomePatterns;
}
protected String[] getPredicates() throws java.io.IOException {
- NUM_PREDS = readInt();
- String[] predLabels = new String[NUM_PREDS];
- for (int i=0; i<NUM_PREDS; i++)
- predLabels[i] = readUTF();
- return predLabels;
+ NUM_PREDS = readInt();
+ String[] predLabels = new String[NUM_PREDS];
+ for (int i = 0; i < NUM_PREDS; i++)
+ predLabels[i] = readUTF();
+ return predLabels;
}
/**
* Reads the parameters from a file and populates an array of context objects.
* @param outcomePatterns The outcomes patterns for the model. The first index refers to which
- * outcome pattern (a set of outcomes that occurs with a context) is being specified. The
- * second index specifies the number of contexts which use this pattern at index 0, and the
- * index of each outcomes which make up this pattern in indicies 1-n.
+ * outcome pattern (a set of outcomes that occurs with a context) is being specified. The
+ * second index specifies the number of contexts which use this pattern at index 0, and the
+ * index of each outcomes which make up this pattern in indicies 1-n.
* @return An array of context objects.
* @throws java.io.IOException when the model file does not match the outcome patterns or can not be read.
*/
protected Context[] getParameters(int[][] outcomePatterns) throws java.io.IOException {
Context[] params = new Context[NUM_PREDS];
- int pid=0;
- for (int i=0; i<outcomePatterns.length; i++) {
+ int pid = 0;
+ for (int i = 0; i < outcomePatterns.length; i++) {
//construct outcome pattern
- int[] outcomePattern = new int[outcomePatterns[i].length-1];
- System.arraycopy(outcomePatterns[i], 1, outcomePattern, 0, outcomePatterns[i].length - 1);
+ int[] outcomePattern = new int[outcomePatterns[i].length - 1];
+ System.arraycopy(outcomePatterns[i], 1, outcomePattern, 0, outcomePatterns[i].length - 1);
//System.err.println("outcomePattern "+i+" of "+outcomePatterns.length+" with "+outcomePatterns[i].length+" outcomes ");
//populate parameters for each context which uses this outcome pattern.
- for (int j=0; j<outcomePatterns[i][0]; j++) {
- double[] contextParameters = new double[outcomePatterns[i].length-1];
- for (int k=1; k<outcomePatterns[i].length; k++) {
- contextParameters[k-1] = readDouble();
+ for (int j = 0; j < outcomePatterns[i][0]; j++) {
+ double[] contextParameters = new double[outcomePatterns[i].length - 1];
+ for (int k = 1; k < outcomePatterns[i].length; k++) {
+ contextParameters[k - 1] = readDouble();
}
params[pid] = new Context(outcomePattern,contextParameters);
pid++;
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/ComparableEvent.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/ComparableEvent.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/ComparableEvent.java
index 83ed078..2ac89bb 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/ComparableEvent.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/ComparableEvent.java
@@ -27,8 +27,7 @@ package opennlp.tools.ml.model;
public class ComparableEvent implements Comparable<ComparableEvent> {
public int outcome;
public int[] predIndexes;
- public int seen = 1; // the number of times this event
- // has been seen.
+ public int seen = 1; // the number of times this event has been seen.
public float[] values;
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/ComparablePredicate.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/ComparablePredicate.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/ComparablePredicate.java
index 7483a26..b08b574 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/ComparablePredicate.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/ComparablePredicate.java
@@ -36,10 +36,10 @@ public class ComparablePredicate implements Comparable<ComparablePredicate> {
}
public int compareTo(ComparablePredicate cp) {
- int smallerLength = outcomes.length > cp.outcomes.length?
+ int smallerLength = outcomes.length > cp.outcomes.length ?
cp.outcomes.length : outcomes.length;
- for (int i=0; i<smallerLength; i++) {
+ for (int i = 0; i < smallerLength; i++) {
if (outcomes[i] < cp.outcomes[i]) return -1;
else if (outcomes[i] > cp.outcomes[i]) return 1;
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/DataIndexer.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/DataIndexer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/DataIndexer.java
index 35680ac..4bb259e 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/DataIndexer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/DataIndexer.java
@@ -25,7 +25,7 @@ public interface DataIndexer {
/**
* Returns the array of predicates seen in each event.
* @return a 2-D array whose first dimension is the event index and array this refers to contains
- * the contexts for that event.
+ * the contexts for that event.
*/
public int[][] getContexts();
@@ -44,7 +44,7 @@ public interface DataIndexer {
/**
* Returns an array of predicate/context names.
* @return an array of predicate/context names indexed by context index. These indices are the
- * value of the array returned by <code>getContexts</code>.
+ * value of the array returned by <code>getContexts</code>.
*/
public String[] getPredLabels();
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/EvalParameters.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/EvalParameters.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/EvalParameters.java
index 08dce82..6290e40 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/EvalParameters.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/EvalParameters.java
@@ -19,13 +19,13 @@
package opennlp.tools.ml.model;
- /**
+/**
* This class encapsulates the varibales used in producing probabilities from a model
* and facilitaes passing these variables to the eval method.
*/
public class EvalParameters {
- /** Mapping between outcomes and paramater values for each context.
+ /** Mapping between outcomes and paramater values for each context.
* The integer representation of the context can be found using <code>pmap</code>.*/
private Context[] params;
/** The number of outcomes being predicted. */
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/Event.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/Event.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/Event.java
index 0038995..9327e16 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/Event.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/Event.java
@@ -25,49 +25,48 @@ package opennlp.tools.ml.model;
* contextual predicates and an outcome.
*/
public class Event {
- private String outcome;
- private String[] context;
- private float[] values;
+ private String outcome;
+ private String[] context;
+ private float[] values;
- public Event(String outcome, String[] context) {
- this(outcome,context,null);
- }
+ public Event(String outcome, String[] context) {
+ this(outcome,context,null);
+ }
- public Event(String outcome, String[] context, float[] values) {
- this.outcome = outcome;
- this.context = context;
- this.values = values;
- }
+ public Event(String outcome, String[] context, float[] values) {
+ this.outcome = outcome;
+ this.context = context;
+ this.values = values;
+ }
- public String getOutcome() {
- return outcome;
- }
+ public String getOutcome() {
+ return outcome;
+ }
- public String[] getContext() {
- return context;
- }
+ public String[] getContext() {
+ return context;
+ }
- public float[] getValues() {
- return values;
- }
+ public float[] getValues() {
+ return values;
+ }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append(outcome).append(" [");
- if (context.length > 0) {
- sb.append(context[0]);
- if (values != null) {
- sb.append("=").append(values[0]);
- }
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ sb.append(outcome).append(" [");
+ if (context.length > 0) {
+ sb.append(context[0]);
+ if (values != null) {
+ sb.append("=").append(values[0]);
}
- for (int ci=1;ci<context.length;ci++) {
- sb.append(" ").append(context[ci]);
- if (values != null) {
- sb.append("=").append(values[ci]);
- }
+ }
+ for (int ci = 1; ci < context.length; ci++) {
+ sb.append(" ").append(context[ci]);
+ if (values != null) {
+ sb.append("=").append(values[ci]);
}
- sb.append("]");
- return sb.toString();
}
-
+ sb.append("]");
+ return sb.toString();
+ }
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/FileEventStream.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/FileEventStream.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/FileEventStream.java
index b44707a..03fa51b 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/FileEventStream.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/FileEventStream.java
@@ -67,7 +67,7 @@ public class FileEventStream implements ObjectStream<Event> {
@Override
public Event read() throws IOException {
String line;
- if ((line =reader.readLine()) != null) {
+ if ((line = reader.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
String outcome = st.nextToken();
int count = st.countTokens();
@@ -96,7 +96,7 @@ public class FileEventStream implements ObjectStream<Event> {
StringBuilder sb = new StringBuilder();
sb.append(event.getOutcome());
String[] context = event.getContext();
- for (int ci=0,cl=context.length;ci<cl;ci++) {
+ for (int ci = 0,cl = context.length; ci < cl; ci++) {
sb.append(" ").append(context[ci]);
}
sb.append(System.getProperty("line.separator"));
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/GenericModelReader.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/GenericModelReader.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/GenericModelReader.java
index 37c000d..fc3ef8e 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/GenericModelReader.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/GenericModelReader.java
@@ -31,7 +31,7 @@ public class GenericModelReader extends AbstractModelReader {
private AbstractModelReader delegateModelReader;
- public GenericModelReader (File f) throws IOException {
+ public GenericModelReader(File f) throws IOException {
super(f);
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/HashSumEventStream.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/HashSumEventStream.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/HashSumEventStream.java
index b869c1c..9caebb1 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/HashSumEventStream.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/HashSumEventStream.java
@@ -37,7 +37,7 @@ public class HashSumEventStream extends AbstractObjectStream<Event> {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
// should never happen, does all java runtimes have md5 ?!
- throw new IllegalStateException(e);
+ throw new IllegalStateException(e);
}
}
@@ -63,7 +63,7 @@ public class HashSumEventStream extends AbstractObjectStream<Event> {
*
* @return the hash sum
* @throws IllegalStateException if the stream is not consumed completely,
- * completely means that hasNext() returns false
+ * completely means that hasNext() returns false
*/
public BigInteger calculateHashSum() {
return new BigInteger(1, digest.digest());
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/IndexHashTable.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/IndexHashTable.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/IndexHashTable.java
index 5849791..42a9b6f 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/IndexHashTable.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/IndexHashTable.java
@@ -33,8 +33,8 @@ package opennlp.tools.ml.model;
* The table is thread safe and can concurrently accessed by multiple threads,
* thread safety is achieved through immutability. Though its not strictly immutable
* which means, that the table must still be safely published to other threads.
- *
- * @deprecated use java.util.HashMap instead
+ *
+ * @deprecated use java.util.HashMap instead
*/
@Deprecated
public class IndexHashTable<T> {
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/MaxentModel.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/MaxentModel.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/MaxentModel.java
index a5fd184..d24404f 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/MaxentModel.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/MaxentModel.java
@@ -98,7 +98,7 @@ public interface MaxentModel {
* @param outcome the String name of the outcome for which the
* index is desired
* @return the index if the given outcome label exists for this
- * model, -1 if it does not.
+ * model, -1 if it does not.
**/
int getIndex(String outcome);
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/MutableContext.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/MutableContext.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/MutableContext.java
index 36e2cf1..6759252 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/MutableContext.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/MutableContext.java
@@ -45,7 +45,7 @@ public class MutableContext extends Context {
* @param value The value to be assigned.
*/
public void setParameter(int outcomeIndex, double value) {
- parameters[outcomeIndex]=value;
+ parameters[outcomeIndex] = value;
}
/**
@@ -55,7 +55,7 @@ public class MutableContext extends Context {
* @param value The value to be added.
*/
public void updateParameter(int outcomeIndex, double value) {
- parameters[outcomeIndex]+=value;
+ parameters[outcomeIndex] += value;
}
public boolean contains(int outcome) {
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java
index 272f608..4b8f946 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/OnePassRealValueDataIndexer.java
@@ -80,7 +80,7 @@ public class OnePassRealValueDataIndexer extends OnePassDataIndexer {
List<ComparableEvent> eventsToCompare = new ArrayList<>(numEvents);
List<Integer> indexedContext = new ArrayList<>();
- for (int eventIndex=0; eventIndex<numEvents; eventIndex++) {
+ for (int eventIndex = 0; eventIndex < numEvents; eventIndex++) {
Event ev = events.removeFirst();
String[] econtext = ev.getContext();
ComparableEvent ce;
@@ -104,16 +104,16 @@ public class OnePassRealValueDataIndexer extends OnePassDataIndexer {
//drop events with no active features
if (indexedContext.size() > 0) {
int[] cons = new int[indexedContext.size()];
- for (int ci=0;ci<cons.length;ci++) {
+ for (int ci = 0; ci < cons.length; ci++) {
cons[ci] = indexedContext.get(ci);
}
ce = new ComparableEvent(ocID, cons, ev.getValues());
eventsToCompare.add(ce);
}
else {
- System.err.println("Dropped event "+ev.getOutcome()+":"+Arrays.asList(ev.getContext()));
+ System.err.println("Dropped event " + ev.getOutcome() + ":" + Arrays.asList(ev.getContext()));
}
-// recycle the TIntArrayList
+
indexedContext.clear();
}
outcomeLabels = toIndexedStringArray(omap);
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java
index 86fb6a5..ea1f2bc 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/TwoPassDataIndexer.java
@@ -45,7 +45,7 @@ import opennlp.tools.util.ObjectStream;
* the events. During the first pass a temporary event file is created which
* is read during the second pass.
*/
-public class TwoPassDataIndexer extends AbstractDataIndexer{
+public class TwoPassDataIndexer extends AbstractDataIndexer {
/**
* One argument constructor for DataIndexer which calls the two argument
@@ -61,6 +61,7 @@ public class TwoPassDataIndexer extends AbstractDataIndexer{
public TwoPassDataIndexer(ObjectStream<Event> eventStream, int cutoff) throws IOException {
this(eventStream,cutoff,true);
}
+
/**
* Two argument constructor for DataIndexer.
*
@@ -102,7 +103,7 @@ public class TwoPassDataIndexer extends AbstractDataIndexer{
sortAndMerge(eventsToCompare,sort);
System.out.println("Done indexing.");
}
- catch(IOException e) {
+ catch (IOException e) {
System.err.println(e);
}
}
@@ -133,7 +134,7 @@ public class TwoPassDataIndexer extends AbstractDataIndexer{
}
predCounts = new int[predicateSet.size()];
int index = 0;
- for (Iterator<String> pi=predicateSet.iterator();pi.hasNext();index++) {
+ for (Iterator<String> pi = predicateSet.iterator(); pi.hasNext(); index++) {
String predicate = pi.next();
predCounts[index] = counter.get(predicate);
predicatesInOut.put(predicate,index);
@@ -173,7 +174,7 @@ public class TwoPassDataIndexer extends AbstractDataIndexer{
// drop events with no active features
if (indexedContext.size() > 0) {
int[] cons = new int[indexedContext.size()];
- for (int ci=0;ci<cons.length;ci++) {
+ for (int ci = 0;ci < cons.length; ci++) {
cons[ci] = indexedContext.get(ci);
}
ce = new ComparableEvent(ocID, cons);
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/model/UniformPrior.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/model/UniformPrior.java b/opennlp-tools/src/main/java/opennlp/tools/ml/model/UniformPrior.java
index 7d1b9b9..4929e40 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/model/UniformPrior.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/model/UniformPrior.java
@@ -28,7 +28,7 @@ public class UniformPrior implements Prior {
private double r;
public void logPrior(double[] dist, int[] context, float[] values) {
- for (int oi=0;oi<numOutcomes;oi++) {
+ for (int oi = 0; oi < numOutcomes; oi++) {
dist[oi] = r;
}
}
@@ -39,6 +39,6 @@ public class UniformPrior implements Prior {
public void setLabels(String[] outcomeLabels, String[] contextLabels) {
this.numOutcomes = outcomeLabels.length;
- r = Math.log(1.0/numOutcomes);
+ r = Math.log(1.0 / numOutcomes);
}
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesEvalParameters.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesEvalParameters.java b/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesEvalParameters.java
index 5949b3c..65d8589 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesEvalParameters.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesEvalParameters.java
@@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
package opennlp.tools.ml.naivebayes;
import opennlp.tools.ml.model.Context;
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReader.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReader.java b/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReader.java
index 4bc58bf..00acde6 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReader.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReader.java
@@ -59,7 +59,7 @@ public class NaiveBayesModelReader extends AbstractModelReader {
* other methods provided in this abstract class.
*
* @return The NaiveBayesModel stored in the format and location specified to
- * this NaiveBayesModelReader (usually via its the constructor).
+ * this NaiveBayesModelReader (usually via its the constructor).
*/
public AbstractModel constructModel() throws IOException {
String[] outcomeLabels = getOutcomes();
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/BinaryPerceptronModelReader.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/BinaryPerceptronModelReader.java b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/BinaryPerceptronModelReader.java
index ffba2dd..0e59c38 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/BinaryPerceptronModelReader.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/BinaryPerceptronModelReader.java
@@ -45,7 +45,7 @@ public class BinaryPerceptronModelReader extends PerceptronModelReader {
*
* @param f The File in which the model is stored.
*/
- public BinaryPerceptronModelReader (File f) throws IOException {
+ public BinaryPerceptronModelReader(File f) throws IOException {
super(f);
}
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/BinaryPerceptronModelWriter.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/BinaryPerceptronModelWriter.java b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/BinaryPerceptronModelWriter.java
index 8d0085b..3c31fb6 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/BinaryPerceptronModelWriter.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/BinaryPerceptronModelWriter.java
@@ -41,7 +41,7 @@ public class BinaryPerceptronModelWriter extends PerceptronModelWriter {
* @param model The GISModel which is to be persisted.
* @param f The File in which the model is to be persisted.
*/
- public BinaryPerceptronModelWriter (AbstractModel model, File f) throws IOException {
+ public BinaryPerceptronModelWriter(AbstractModel model, File f) throws IOException {
super(model);
@@ -61,24 +61,24 @@ public class BinaryPerceptronModelWriter extends PerceptronModelWriter {
* @param model The GISModel which is to be persisted.
* @param dos The stream which will be used to persist the model.
*/
- public BinaryPerceptronModelWriter (AbstractModel model, DataOutputStream dos) {
+ public BinaryPerceptronModelWriter(AbstractModel model, DataOutputStream dos) {
super(model);
output = dos;
}
- public void writeUTF (String s) throws java.io.IOException {
+ public void writeUTF(String s) throws java.io.IOException {
output.writeUTF(s);
}
- public void writeInt (int i) throws java.io.IOException {
+ public void writeInt(int i) throws java.io.IOException {
output.writeInt(i);
}
- public void writeDouble (double d) throws java.io.IOException {
+ public void writeDouble(double d) throws java.io.IOException {
output.writeDouble(d);
}
- public void close () throws java.io.IOException {
+ public void close() throws java.io.IOException {
output.flush();
output.close();
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModel.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModel.java b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModel.java
index abc2859..5f3d8ac 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModel.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModel.java
@@ -56,7 +56,7 @@ public class PerceptronModel extends AbstractModel {
public double[] eval(String[] context, float[] values,double[] outsums) {
int[] scontexts = new int[context.length];
java.util.Arrays.fill(outsums, 0);
- for (int i=0; i<context.length; i++) {
+ for (int i = 0; i < context.length; i++) {
Integer ci = pmap.get(context[i]);
scontexts[i] = ci == null ? -1 : ci;
}
@@ -98,7 +98,7 @@ public class PerceptronModel extends AbstractModel {
double normal = 0.0;
for (int oid = 0; oid < numOutcomes; oid++) {
- prior[oid] = Math.exp(prior[oid]/maxPrior);
+ prior[oid] = Math.exp(prior[oid] / maxPrior);
normal += prior[oid];
}
@@ -119,8 +119,8 @@ public class PerceptronModel extends AbstractModel {
for (String line = in.readLine(); line != null; line = in.readLine()) {
String[] context = line.split(" ");
double[] dist = m.eval(context);
- for (int oi=0;oi<dist.length;oi++) {
- System.out.print("["+m.getOutcome(oi)+" "+df.format(dist[oi])+"] ");
+ for (int oi = 0;oi < dist.length; oi++) {
+ System.out.print("[" + m.getOutcome(oi) + " " + df.format(dist[oi]) + "] ");
}
System.out.println();
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelReader.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelReader.java b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelReader.java
index 5374ee5..9205c30 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelReader.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelReader.java
@@ -33,51 +33,51 @@ import opennlp.tools.ml.model.DataReader;
*/
public class PerceptronModelReader extends AbstractModelReader {
- public PerceptronModelReader(File file) throws IOException {
- super(file);
- }
+ public PerceptronModelReader(File file) throws IOException {
+ super(file);
+ }
- public PerceptronModelReader(DataReader dataReader) {
- super(dataReader);
- }
+ public PerceptronModelReader(DataReader dataReader) {
+ super(dataReader);
+ }
- /**
- * Retrieve a model from disk. It assumes that models are saved in the
- * following sequence:
- *
- * <br>Perceptron (model type identifier)
- * <br>1. # of parameters (int)
- * <br>2. # of outcomes (int)
- * <br> * list of outcome names (String)
- * <br>3. # of different types of outcome patterns (int)
- * <br> * list of (int int[])
- * <br> [# of predicates for which outcome pattern is true] [outcome pattern]
- * <br>4. # of predicates (int)
- * <br> * list of predicate names (String)
- *
- * <p>If you are creating a reader for a format which won't work with this
- * (perhaps a database or xml file), override this method and ignore the
- * other methods provided in this abstract class.
- *
- * @return The PerceptronModel stored in the format and location specified to
- * this PerceptronModelReader (usually via its the constructor).
- */
- public AbstractModel constructModel() throws IOException {
- String[] outcomeLabels = getOutcomes();
- int[][] outcomePatterns = getOutcomePatterns();
- String[] predLabels = getPredicates();
- Context[] params = getParameters(outcomePatterns);
+ /**
+ * Retrieve a model from disk. It assumes that models are saved in the
+ * following sequence:
+ *
+ * <br>Perceptron (model type identifier)
+ * <br>1. # of parameters (int)
+ * <br>2. # of outcomes (int)
+ * <br> * list of outcome names (String)
+ * <br>3. # of different types of outcome patterns (int)
+ * <br> * list of (int int[])
+ * <br> [# of predicates for which outcome pattern is true] [outcome pattern]
+ * <br>4. # of predicates (int)
+ * <br> * list of predicate names (String)
+ *
+ * <p>If you are creating a reader for a format which won't work with this
+ * (perhaps a database or xml file), override this method and ignore the
+ * other methods provided in this abstract class.
+ *
+ * @return The PerceptronModel stored in the format and location specified to
+ * this PerceptronModelReader (usually via its the constructor).
+ */
+ public AbstractModel constructModel() throws IOException {
+ String[] outcomeLabels = getOutcomes();
+ int[][] outcomePatterns = getOutcomePatterns();
+ String[] predLabels = getPredicates();
+ Context[] params = getParameters(outcomePatterns);
- return new PerceptronModel(params,
- predLabels,
- outcomeLabels);
- }
+ return new PerceptronModel(params,
+ predLabels,
+ outcomeLabels);
+ }
- public void checkModelType() throws java.io.IOException {
- String modelType = readUTF();
- if (!modelType.equals("Perceptron"))
- System.out.println("Error: attempting to load a "+modelType+
- " model as a Perceptron model."+
- " You should expect problems.");
- }
+ public void checkModelType() throws java.io.IOException {
+ String modelType = readUTF();
+ if (!modelType.equals("Perceptron"))
+ System.out.println("Error: attempting to load a " + modelType +
+ " model as a Perceptron model." +
+ " You should expect problems.");
+ }
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelWriter.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelWriter.java b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelWriter.java
index e958c3c..fcab1f4 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelWriter.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronModelWriter.java
@@ -37,129 +37,129 @@ import opennlp.tools.ml.model.Context;
*
*/
public abstract class PerceptronModelWriter extends AbstractModelWriter {
- protected Context[] PARAMS;
- protected String[] OUTCOME_LABELS;
- protected String[] PRED_LABELS;
- private int numOutcomes;
+ protected Context[] PARAMS;
+ protected String[] OUTCOME_LABELS;
+ protected String[] PRED_LABELS;
+ private int numOutcomes;
- public PerceptronModelWriter (AbstractModel model) {
+ public PerceptronModelWriter(AbstractModel model) {
- Object[] data = model.getDataStructures();
- this.numOutcomes = model.getNumOutcomes();
- PARAMS = (Context[]) data[0];
+ Object[] data = model.getDataStructures();
+ this.numOutcomes = model.getNumOutcomes();
+ PARAMS = (Context[]) data[0];
- @SuppressWarnings("unchecked")
- Map<String, Integer> pmap = (Map<String, Integer>) data[1];
- OUTCOME_LABELS = (String[])data[2];
+ @SuppressWarnings("unchecked")
+ Map<String, Integer> pmap = (Map<String, Integer>) data[1];
+ OUTCOME_LABELS = (String[])data[2];
- PRED_LABELS = new String[pmap.size()];
- for (String pred : pmap.keySet()) {
- PRED_LABELS[pmap.get(pred)] = pred;
- }
+ PRED_LABELS = new String[pmap.size()];
+ for (String pred : pmap.keySet()) {
+ PRED_LABELS[pmap.get(pred)] = pred;
}
-
- protected ComparablePredicate[] sortValues () {
- ComparablePredicate[] sortPreds;
- ComparablePredicate[] tmpPreds = new ComparablePredicate[PARAMS.length];
- int[] tmpOutcomes = new int[numOutcomes];
- double[] tmpParams = new double[numOutcomes];
- int numPreds = 0;
- //remove parameters with 0 weight and predicates with no parameters
- for (int pid=0; pid<PARAMS.length; pid++) {
- int numParams = 0;
- double[] predParams = PARAMS[pid].getParameters();
- int[] outcomePattern = PARAMS[pid].getOutcomes();
- for (int pi=0;pi<predParams.length;pi++) {
- if (predParams[pi] != 0d) {
- tmpOutcomes[numParams]=outcomePattern[pi];
- tmpParams[numParams]=predParams[pi];
- numParams++;
- }
- }
-
- int[] activeOutcomes = new int[numParams];
- double[] activeParams = new double[numParams];
-
- for (int pi=0;pi<numParams;pi++) {
- activeOutcomes[pi] = tmpOutcomes[pi];
- activeParams[pi] = tmpParams[pi];
- }
- if (numParams != 0) {
- tmpPreds[numPreds] = new ComparablePredicate(PRED_LABELS[pid],activeOutcomes,activeParams);
- numPreds++;
+ }
+
+ protected ComparablePredicate[] sortValues() {
+ ComparablePredicate[] sortPreds;
+ ComparablePredicate[] tmpPreds = new ComparablePredicate[PARAMS.length];
+ int[] tmpOutcomes = new int[numOutcomes];
+ double[] tmpParams = new double[numOutcomes];
+ int numPreds = 0;
+ //remove parameters with 0 weight and predicates with no parameters
+ for (int pid = 0; pid < PARAMS.length; pid++) {
+ int numParams = 0;
+ double[] predParams = PARAMS[pid].getParameters();
+ int[] outcomePattern = PARAMS[pid].getOutcomes();
+ for (int pi = 0; pi < predParams.length; pi++) {
+ if (predParams[pi] != 0d) {
+ tmpOutcomes[numParams] = outcomePattern[pi];
+ tmpParams[numParams] = predParams[pi];
+ numParams++;
}
}
- System.err.println("Compressed "+PARAMS.length+" parameters to "+numPreds);
- sortPreds = new ComparablePredicate[numPreds];
- System.arraycopy(tmpPreds, 0, sortPreds, 0, numPreds);
- Arrays.sort(sortPreds);
- return sortPreds;
- }
+ int[] activeOutcomes = new int[numParams];
+ double[] activeParams = new double[numParams];
- protected List<List<ComparablePredicate>> computeOutcomePatterns(ComparablePredicate[] sorted) {
- ComparablePredicate cp = sorted[0];
- List<List<ComparablePredicate>> outcomePatterns = new ArrayList<>();
- List<ComparablePredicate> newGroup = new ArrayList<>();
- for (ComparablePredicate predicate : sorted) {
- if (cp.compareTo(predicate) == 0) {
- newGroup.add(predicate);
- } else {
- cp = predicate;
- outcomePatterns.add(newGroup);
- newGroup = new ArrayList<>();
- newGroup.add(predicate);
- }
+ for (int pi = 0; pi < numParams; pi++) {
+ activeOutcomes[pi] = tmpOutcomes[pi];
+ activeParams[pi] = tmpParams[pi];
+ }
+ if (numParams != 0) {
+ tmpPreds[numPreds] = new ComparablePredicate(PRED_LABELS[pid],activeOutcomes,activeParams);
+ numPreds++;
}
- outcomePatterns.add(newGroup);
- System.err.println(outcomePatterns.size()+" outcome patterns");
- return outcomePatterns;
}
-
- /**
- * Writes the model to disk, using the <code>writeX()</code> methods
- * provided by extending classes.
- *
- * <p>If you wish to create a PerceptronModelWriter which uses a different
- * structure, it will be necessary to override the persist method in
- * addition to implementing the <code>writeX()</code> methods.
- */
- public void persist() throws IOException {
-
- // the type of model (Perceptron)
- writeUTF("Perceptron");
-
- // the mapping from outcomes to their integer indexes
- writeInt(OUTCOME_LABELS.length);
-
- for (String label : OUTCOME_LABELS) {
- writeUTF(label);
+ System.err.println("Compressed " + PARAMS.length + " parameters to " + numPreds);
+ sortPreds = new ComparablePredicate[numPreds];
+ System.arraycopy(tmpPreds, 0, sortPreds, 0, numPreds);
+ Arrays.sort(sortPreds);
+ return sortPreds;
+ }
+
+
+ protected List<List<ComparablePredicate>> computeOutcomePatterns(ComparablePredicate[] sorted) {
+ ComparablePredicate cp = sorted[0];
+ List<List<ComparablePredicate>> outcomePatterns = new ArrayList<>();
+ List<ComparablePredicate> newGroup = new ArrayList<>();
+ for (ComparablePredicate predicate : sorted) {
+ if (cp.compareTo(predicate) == 0) {
+ newGroup.add(predicate);
+ } else {
+ cp = predicate;
+ outcomePatterns.add(newGroup);
+ newGroup = new ArrayList<>();
+ newGroup.add(predicate);
}
+ }
+ outcomePatterns.add(newGroup);
+ System.err.println(outcomePatterns.size() + " outcome patterns");
+ return outcomePatterns;
+ }
+
+ /**
+ * Writes the model to disk, using the <code>writeX()</code> methods
+ * provided by extending classes.
+ *
+ * <p>If you wish to create a PerceptronModelWriter which uses a different
+ * structure, it will be necessary to override the persist method in
+ * addition to implementing the <code>writeX()</code> methods.
+ */
+ public void persist() throws IOException {
+
+ // the type of model (Perceptron)
+ writeUTF("Perceptron");
+
+ // the mapping from outcomes to their integer indexes
+ writeInt(OUTCOME_LABELS.length);
+
+ for (String label : OUTCOME_LABELS) {
+ writeUTF(label);
+ }
- // the mapping from predicates to the outcomes they contributed to.
- // The sorting is done so that we actually can write this out more
- // compactly than as the entire list.
- ComparablePredicate[] sorted = sortValues();
- List<List<ComparablePredicate>> compressed = computeOutcomePatterns(sorted);
+ // the mapping from predicates to the outcomes they contributed to.
+ // The sorting is done so that we actually can write this out more
+ // compactly than as the entire list.
+ ComparablePredicate[] sorted = sortValues();
+ List<List<ComparablePredicate>> compressed = computeOutcomePatterns(sorted);
- writeInt(compressed.size());
+ writeInt(compressed.size());
- for (List<ComparablePredicate> a : compressed) {
- writeUTF(a.size() + a.get(0).toString());
- }
+ for (List<ComparablePredicate> a : compressed) {
+ writeUTF(a.size() + a.get(0).toString());
+ }
- // the mapping from predicate names to their integer indexes
- writeInt(sorted.length);
+ // the mapping from predicate names to their integer indexes
+ writeInt(sorted.length);
- for (ComparablePredicate s : sorted) {
- writeUTF(s.name);
- }
+ for (ComparablePredicate s : sorted) {
+ writeUTF(s.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]);
+ // 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]);
- close();
- }
+ close();
+ }
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronTrainer.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronTrainer.java b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronTrainer.java
index 68883fa..abbace2 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronTrainer.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PerceptronTrainer.java
@@ -216,11 +216,11 @@ public class PerceptronTrainer extends AbstractEventTrainer {
return new PerceptronModel(finalParameters, predLabels, outcomeLabels);
}
- private MutableContext[] findParameters (int iterations, boolean useAverage) {
+ private MutableContext[] findParameters(int iterations, boolean useAverage) {
display("Performing " + iterations + " iterations.\n");
- int[] allOutcomesPattern= new int[numOutcomes];
+ int[] allOutcomesPattern = new int[numOutcomes];
for (int oi = 0; oi < numOutcomes; oi++)
allOutcomesPattern[oi] = oi;
@@ -228,7 +228,7 @@ public class PerceptronTrainer extends AbstractEventTrainer {
MutableContext[] params = new MutableContext[numPreds];
for (int pi = 0; pi < numPreds; pi++) {
params[pi] = new MutableContext(allOutcomesPattern,new double[numOutcomes]);
- for (int aoi=0;aoi<numOutcomes;aoi++)
+ for (int aoi = 0; aoi < numOutcomes; aoi++)
params[pi].setParameter(aoi, 0.0);
}
@@ -239,7 +239,7 @@ public class PerceptronTrainer extends AbstractEventTrainer {
if (useAverage) {
for (int pi = 0; pi < numPreds; pi++) {
summedParams[pi] = new MutableContext(allOutcomesPattern,new double[numOutcomes]);
- for (int aoi=0;aoi<numOutcomes;aoi++)
+ for (int aoi = 0; aoi < numOutcomes; aoi++)
summedParams[pi].setParameter(aoi, 0.0);
}
}
@@ -324,8 +324,8 @@ public class PerceptronTrainer extends AbstractEventTrainer {
// current training accuracy and all of the previous three
// training accuracies, stop training.
if (Math.abs(prevAccuracy1 - trainingAccuracy) < tolerance
- && Math.abs(prevAccuracy2 - trainingAccuracy) < tolerance
- && Math.abs(prevAccuracy3 - trainingAccuracy) < tolerance) {
+ && Math.abs(prevAccuracy2 - trainingAccuracy) < tolerance
+ && Math.abs(prevAccuracy3 - trainingAccuracy) < tolerance) {
display("Stopping: change in training set accuracy less than " + tolerance + "\n");
break;
}
@@ -342,8 +342,8 @@ public class PerceptronTrainer extends AbstractEventTrainer {
// Create averaged parameters
if (useAverage) {
for (int pi = 0; pi < numPreds; pi++)
- for (int aoi=0;aoi<numOutcomes;aoi++)
- summedParams[pi].setParameter(aoi, summedParams[pi].getParameters()[aoi]/numTimesSummed);
+ for (int aoi = 0; aoi < numOutcomes; aoi++)
+ summedParams[pi].setParameter(aoi, summedParams[pi].getParameters()[aoi] / numTimesSummed);
return summedParams;
@@ -355,11 +355,11 @@ public class PerceptronTrainer extends AbstractEventTrainer {
}
- private double trainingStats (EvalParameters evalParams) {
+ private double trainingStats(EvalParameters evalParams) {
int numCorrect = 0;
for (int ei = 0; ei < numUniqueEvents; ei++) {
- for (int ni=0;ni<this.numTimesEventsSeen[ei];ni++) {
+ for (int ni = 0; ni < this.numTimesEventsSeen[ei]; ni++) {
double[] modelDistribution = new double[numOutcomes];
@@ -374,12 +374,12 @@ public class PerceptronTrainer extends AbstractEventTrainer {
}
}
double trainingAccuracy = (double) numCorrect / numEvents;
- display("Stats: (" + numCorrect + "/" + numEvents+") " + trainingAccuracy + "\n");
+ display("Stats: (" + numCorrect + "/" + numEvents + ") " + trainingAccuracy + "\n");
return trainingAccuracy;
}
- private int maxIndex (double[] values) {
+ private int maxIndex(double[] values) {
int max = 0;
for (int i = 1; i < values.length; i++)
if (values[i] > values[max])
@@ -387,13 +387,13 @@ public class PerceptronTrainer extends AbstractEventTrainer {
return max;
}
- private void display (String s) {
+ private void display(String s) {
if (printMessages)
System.out.print(s);
}
- private void displayIteration (int i) {
- if (i > 10 && (i%10) != 0)
+ private void displayIteration(int i) {
+ if (i > 10 && (i % 10) != 0)
return;
if (i < 10)
@@ -406,9 +406,9 @@ public class PerceptronTrainer extends AbstractEventTrainer {
// See whether a number is a perfect square. Inefficient, but fine
// for our purposes.
- private static boolean isPerfectSquare (int n) {
- int root = (int)Math.sqrt(n);
- return root*root == n;
+ private static boolean isPerfectSquare(int n) {
+ int root = (int) Math.sqrt(n);
+ return root * root == n;
}
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PlainTextPerceptronModelReader.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PlainTextPerceptronModelReader.java b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PlainTextPerceptronModelReader.java
index c3a7c55..a9f3788 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PlainTextPerceptronModelReader.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PlainTextPerceptronModelReader.java
@@ -44,7 +44,7 @@ public class PlainTextPerceptronModelReader extends PerceptronModelReader {
*
* @param f The File in which the model is stored.
*/
- public PlainTextPerceptronModelReader (File f) throws IOException {
+ public PlainTextPerceptronModelReader(File f) throws IOException {
super(f);
}
}
http://git-wip-us.apache.org/repos/asf/opennlp/blob/1bccbabb/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PlainTextPerceptronModelWriter.java
----------------------------------------------------------------------
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PlainTextPerceptronModelWriter.java b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PlainTextPerceptronModelWriter.java
index ffde6c6..4f65f55 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PlainTextPerceptronModelWriter.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/perceptron/PlainTextPerceptronModelWriter.java
@@ -42,8 +42,8 @@ public class PlainTextPerceptronModelWriter extends PerceptronModelWriter {
* @param model The PerceptronModel which is to be persisted.
* @param f The File in which the model is to be persisted.
*/
- public PlainTextPerceptronModelWriter (AbstractModel model, File f)
- throws IOException {
+ public PlainTextPerceptronModelWriter(AbstractModel model, File f)
+ throws IOException {
super(model);
if (f.getName().endsWith(".gz")) {
@@ -62,27 +62,27 @@ public class PlainTextPerceptronModelWriter extends PerceptronModelWriter {
* @param model The PerceptronModel which is to be persisted.
* @param bw The BufferedWriter which will be used to persist the model.
*/
- public PlainTextPerceptronModelWriter (AbstractModel model, BufferedWriter bw) {
+ public PlainTextPerceptronModelWriter(AbstractModel model, BufferedWriter bw) {
super(model);
output = bw;
}
- public void writeUTF (String s) throws java.io.IOException {
+ public void writeUTF(String s) throws java.io.IOException {
output.write(s);
output.newLine();
}
- public void writeInt (int i) throws java.io.IOException {
+ public void writeInt(int i) throws java.io.IOException {
output.write(Integer.toString(i));
output.newLine();
}
- public void writeDouble (double d) throws java.io.IOException {
+ public void writeDouble(double d) throws java.io.IOException {
output.write(Double.toString(d));
output.newLine();
}
- public void close () throws java.io.IOException {
+ public void close() throws java.io.IOException {
output.flush();
output.close();
}