@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()); }
@Test public void testNaiveBayes2() throws IOException { testDataIndexer.index(createTrainingStream()); NaiveBayesModel model = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); String label = "sports"; String[] context = {"bow=manchester", "bow=united"}; Event event = new Event(label, context); // testModel(model, event, 1.0); // Expected value without smoothing testModel(model, event, 0.9658833555831029); // Expected value with smoothing }
@Test public void testNaiveBayes3() throws IOException { testDataIndexer.index(createTrainingStream()); NaiveBayesModel model = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); String label = "politics"; String[] context = {"bow=united"}; Event event = new Event(label, context); //testModel(model, event, 2.0/3.0); // Expected value without smoothing testModel(model, event, 0.6655036407766989); // Expected value with smoothing }
@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(); } }
@Test public void testNaiveBayes4() throws IOException { testDataIndexer.index(createTrainingStream()); NaiveBayesModel model = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); String label = "politics"; String[] context = {}; Event event = new Event(label, context); testModel(model, event, 7.0 / 12.0); }
@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(); } } }
@Test public void testNaiveBayes1() throws IOException { testDataIndexer.index(createTrainingStream()); NaiveBayesModel model = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); String label = "politics"; String[] context = {"bow=united", "bow=nations"}; Event event = new Event(label, context); // testModel(model, event, 1.0); // Expected value without smoothing testModel(model, event, 0.9681650180264167); // Expected value with smoothing }
@Test public void testNaiveBayes1() throws IOException { testDataIndexer.index(NaiveBayesCorrectnessTest.createTrainingStream()); NaiveBayesModel model1 = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); NaiveBayesModel model2 = persistedModel(model1); String label = "politics"; String[] context = {"bow=united", "bow=nations"}; Event event = new Event(label, context); testModelOutcome(model1, model2, event); }
@Test public void testNaiveBayes3() throws IOException { testDataIndexer.index(NaiveBayesCorrectnessTest.createTrainingStream()); NaiveBayesModel model1 = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); NaiveBayesModel model2 = persistedModel(model1); String label = "politics"; String[] context = {"bow=united"}; Event event = new Event(label, context); testModelOutcome(model1, model2, event); }
@Test public void testNaiveBayes4() throws IOException { testDataIndexer.index(NaiveBayesCorrectnessTest.createTrainingStream()); NaiveBayesModel model1 = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); NaiveBayesModel model2 = persistedModel(model1); String label = "politics"; String[] context = {}; Event event = new Event(label, context); testModelOutcome(model1, model2, event); }
@Test public void testNaiveBayes2() throws IOException { testDataIndexer.index(NaiveBayesCorrectnessTest.createTrainingStream()); NaiveBayesModel model1 = (NaiveBayesModel) new NaiveBayesTrainer().trainModel(testDataIndexer); NaiveBayesModel model2 = persistedModel(model1); String label = "sports"; String[] context = {"bow=manchester", "bow=united"}; Event event = new Event(label, context); testModelOutcome(model1, model2, event); }