This is an automated email from the ASF dual-hosted git repository.

joern pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/opennlp.git


The following commit(s) were added to refs/heads/master by this push:
     new 8460b28  OPENNLP-1200: Add util to sum features in context
8460b28 is described below

commit 8460b286448012ab3a7dcec8598215154ff76518
Author: Joern Kottmann <[email protected]>
AuthorDate: Tue May 22 13:50:53 2018 +0200

    OPENNLP-1200: Add util to sum features in context
---
 .../src/main/java/opennlp/tools/ml/ArrayMath.java  | 20 ++++++++++++++++++++
 .../java/opennlp/tools/ml/maxent/GISModel.java     | 22 +++-------------------
 .../tools/ml/naivebayes/NaiveBayesModel.java       |  1 -
 .../tools/ml/perceptron/PerceptronModel.java       | 21 +++------------------
 4 files changed, 26 insertions(+), 38 deletions(-)

diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/ArrayMath.java 
b/opennlp-tools/src/main/java/opennlp/tools/ml/ArrayMath.java
index 3d40c69..cf18623 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/ArrayMath.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/ArrayMath.java
@@ -19,6 +19,8 @@ package opennlp.tools.ml;
 
 import java.util.List;
 
+import opennlp.tools.ml.model.Context;
+
 /**
  * Utility class for simple vector arithmetic.
  */
@@ -100,6 +102,24 @@ public class ArrayMath {
     return maxIdx;
   }
 
+  public static void sumFeatures(Context[] context, float[] values, double[] 
prior) {
+    for (int ci = 0; ci < context.length; ci++) {
+      if (context[ci] != null) {
+        Context predParams = context[ci];
+        int[] activeOutcomes = predParams.getOutcomes();
+        double[] activeParameters = predParams.getParameters();
+        double value = 1;
+        if (values != null) {
+          value = values[ci];
+        }
+        for (int ai = 0; ai < activeOutcomes.length; ai++) {
+          int oid = activeOutcomes[ai];
+          prior[oid] += activeParameters[ai] * value;
+        }
+      }
+    }
+  }
+
   // === Not really related to math ===
   /**
    * Convert a list of Double objects into an array of primitive doubles
diff --git a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/GISModel.java 
b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/GISModel.java
index 700a81d..9c99308 100644
--- a/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/GISModel.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/ml/maxent/GISModel.java
@@ -17,6 +17,7 @@
 
 package opennlp.tools.ml.maxent;
 
+import opennlp.tools.ml.ArrayMath;
 import opennlp.tools.ml.model.AbstractModel;
 import opennlp.tools.ml.model.Context;
 import opennlp.tools.ml.model.EvalParameters;
@@ -182,25 +183,8 @@ public final class GISModel extends AbstractModel {
    */
   static double[] eval(Context[] context, float[] values, double[] prior,
                        EvalParameters model) {
-    int[] numfeats = new int[model.getNumOutcomes()];
-    int[] activeOutcomes;
-    double[] activeParameters;
-    double value = 1;
-    for (int ci = 0; ci < context.length; ci++) {
-      if (context[ci] != null) {
-        Context predParams = context[ci];
-        activeOutcomes = predParams.getOutcomes();
-        activeParameters = predParams.getParameters();
-        if (values != null) {
-          value = values[ci];
-        }
-        for (int ai = 0; ai < activeOutcomes.length; ai++) {
-          int oid = activeOutcomes[ai];
-          numfeats[oid]++;
-          prior[oid] += activeParameters[ai] * value;
-        }
-      }
-    }
+
+    ArrayMath.sumFeatures(context, values, prior);
 
     double normal = 0.0;
     for (int oid = 0; oid < model.getNumOutcomes(); oid++) {
diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModel.java 
b/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModel.java
index 85e17eb..77dfc79 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModel.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesModel.java
@@ -89,7 +89,6 @@ public class NaiveBayesModel extends AbstractModel {
   static double[] eval(Context[] context, float[] values, double[] prior,
                        EvalParameters model, boolean normalize) {
     Probabilities<Integer> probabilities = new LogProbabilities<>();
-    Context[] params = model.getParams();
     double[] outcomeTotals = model instanceof NaiveBayesEvalParameters
         ? ((NaiveBayesEvalParameters) model).getOutcomeTotals() : new 
double[prior.length];
     long vocabulary = model instanceof NaiveBayesEvalParameters
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 b931c50..a34c7ef 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
@@ -17,6 +17,7 @@
 
 package opennlp.tools.ml.perceptron;
 
+import opennlp.tools.ml.ArrayMath;
 import opennlp.tools.ml.model.AbstractModel;
 import opennlp.tools.ml.model.Context;
 import opennlp.tools.ml.model.EvalParameters;
@@ -65,24 +66,8 @@ public class PerceptronModel extends AbstractModel {
 
   static double[] eval(Context[] context, float[] values, double[] prior, 
EvalParameters model,
                        boolean normalize) {
-    Context[] params = model.getParams();
-    double[] activeParameters;
-    int[] activeOutcomes;
-    double value = 1;
-    for (int ci = 0; ci < context.length; ci++) {
-      if (context[ci] != null) {
-        Context predParams = context[ci];
-        activeOutcomes = predParams.getOutcomes();
-        activeParameters = predParams.getParameters();
-        if (values != null) {
-          value = values[ci];
-        }
-        for (int ai = 0; ai < activeOutcomes.length; ai++) {
-          int oid = activeOutcomes[ai];
-          prior[oid] += activeParameters[ai] * value;
-        }
-      }
-    }
+
+    ArrayMath.sumFeatures(context, values, prior);
 
     if (normalize) {
       int numOutcomes = model.getNumOutcomes();

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to