WindowFeatureGenerator implements AdaptiveFeatureGenerator { public void createFeatures(List<String> features, String[] tokens, int index, String[] preds) { generator.createFeatures(features, tokens, index, preds); for (int i = 1; i < prevWindowSize + 1; i++) { if (index - i >= 0) { List<String> prevFeatures = new ArrayList<String>(); generator.createFeatures(prevFeatures, tokens, index - i, preds); for (String prevFeature : prevFeatures) { features.add(PREV_PREFIX + i + prevFeature); } } } for (int i = 1; i < nextWindowSize + 1; i++) { if (i + index < tokens.length) { List<String> nextFeatures = new ArrayList<String>(); generator.createFeatures(nextFeatures, tokens, index + i, preds); for (String nextFeature : nextFeatures) { features.add(NEXT_PREFIX + i + nextFeature); } } } } WindowFeatureGenerator(AdaptiveFeatureGenerator generator, int prevWindowSize,  int nextWindowSize);  WindowFeatureGenerator(int prevWindowSize, int nextWindowSize, AdaptiveFeatureGenerator... generators);  WindowFeatureGenerator(AdaptiveFeatureGenerator generator);  WindowFeatureGenerator(AdaptiveFeatureGenerator... generators); void createFeatures(List<String> features, String[] tokens, int index, String[] preds); void updateAdaptiveData(String[] tokens, String[] outcomes); void clearAdaptiveData(); @Override String toString(); static final String PREV_PREFIX; static final String NEXT_PREFIX; }
@Test public void testWithoutWindow() { AdaptiveFeatureGenerator windowFeatureGenerator = new WindowFeatureGenerator( new IdentityFeatureGenerator(), 0, 0); int testTokenIndex = 2; windowFeatureGenerator.createFeatures(features, testSentence, testTokenIndex, null); assertEquals(1, features.size()); assertEquals(features.get(0), testSentence[testTokenIndex]); }
@Test public void testWindowSizeOne() { AdaptiveFeatureGenerator windowFeatureGenerator = new WindowFeatureGenerator( new IdentityFeatureGenerator(), 1, 1); int testTokenIndex = 2; windowFeatureGenerator.createFeatures(features, testSentence, testTokenIndex, null); assertEquals(3, features.size()); }
@Test public void testWindowAtBeginOfSentence() { AdaptiveFeatureGenerator windowFeatureGenerator = new WindowFeatureGenerator( new IdentityFeatureGenerator(), 1, 0); int testTokenIndex = 0; windowFeatureGenerator.createFeatures(features, testSentence, testTokenIndex, null); assertEquals(1, features.size()); assertEquals(features.get(0), testSentence[testTokenIndex]); }
@Test public void testWindowAtEndOfSentence() { AdaptiveFeatureGenerator windowFeatureGenerator = new WindowFeatureGenerator( new IdentityFeatureGenerator(), 0, 1); int testTokenIndex = testSentence.length - 1; windowFeatureGenerator.createFeatures(features, testSentence, testTokenIndex, null); assertEquals(1, features.size()); assertEquals(features.get(0), testSentence[testTokenIndex]); }
@Test public void testForCorrectFeatures() { AdaptiveFeatureGenerator windowFeatureGenerator = new WindowFeatureGenerator( new IdentityFeatureGenerator(), 2, 2); int testTokenIndex = 3; windowFeatureGenerator.createFeatures(features, testSentence, testTokenIndex, null); assertEquals(5, features.size()); assertTrue(features.contains(WindowFeatureGenerator.PREV_PREFIX + "2" + testSentence[testTokenIndex - 2])); assertTrue(features.contains(WindowFeatureGenerator.PREV_PREFIX + "1" + testSentence[testTokenIndex - 1])); assertTrue(features.contains(testSentence[testTokenIndex])); assertTrue(features.contains(WindowFeatureGenerator.NEXT_PREFIX + "1" + testSentence[testTokenIndex + 1])); assertTrue(features.contains(WindowFeatureGenerator.NEXT_PREFIX + "2" + testSentence[testTokenIndex + 2])); }