DefaultOutcomeFeatureExtractor implements OutcomeFeatureExtractor { public List<Feature> extractFeatures(List<Object> previousOutcomes) { if (previousOutcomes == null || previousOutcomes.size() == 0) { return Collections.emptyList(); } List<Feature> features = new ArrayList<Feature>(); for (int i = mostRecentOutcome; i <= leastRecentOutcome; i++) { int index = previousOutcomes.size() - i; if (index >= 0) { Feature feature = new Feature("PreviousOutcome_L" + i, previousOutcomes.get(index)); features.add(feature); } } if (useBigram && previousOutcomes.size() >= 2) { int size = previousOutcomes.size(); String featureValue = previousOutcomes.get(size - 1).toString() + "_" + previousOutcomes.get(size - 2); Feature feature = new Feature("PreviousOutcomes_L1_2gram_L2R", featureValue); features.add(feature); } if (useTrigram && previousOutcomes.size() >= 3) { int size = previousOutcomes.size(); String featureValue = previousOutcomes.get(size - 1).toString() + "_" + previousOutcomes.get(size - 2) + "_" + previousOutcomes.get(size - 3); Feature feature = new Feature("PreviousOutcomes_L1_3gram_L2R", featureValue); features.add(feature); } if (use4gram && previousOutcomes.size() >= 4) { int size = previousOutcomes.size(); String featureValue = previousOutcomes.get(size - 1).toString() + "_" + previousOutcomes.get(size - 2) + "_" + previousOutcomes.get(size - 3) + "_" + previousOutcomes.get(size - 4); Feature feature = new Feature("PreviousOutcomes_L1_4gram_L2R", featureValue); features.add(feature); } return features; }  void initialize(UimaContext context); List<Feature> extractFeatures(List<Object> previousOutcomes); static final String PARAM_MOST_RECENT_OUTCOME; static final String PARAM_LEAST_RECENT_OUTCOME; static final String PARAM_USE_BIGRAM; static final String PARAM_USE_TRIGRAM; static final String PARAM_USE4GRAM; }
@Test public void testExtractFeatures() throws ResourceInitializationException { UimaContext context = UimaContextFactory.createUimaContext(); OutcomeFeatureExtractor ofe = new DefaultOutcomeFeatureExtractor(); ofe.initialize(context); List<Feature> features = ofe.extractFeatures(Arrays.asList(new Object[] { "A", "B", "C", "D" })); assertEquals(5, features.size()); Set<String> featureNames = new HashSet<String>(); for (Feature feature : features) { featureNames.add(feature.getName() + "_" + feature.getValue()); } assertTrue(featureNames.contains("PreviousOutcome_L1_D")); assertTrue(featureNames.contains("PreviousOutcome_L2_C")); assertTrue(featureNames.contains("PreviousOutcome_L3_B")); assertTrue(featureNames.contains("PreviousOutcomes_L1_2gram_L2R_D_C")); assertTrue(featureNames.contains("PreviousOutcomes_L1_3gram_L2R_D_C_B")); features = ofe.extractFeatures(Arrays.asList(new Object[] { "A", "B" })); assertEquals(3, features.size()); featureNames.clear(); for (Feature feature : features) { featureNames.add(feature.getName() + "_" + feature.getValue()); } assertTrue(featureNames.contains("PreviousOutcome_L1_B")); assertTrue(featureNames.contains("PreviousOutcome_L2_A")); assertTrue(featureNames.contains("PreviousOutcomes_L1_2gram_L2R_B_A")); features = ofe.extractFeatures(Arrays.asList(new Object[0])); assertEquals(0, features.size()); context = UimaContextFactory.createUimaContext( DefaultOutcomeFeatureExtractor.PARAM_MOST_RECENT_OUTCOME, 2, DefaultOutcomeFeatureExtractor.PARAM_LEAST_RECENT_OUTCOME, 3, DefaultOutcomeFeatureExtractor.PARAM_USE4GRAM, true, DefaultOutcomeFeatureExtractor.PARAM_USE_BIGRAM, false); ofe = new DefaultOutcomeFeatureExtractor(); ofe.initialize(context); features = ofe.extractFeatures(Arrays.asList(new Object[] { "A", "B", "C", "D" })); assertEquals(4, features.size()); featureNames.clear(); for (Feature feature : features) { featureNames.add(feature.getName() + "_" + feature.getValue()); } assertTrue(featureNames.contains("PreviousOutcome_L2_C")); assertTrue(featureNames.contains("PreviousOutcome_L3_B")); assertTrue(featureNames.contains("PreviousOutcomes_L1_3gram_L2R_D_C_B")); assertTrue(featureNames.contains("PreviousOutcomes_L1_4gram_L2R_D_C_B_A")); features = ofe.extractFeatures(Arrays.asList(new Object[] { 1, 3, "CAT", 5.5f })); assertEquals(4, features.size()); featureNames.clear(); for (Feature feature : features) { featureNames.add(feature.getName() + "_" + feature.getValue()); } assertTrue(featureNames.contains("PreviousOutcome_L2_CAT")); assertTrue(featureNames.contains("PreviousOutcome_L3_3")); assertTrue(featureNames.contains("PreviousOutcomes_L1_3gram_L2R_5.5_CAT_3")); assertTrue(featureNames.contains("PreviousOutcomes_L1_4gram_L2R_5.5_CAT_3_1")); }