Search in sources :

Example 6 with TextAnnotation

use of org.elasticsearch.service.opennlp.models.TextAnnotation in project elasticsearch-opennlp-plugin by spinscale.

the class SimpleNlpTest method testThatMultipleFindersWork.

@Test
public void testThatMultipleFindersWork() throws Exception {
    loadFinders();
    Map<String, Set<String>> namedEntities = Maps.newHashMap();
    for (int si = 0; si < sentences.length; si++) {
        List<TextAnnotation> allTextAnnotations = new ArrayList<TextAnnotation>();
        String[] tokens = tokenizer.tokenize(sentences[si]);
        for (int fi = 0; fi < finders.length; fi++) {
            Span[] spans = finders[fi].find(tokens);
            double[] probs = finders[fi].probs(spans);
            for (int ni = 0; ni < spans.length; ni++) {
                allTextAnnotations.add(new TextAnnotation(names[fi], spans[ni], probs[ni]));
            }
        }
        removeConflicts(allTextAnnotations);
        convertTextAnnotationsToNamedEntities(tokens, allTextAnnotations, namedEntities);
    }
    assertThat(namedEntities.get("person"), hasSize(3));
    assertThat(namedEntities.get("person"), containsInAnyOrder("Nancy Reagan", "Reagan", "Joanne Drake"));
    assertThat(namedEntities.get("location"), hasSize(3));
    assertThat(namedEntities.get("location"), containsInAnyOrder("Los Angeles", "Santa Monica", "California"));
    assertThat(namedEntities.get("date"), hasSize(1));
    assertThat(namedEntities.get("date"), containsInAnyOrder("Sunday"));
}
Also used : TextAnnotation(org.elasticsearch.service.opennlp.models.TextAnnotation) Span(opennlp.tools.util.Span) Test(org.junit.Test)

Aggregations

TextAnnotation (org.elasticsearch.service.opennlp.models.TextAnnotation)6 Span (opennlp.tools.util.Span)2 NameFinderME (opennlp.tools.namefind.NameFinderME)1 TokenNameFinderModel (opennlp.tools.namefind.TokenNameFinderModel)1 PooledTokenNameFinderModel (org.elasticsearch.service.opennlp.models.PooledTokenNameFinderModel)1 Test (org.junit.Test)1