HeadNounNormalizer implements FieldNormalizer { @Override public NormalizedField normalizeField(ChunkedExtraction field) { boolean containsProperNoun = false; int lastNounIndex = -1; for (int i = 0; i < field.getLength(); i++) { String tag = field.getPosTag(i); if (tag.equals("NNP") || tag.equals("NNPS")) { containsProperNoun = true; } if (tag.startsWith("N")) { lastNounIndex = i; } } NormalizedField norm; if (containsProperNoun || lastNounIndex == -1) { try { norm = new NormalizedField(field, field.getTokens(), field.getPosTags()); } catch (SequenceException e) { String msg = String .format("tokens and posTags are not the same length for field %s", field); throw new IllegalStateException(msg, e); } } else { String token = field.getToken(lastNounIndex); String posTag = field.getPosTag(lastNounIndex); String normToken = stem(token, posTag); String[] tokens = { normToken }; String[] posTags = { posTag }; try { norm = new NormalizedField(field, tokens, posTags); } catch (SequenceException e) { String msg = String .format("tokens and posTags are not the same length for field %s", field); throw new IllegalStateException(msg, e); } } return norm; } HeadNounNormalizer(); @Override NormalizedField normalizeField(ChunkedExtraction field);  }
@Test public void testNormalizeField() { HeadNounNormalizer normalizer = new HeadNounNormalizer(); assertEquals("The President of the United States", normalizer.normalizeField(field1).toString()); assertEquals("shoe", normalizer.normalizeField(field2).toString()); assertEquals("blue", normalizer.normalizeField(field3).toString()); }