NameFinder implements Analyzer { public void analyze(Document document) { List<Sentence> sentences = document.getSentences(); for (Sentence sentence : sentences) { Span[] namesSpan; synchronized (this.nameFinder) { namesSpan = nameFinder.find(TextUtils.tokensToString(sentence .getTokens())); } List<Token> newTokens = sentence.getTokens(); for (int i = namesSpan.length - 1; i >= 0; i--) { int start = namesSpan[i].getStart(), end = namesSpan[i].getEnd(); int chStart = newTokens.get(start).getStart(); int chEnd = newTokens.get(end - 1).getEnd(); String name = sentence.getText().substring(chStart, chEnd).replace(" ", "_"); newTokens.remove(end - 1); for (int j = end - 2; j >= start; j--) { newTokens.remove(j); } Token token = new TokenImpl(chStart, chEnd, name); newTokens.add(start, token); token.addContext(Analyzers.NAME_FINDER, "P"); } sentence.setTokens(newTokens); } } NameFinder(NameFinderME nameFinder); void analyze(Document document);  }
@Test public void testAnalyze() throws FileNotFoundException { DocumentImpl document = new DocumentImpl(); String text = "O Sr. Luis Carlos faleceu ontem ."; String[] textArray = text.split(" "); document.setText(text); Sentence sentence = new SentenceImpl(0, 33,document); document.setSentences(Collections.singletonList(sentence)); Span[] spans = { new Span(1, 4) }; List<Token> tokens = createTokens(textArray); sentence.setTokens(tokens); when(mockedNameFinder.find(textArray)).thenReturn(spans); nameFinder.analyze(document); assertEquals("Sr._Luis_Carlos", document.getSentences().get(0).getTokens() .get(1).getLexeme()); }
@Test public void testAnalyzeManyNames() throws FileNotFoundException { DocumentImpl document = new DocumentImpl(); String text = "O Sr. Luis Carlos e sua esposa Ana Maria foram para a praia ."; String[] textArray = text.split(" "); document.setText(text); Sentence sentence = new SentenceImpl(0, 61, document); document.setSentences(Collections.singletonList(sentence)); Span[] spans = { new Span(1, 4), new Span(7, 9) }; List<Token> tokens = createTokens(textArray); sentence.setTokens(tokens); when(mockedNameFinder.find(textArray)).thenReturn(spans); nameFinder.analyze(document); assertEquals("Sr._Luis_Carlos", document.getSentences().get(0).getTokens() .get(1).getLexeme()); assertEquals("Ana_Maria", document.getSentences().get(0).getTokens().get(5) .getLexeme()); }
@Test public void testAnalyzeNoNames() throws FileNotFoundException { DocumentImpl document = new DocumentImpl(); String text = "Eles saíram ."; String[] textArray = text.split(" "); document.setText(text); Sentence sentence = new SentenceImpl(0, 61, document); document.setSentences(Collections.singletonList(sentence)); Span[] spans = new Span[0]; List<Token> tokens = createTokens(textArray); sentence.setTokens(tokens); when(mockedNameFinder.find(textArray)).thenReturn(spans); nameFinder.analyze(document); assertEquals(3, document.getSentences().get(0).getTokens().size()); }