Author: tommaso
Date: Thu Sep 17 08:56:15 2015
New Revision: 1703533

URL: http://svn.apache.org/r1703533
Log:
OPENNLP-777 - added model RW test, minor javadoc comment tweaks

Added:
    
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReadWriteTest.java
Modified:
    
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesEvalParameters.java
    
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesCorrectnessTest.java

Modified: 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesEvalParameters.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesEvalParameters.java?rev=1703533&r1=1703532&r2=1703533&view=diff
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesEvalParameters.java
 (original)
+++ 
opennlp/trunk/opennlp-tools/src/main/java/opennlp/tools/ml/naivebayes/NaiveBayesEvalParameters.java
 Thu Sep 17 08:56:15 2015
@@ -19,6 +19,9 @@ package opennlp.tools.ml.naivebayes;
 import opennlp.tools.ml.model.Context;
 import opennlp.tools.ml.model.EvalParameters;
 
+/**
+ * Parameters for the evalution of a naive bayes classifier
+ */
 public class NaiveBayesEvalParameters extends EvalParameters {
 
   protected double[] outcomeTotals;

Modified: 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesCorrectnessTest.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesCorrectnessTest.java?rev=1703533&r1=1703532&r2=1703533&view=diff
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesCorrectnessTest.java
 (original)
+++ 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesCorrectnessTest.java
 Thu Sep 17 08:56:15 2015
@@ -39,7 +39,7 @@ public class NaiveBayesCorrectnessTest {
   @Test
   public void testNaiveBayes1() throws IOException {
 
-    NaiveBayesModel.setSmoothed(false); // Naive Bayes should always be run 
with smoothing, but I am taking it out here just for mathematical verification
+    NaiveBayesModel.setSmoothed(false); // Naive Bayes should always be run 
with smoothing, taken out here for mathematical verification
 
     NaiveBayesModel model =
         (NaiveBayesModel)new NaiveBayesTrainer().trainModel(new 
TwoPassDataIndexer(createTrainingStream(), 1, false));
@@ -57,7 +57,7 @@ public class NaiveBayesCorrectnessTest {
   @Test
   public void testNaiveBayes2() throws IOException {
 
-    NaiveBayesModel.setSmoothed(false); // Naive Bayes should always be run 
with smoothing, but I am taking it out here just for mathematical verification
+    NaiveBayesModel.setSmoothed(false); // Naive Bayes should always be run 
with smoothing, taken out here for mathematical verification
 
     NaiveBayesModel model =
         (NaiveBayesModel)new NaiveBayesTrainer().trainModel(new 
TwoPassDataIndexer(createTrainingStream(), 1, false));

Added: 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReadWriteTest.java
URL: 
http://svn.apache.org/viewvc/opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReadWriteTest.java?rev=1703533&view=auto
==============================================================================
--- 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReadWriteTest.java
 (added)
+++ 
opennlp/trunk/opennlp-tools/src/test/java/opennlp/tools/ml/naivebayes/NaiveBayesModelReadWriteTest.java
 Thu Sep 17 08:56:15 2015
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package opennlp.tools.ml.naivebayes;
+
+import java.io.File;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import opennlp.tools.ml.model.AbstractModel;
+import opennlp.tools.ml.model.TwoPassDataIndexer;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Tests for persisting and reading naive bayes models
+ */
+public class NaiveBayesModelReadWriteTest {
+
+  @Test
+  public void testBinaryModelPersistence() throws Exception {
+    NaiveBayesModel model = (NaiveBayesModel) new 
NaiveBayesTrainer().trainModel(new TwoPassDataIndexer(
+        NaiveBayesCorrectnessTest.createTrainingStream(), 1, false));
+    Path path = Paths.get(getClass().getResource("/").getFile());
+    Path tempFile = Files.createTempFile(path, "bnb-", ".bin");
+    File file = tempFile.toFile();
+    NaiveBayesModelWriter modelWriter = new BinaryNaiveBayesModelWriter(model, 
file);
+    modelWriter.persist();
+    NaiveBayesModelReader reader = new BinaryNaiveBayesModelReader(file);
+    reader.checkModelType();
+    AbstractModel abstractModel = reader.constructModel();
+    assertNotNull(abstractModel);
+  }
+
+  @Test
+  public void testTextModelPersistence() throws Exception {
+    NaiveBayesModel model = (NaiveBayesModel) new 
NaiveBayesTrainer().trainModel(new TwoPassDataIndexer(
+        NaiveBayesCorrectnessTest.createTrainingStream(), 1, false));
+    Path path = Paths.get(getClass().getResource("/").getFile());
+    Path tempFile = Files.createTempFile(path, "ptnb-", ".txt");
+    File file = tempFile.toFile();
+    NaiveBayesModelWriter modelWriter = new 
PlainTextNaiveBayesModelWriter(model, file);
+    modelWriter.persist();
+    NaiveBayesModelReader reader = new PlainTextNaiveBayesModelReader(file);
+    reader.checkModelType();
+    AbstractModel abstractModel = reader.constructModel();
+    assertNotNull(abstractModel);
+  }
+
+
+}
\ No newline at end of file


Reply via email to