POSTagger implements Analyzer { public void analyze(Document document) { List<Sentence> sentences = document.getSentences(); for (Sentence sentence : sentences) { List<Token> tokens = sentence.getTokens(); String[] tags; double[] probs; String[][] ac = TextUtils.additionalContext(tokens, Arrays.asList(Analyzers.CONTRACTION_FINDER, Analyzers.NAME_FINDER)); String[] toks = TextUtils.tokensToString(sentence.getTokens()); synchronized (this.tagger) { tags = tagger.tag(toks, ac); probs = tagger.probs(); } double finalProb = computeFinalProb(probs); sentence.setTokensProb(finalProb); if (LOGGER.isDebugEnabled()) { StringBuilder sb = new StringBuilder("Probabilidades do tagger:\n"); for (int i = 0; i < toks.length; i++) { sb.append("[").append(toks[i]).append("_").append(tags[i]) .append(" ").append(probs[i]).append("] "); } LOGGER.debug(sb.toString()); LOGGER.debug("Soma dos logs das probabilidades: " + finalProb); } tags = GenderUtil.removeGender(tags); for (int i = 0; i < tags.length; i++) { tokens.get(i).setPOSTag(tags[i]); tokens.get(i).setPOSTagProb(probs[i]); } EntityUtils.groupTokens(sentence.getText(), tokens, createSpanList(toTokensArray(tokens), toTagsArray(tokens))); mergeHyphenedWords(sentence); } } POSTagger(POSTaggerME tagger); void analyze(Document document); static List<Span> createSpanList(String[] toks, String[] tags);  }
@Test public void testAnalyze() throws FileNotFoundException { DocumentImpl document = new DocumentImpl(); String text = "A menina pequena foi para o seu quarto ."; document.setText(text); String[] textArray = text.split(" "); Sentence sentence = new SentenceImpl(0, 40, document); document.setSentences(Collections.singletonList(sentence)); List<Token> tokens = createTokens(textArray); sentence.setTokens(tokens); String[] tags = { "art", "n", "adj", "v-fin", "prp", "art", "pron-det", "n", "punc" }; double[] probs = { 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 }; when(mockedTagger.tag(any(String[].class), any(Object[].class))).thenReturn(tags); when(mockedTagger.probs()).thenReturn(probs); tagger.analyze(document); assertEquals("art", document.getSentences().get(0).getTokens().get(0) .getPOSTag()); assertEquals("n", document.getSentences().get(0).getTokens().get(1) .getPOSTag()); assertEquals("adj", document.getSentences().get(0).getTokens().get(2) .getPOSTag()); assertEquals("v-fin", document.getSentences().get(0).getTokens().get(3) .getPOSTag()); assertEquals("prp", document.getSentences().get(0).getTokens().get(4) .getPOSTag()); assertEquals("art", document.getSentences().get(0).getTokens().get(5) .getPOSTag()); assertEquals("pron-det", document.getSentences().get(0).getTokens().get(6) .getPOSTag()); assertEquals("n", document.getSentences().get(0).getTokens().get(7) .getPOSTag()); assertEquals("punc", document.getSentences().get(0).getTokens().get(8) .getPOSTag()); }