Search in sources :

Example 1 with AnnotationClass

use of com.yahoo.text.interpretation.AnnotationClass in project vespa by vespa-engine.

the class AnnotationTestCase method testAnnotationAPI.

public void testAnnotationAPI() {
    Interpretation a = new Interpretation("new york hotel");
    a.annotate(0, 3, "token");
    a.annotate(0, 8, "state").put("name", "New York");
    a.annotate(0, 8, "state").put("country", "US");
    a.annotate(0, 8, "state").put("coast", "east");
    a.annotate(9, 14, "business");
    a.annotate(4, 8, "token");
    a.annotate(9, 14, "token");
    for (Span span : a.getTokens()) {
        assertTrue(span.hasClass(new AnnotationClass("token")));
    }
    Set<AnnotationClass> annotationClasses = a.getClasses(0, 3);
    Set<AnnotationClass> testClass = new HashSet<>(Arrays.asList(new AnnotationClass("token"), new AnnotationClass("state")));
    assertEquals(testClass, annotationClasses);
    assertNull(a.get("state", "country"));
    assertEquals("US", a.get(0, 8, "state", "country"));
    assertEquals("new york", a.root().getSubSpans().get(0).getText());
    assertEquals("hotel", a.root().getSubSpans().get(1).getText());
    assertEquals(2, a.root().getSubSpans().size());
    // Test scoring
    a.setProbability(5);
    Interpretation b = new Interpretation("new york hotel");
    b.setProbability(3);
    // Test the interpretation API
    a.annotate("vespa_query");
    assertNotNull(a.get("vespa_query"));
    // This is bad about the API, getTokens may not necessairily return what a user thinks a token is
    // But it should still be tested
    a.annotate(0, 1, "n");
    Set<String> testSet = new HashSet<>(Arrays.asList("n", "york", "hotel"));
    for (Span span : a.getTokens()) {
        assertTrue(testSet.remove(span.getText()));
    }
    assertEquals(0, testSet.size());
}
Also used : Interpretation(com.yahoo.text.interpretation.Interpretation) Span(com.yahoo.text.interpretation.Span) AnnotationClass(com.yahoo.text.interpretation.AnnotationClass) HashSet(java.util.HashSet)

Aggregations

AnnotationClass (com.yahoo.text.interpretation.AnnotationClass)1 Interpretation (com.yahoo.text.interpretation.Interpretation)1 Span (com.yahoo.text.interpretation.Span)1 HashSet (java.util.HashSet)1