@Test public void testEmpty() throws Exception { NGramFeatureGenerator generator = new NGramFeatureGenerator(); Assert.assertEquals(0, generator.extractFeatures(new String[]{}, Collections.emptyMap()).size()); }
@Test public void testInvalidGramSize1() { try { new NGramFeatureGenerator(0, 1); Assert.fail("InvalidFormatException must be thrown"); } catch (InvalidFormatException expected) { } }
@Test public void testTrigram() throws Exception { NGramFeatureGenerator generator = new NGramFeatureGenerator(3, 3); Assert.assertArrayEquals( new String[]{"ng=:a:b:c", "ng=:b:c:d", "ng=:c:d:e", "ng=:d:e:f", "ng=:e:f:g"}, generator.extractFeatures(TOKENS, Collections.emptyMap()).toArray()); }
@Test public void testInvalidGramSize2() { try { new NGramFeatureGenerator(2, 1); Assert.fail("InvalidFormatException must be thrown"); } catch (InvalidFormatException expected) { } }
@Test public void testUnigram() throws Exception { NGramFeatureGenerator generator = new NGramFeatureGenerator(1, 1); Assert.assertArrayEquals( new String[]{"ng=:a", "ng=:b", "ng=:c", "ng=:d", "ng=:e", "ng=:f", "ng=:g"}, generator.extractFeatures(TOKENS, Collections.emptyMap()).toArray()); }
@Test public void testBigram() throws Exception { NGramFeatureGenerator generator = new NGramFeatureGenerator(2, 2); Assert.assertArrayEquals( new String[]{"ng=:a:b", "ng=:b:c", "ng=:c:d", "ng=:d:e", "ng=:e:f", "ng=:f:g"}, generator.extractFeatures(TOKENS, Collections.emptyMap()).toArray()); }
@Test public void testNull() throws Exception { NGramFeatureGenerator generator = new NGramFeatureGenerator(); try { generator.extractFeatures(null, Collections.emptyMap()); Assert.fail("NullPointerException must be thrown"); } catch (NullPointerException expected) { } }
@Test public void test12gram() throws Exception { NGramFeatureGenerator generator = new NGramFeatureGenerator(1, 2); Assert.assertArrayEquals( new String[]{ "ng=:a", "ng=:a:b", "ng=:b", "ng=:b:c", "ng=:c", "ng=:c:d", "ng=:d", "ng=:d:e", "ng=:e", "ng=:e:f", "ng=:f", "ng=:f:g", "ng=:g" }, generator.extractFeatures(TOKENS, Collections.emptyMap()).toArray()); }
@Test public void test13gram() throws Exception { NGramFeatureGenerator generator = new NGramFeatureGenerator(1, 3); Assert.assertArrayEquals( new String[]{ "ng=:a", "ng=:a:b", "ng=:a:b:c", "ng=:b", "ng=:b:c", "ng=:b:c:d", "ng=:c", "ng=:c:d", "ng=:c:d:e", "ng=:d", "ng=:d:e", "ng=:d:e:f", "ng=:e", "ng=:e:f", "ng=:e:f:g", "ng=:f", "ng=:f:g", "ng=:g" }, generator.extractFeatures(TOKENS, Collections.emptyMap()).toArray()); } }
@Test public void testCustom() throws IOException { FeatureGenerator[] featureGenerators = { new BagOfWordsFeatureGenerator(), new NGramFeatureGenerator(), new NGramFeatureGenerator(2,3) }; DoccatFactory factory = new DoccatFactory(featureGenerators); DoccatModel model = train(factory); Assert.assertNotNull(model); ByteArrayOutputStream out = new ByteArrayOutputStream(); model.serialize(out); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); DoccatModel fromSerialized = new DoccatModel(in); factory = fromSerialized.getFactory(); Assert.assertNotNull(factory); Assert.assertEquals(3, factory.getFeatureGenerators().length); Assert.assertEquals(BagOfWordsFeatureGenerator.class, factory.getFeatureGenerators()[0].getClass()); Assert.assertEquals(NGramFeatureGenerator.class, factory.getFeatureGenerators()[1].getClass()); Assert.assertEquals(NGramFeatureGenerator.class,factory.getFeatureGenerators()[2].getClass()); }