ContractionFinder implements Analyzer { public void analyze(Document document) { List<Sentence> sentences = document.getSentences(); for (Sentence sentence : sentences) { Span[] contractionsSpan; synchronized (this.contractionFinder) { contractionsSpan = contractionFinder.find(TextUtils .tokensToString(sentence.getTokens())); } List<Token> newTokens = sentence.getTokens(); for (int i = contractionsSpan.length - 1; i >= 0; i--) { int start = contractionsSpan[i].getStart(); String lexeme = sentence.getTokens().get(start).getLexeme(); String[] contractions = ContractionUtility.expand(lexeme); Token original = newTokens.remove(start); if(contractions != null) { for (int j = contractions.length - 1; j >= 0; j--) { Token token = new TokenImpl(original.getStart(), original.getEnd(), contractions[j]); newTokens.add(start, token); String caze = null; if (j == 0) caze = "B"; else if (j == contractions.length - 1) caze = "E"; else caze = "I"; token.addContext(Analyzers.CONTRACTION_FINDER, caze); } } else { LOGGER.debug("Missing contraction: " + lexeme); } } sentence.setTokens(newTokens); } } ContractionFinder(NameFinderME contractionFinder); void analyze(Document document);  }
@Test public void testAnalyze() throws FileNotFoundException { DocumentImpl document = new DocumentImpl(); String text = "A filha dela vai à tarde ao cinema do centro ."; document.setText(text); Sentence sentence = new SentenceImpl(0, text.length(),document); document.setSentences(Collections.singletonList(sentence)); String[] textArray = text.split(" "); List<Token> tokens = createTokens(textArray); sentence.setTokens(tokens); Span[] spans = { new Span(2, 3), new Span(4, 5), new Span(6, 7), new Span(8, 9) }; when(mockedContractionFinder.find(textArray)).thenReturn(spans); contractionFinder.analyze(document); assertEquals(15, document.getSentences().get(0).getTokens().size()); assertEquals("de", document.getSentences().get(0).getTokens().get(2) .getLexeme()); assertEquals("ela", document.getSentences().get(0).getTokens().get(3) .getLexeme()); }
@Test public void testAnalyzeNoContractions() throws FileNotFoundException { DocumentImpl document = new DocumentImpl(); String text = "A filha de ela vai a a tarde a o cinema de o centro ."; document.setText(text); Sentence sentence = new SentenceImpl(0, text.length(), document); document.setSentences(Collections.singletonList(sentence)); String[] textArray = text.split(" "); List<Token> tokens = createTokens(textArray); sentence.setTokens(tokens); Span[] spans = new Span[0]; when(mockedContractionFinder.find(textArray)).thenReturn(spans); contractionFinder.analyze(document); assertEquals(15, document.getSentences().get(0).getTokens().size()); assertEquals("de", document.getSentences().get(0).getTokens().get(2) .getLexeme()); assertEquals("ela", document.getSentences().get(0).getTokens().get(3) .getLexeme()); }