public void checkModelType() throws IOException { String modelType = readUTF(); switch (modelType) { case "Perceptron": delegateModelReader = new PerceptronModelReader(this.dataReader); break; case "GIS": delegateModelReader = new GISModelReader(this.dataReader); break; case "QN": delegateModelReader = new QNModelReader(this.dataReader); break; case "NaiveBayes": delegateModelReader = new NaiveBayesModelReader(this.dataReader); break; default: throw new IOException("Unknown model format: " + modelType); } }
/** * Retrieve a model from disk. It assumes that models are saved in the * following sequence: * * <br>NaiveBayes (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 NaiveBayesModel stored in the format and location specified to * this NaiveBayesModelReader (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 NaiveBayesModel(params, predLabels, outcomeLabels); }
public void checkModelType() throws java.io.IOException { String modelType = readUTF(); if (!modelType.equals("NaiveBayes")) System.out.println("Error: attempting to load a " + modelType + " model as a NaiveBayes model." + " You should expect problems."); } }
protected static NaiveBayesModel persistedModel(NaiveBayesModel model) throws IOException { Path tempFilePath = Files.createTempFile("ptnb-", ".bin"); File file = tempFilePath.toFile(); try { NaiveBayesModelWriter modelWriter = new BinaryNaiveBayesModelWriter(model, file); modelWriter.persist(); NaiveBayesModelReader reader = new BinaryNaiveBayesModelReader(file); reader.checkModelType(); return (NaiveBayesModel)reader.constructModel(); } finally { file.delete(); } }
@Test public void testPlainTextModel() throws IOException { testDataIndexer.index(NaiveBayesCorrectnessTest.createTrainingStream()); NaiveBayesModel model1 = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); StringWriter sw1 = new StringWriter(); NaiveBayesModelWriter modelWriter = new PlainTextNaiveBayesModelWriter(model1, new BufferedWriter(sw1)); modelWriter.persist(); NaiveBayesModelReader reader = new PlainTextNaiveBayesModelReader(new BufferedReader(new StringReader(sw1.toString()))); reader.checkModelType(); NaiveBayesModel model2 = (NaiveBayesModel)reader.constructModel(); StringWriter sw2 = new StringWriter(); modelWriter = new PlainTextNaiveBayesModelWriter(model2, new BufferedWriter(sw2)); modelWriter.persist(); System.out.println(sw1.toString()); Assert.assertEquals(sw1.toString(), sw2.toString()); }
/** * Retrieve a model from disk. It assumes that models are saved in the * following sequence: * * <br>NaiveBayes (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 NaiveBayesModel stored in the format and location specified to * this NaiveBayesModelReader (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 NaiveBayesModel(params, predLabels, outcomeLabels); }
@Test public void testBinaryModelPersistence() throws Exception { testDataIndexer.index(NaiveBayesCorrectnessTest.createTrainingStream()); NaiveBayesModel model = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); Path tempFile = Files.createTempFile("bnb-", ".bin"); File file = tempFile.toFile(); try { NaiveBayesModelWriter modelWriter = new BinaryNaiveBayesModelWriter(model, file); modelWriter.persist(); NaiveBayesModelReader reader = new BinaryNaiveBayesModelReader(file); reader.checkModelType(); AbstractModel abstractModel = reader.constructModel(); Assert.assertNotNull(abstractModel); } finally { file.delete(); } }
public void checkModelType() throws java.io.IOException { String modelType = readUTF(); if (!modelType.equals("NaiveBayes")) System.out.println("Error: attempting to load a " + modelType + " model as a NaiveBayes model." + " You should expect problems."); } }
public void checkModelType() throws IOException { String modelType = readUTF(); switch (modelType) { case "Perceptron": delegateModelReader = new PerceptronModelReader(this.dataReader); break; case "GIS": delegateModelReader = new GISModelReader(this.dataReader); break; case "QN": delegateModelReader = new QNModelReader(this.dataReader); break; case "NaiveBayes": delegateModelReader = new NaiveBayesModelReader(this.dataReader); break; default: throw new IOException("Unknown model format: " + modelType); } }
/** * Retrieve a model from disk. It assumes that models are saved in the * following sequence: * * <br>NaiveBayes (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 NaiveBayesModel stored in the format and location specified to * this NaiveBayesModelReader (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 NaiveBayesModel(params, predLabels, outcomeLabels); }
@Test public void testTextModelPersistence() throws Exception { testDataIndexer.index(NaiveBayesCorrectnessTest.createTrainingStream()); NaiveBayesModel model = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); Path tempFile = Files.createTempFile("ptnb-", ".txt"); File file = tempFile.toFile(); try { NaiveBayesModelWriter modelWriter = new PlainTextNaiveBayesModelWriter(model, file); modelWriter.persist(); NaiveBayesModelReader reader = new PlainTextNaiveBayesModelReader(file); reader.checkModelType(); AbstractModel abstractModel = reader.constructModel(); Assert.assertNotNull(abstractModel); } finally { file.delete(); } } }
public void checkModelType() throws java.io.IOException { String modelType = readUTF(); if (!modelType.equals("NaiveBayes")) System.out.println("Error: attempting to load a " + modelType + " model as a NaiveBayes model." + " You should expect problems."); } }
public void checkModelType() throws IOException { String modelType = readUTF(); switch (modelType) { case "Perceptron": delegateModelReader = new PerceptronModelReader(this.dataReader); break; case "GIS": delegateModelReader = new GISModelReader(this.dataReader); break; case "QN": delegateModelReader = new QNModelReader(this.dataReader); break; case "NaiveBayes": delegateModelReader = new NaiveBayesModelReader(this.dataReader); break; default: throw new IOException("Unknown model format: " + modelType); } }