Search in sources :

Example 6 with Annotation

use of com.yahoo.document.annotation.Annotation in project vespa by vespa-engine.

the class StringTestCase method annotate.

public Document annotate(Document document, DocumentTypeManager manager) {
    AnnotationTypeRegistry registry = manager.getAnnotationTypeRegistry();
    AnnotationType company = registry.getType("company");
    AnnotationType industry = registry.getType("industry");
    AnnotationType person = registry.getType("person");
    AnnotationType location = registry.getType("location");
    Map<String, AnnotationType> m = registry.getTypes();
    for (String key : m.keySet()) {
        System.out.println("Key: " + key);
        AnnotationType val = m.get(key);
        parseAnnotationType(val);
    }
    SpanTree tree = new SpanTree("testannotations");
    SpanList root = (SpanList) tree.getRoot();
    SpanNode companySpan = new Span(0, 5);
    SpanNode industrySpan = new Span(5, 10);
    SpanNode personSpan = new Span(10, 15);
    SpanNode locationSpan = new Span(15, 20);
    root.add(companySpan);
    root.add(industrySpan);
    root.add(personSpan);
    root.add(locationSpan);
    Struct companyValue = (Struct) company.getDataType().createFieldValue();
    companyValue.setFieldValue("name", new StringFieldValue("Sun"));
    companyValue.setFieldValue("ceo", new StringFieldValue("Scott Mcnealy"));
    companyValue.setFieldValue("lat", new DoubleFieldValue(37.7));
    companyValue.setFieldValue("lon", new DoubleFieldValue(-122.44));
    companyValue.setFieldValue("vertical", new StringFieldValue("software"));
    Annotation compAn = new Annotation(company, companyValue);
    tree.annotate(companySpan, compAn);
    Struct personValue = new Struct(manager.getDataType("annotation.person"));
    personValue.setFieldValue("name", new StringFieldValue("Richard Bair"));
    Annotation personAn = new Annotation(person, personValue);
    tree.annotate(personSpan, personAn);
    Struct locValue = new Struct(manager.getDataType("annotation.location"));
    locValue.setFieldValue("name", new StringFieldValue("Prinsens Gate"));
    Annotation loc = new Annotation(location, locValue);
    tree.annotate(locationSpan, loc);
    Struct locValue2 = new Struct(manager.getDataType("annotation.location"));
    locValue2.setFieldValue("name", new StringFieldValue("Kongens Gate"));
    Annotation locAn = new Annotation(location, locValue2);
    tree.annotate(locationSpan, locAn);
    SpanList branch = new SpanList();
    SpanNode span1 = new Span(0, 3);
    SpanNode span2 = new Span(1, 9);
    SpanNode span3 = new Span(12, 10);
    branch.add(span1);
    branch.add(span3);
    branch.add(span2);
    Struct industryValue = new Struct(manager.getDataType("annotation.industry"));
    industryValue.setFieldValue("vertical", new StringFieldValue("Manufacturing"));
    Annotation ind = new Annotation(industry, industryValue);
    tree.annotate(span1, ind);
    Struct pValue = new Struct(manager.getDataType("annotation.person"));
    pValue.setFieldValue("name", new StringFieldValue("Praveen Mohan"));
    Annotation pAn = new Annotation(person, pValue);
    tree.annotate(span2, pAn);
    Struct lValue = new Struct(manager.getDataType("annotation.location"));
    lValue.setFieldValue("name", new StringFieldValue("Embassy Golf Links"));
    Annotation locn = new Annotation(location, lValue);
    tree.annotate(span3, locn);
    Struct cValue = (Struct) company.getDataType().createFieldValue();
    cValue.setFieldValue("name", new StringFieldValue("Yahoo"));
    cValue.setFieldValue("ceo", new StringFieldValue("Carol Bartz"));
    cValue.setFieldValue("lat", new DoubleFieldValue(127.7));
    cValue.setFieldValue("lon", new DoubleFieldValue(-42.44));
    cValue.setFieldValue("vertical", new StringFieldValue("search"));
    Annotation cAn = new Annotation(company, cValue);
    tree.annotate(branch, cAn);
    Struct pVal = new Struct(manager.getDataType("annotation.person"));
    pVal.setFieldValue("name", new StringFieldValue("Kim Omar"));
    Annotation an = new Annotation(person, pVal);
    tree.annotate(root, an);
    root.add(branch);
    StringFieldValue body = (StringFieldValue) document.getFieldValue(document.getDataType().getField("body"));
    root.remove(branch);
    tree.cleanup();
    System.out.println("No. Of Annotations: " + tree.numAnnotations());
    body.setSpanTree(tree);
    document.setFieldValue(document.getField("body"), body);
    return document;
}
Also used : SpanNode(com.yahoo.document.annotation.SpanNode) SpanList(com.yahoo.document.annotation.SpanList) Span(com.yahoo.document.annotation.Span) AnnotationTypeRegistry(com.yahoo.document.annotation.AnnotationTypeRegistry) AnnotationType(com.yahoo.document.annotation.AnnotationType) Annotation(com.yahoo.document.annotation.Annotation) SpanTree(com.yahoo.document.annotation.SpanTree)

Example 7 with Annotation

use of com.yahoo.document.annotation.Annotation in project vespa by vespa-engine.

the class StringTestCase method testNestedSpanTreeBug4187377.

@Test
public void testNestedSpanTreeBug4187377() {
    AnnotationType type = new AnnotationType("ann", DataType.STRING);
    StringFieldValue outerString = new StringFieldValue("Ballooo");
    SpanTree outerTree = new SpanTree("outer");
    outerString.setSpanTree(outerTree);
    SpanList outerRoot = (SpanList) outerTree.getRoot();
    Span outerSpan = new Span(0, 1);
    outerRoot.add(outerSpan);
    StringFieldValue innerString = new StringFieldValue("innerBalloooo");
    outerTree.annotate(outerSpan, new Annotation(type, innerString));
    SpanTree innerTree = new SpanTree("inner");
    innerString.setSpanTree(innerTree);
    SpanList innerRoot = (SpanList) innerTree.getRoot();
    Span innerSpan = new Span(0, 1);
    innerRoot.add(innerSpan);
    innerTree.annotate(innerSpan, new Annotation(type));
    GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
    DocumentSerializer serializer = DocumentSerializerFactory.create42(buffer);
    try {
        serializer.write(null, outerString);
        fail("Should have failed, nested span trees are not supported.");
    } catch (SerializationException se) {
    // OK!
    }
}
Also used : GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) SpanList(com.yahoo.document.annotation.SpanList) Span(com.yahoo.document.annotation.Span) AnnotationType(com.yahoo.document.annotation.AnnotationType) Annotation(com.yahoo.document.annotation.Annotation) SpanTree(com.yahoo.document.annotation.SpanTree) Test(org.junit.Test) AbstractTypesTest(com.yahoo.document.annotation.AbstractTypesTest)

Example 8 with Annotation

use of com.yahoo.document.annotation.Annotation in project vespa by vespa-engine.

the class DocumentGenPluginTest method testFactory.

@Test
public void testFactory() {
    Book b = (Book) ConcreteDocumentFactory.getDocument("book", new DocumentId("doc:book:10"));
    b.setAuthor("Per Ulv");
    final Date d = (Date) ConcreteDocumentFactory.getAnnotation("date");
    d.setExacttime(79l);
    b.setAuthorSpanTrees(new HashMap<String, SpanTree>() {

        {
            put("root", new SpanTree("root").annotate(d));
        }
    });
    StringFieldValue authorCheck = (StringFieldValue) b.getFieldValue("author");
    assertEquals(authorCheck.getWrappedValue(), "Per Ulv");
    SpanTree treeCheck = authorCheck.getSpanTrees().iterator().next();
    Annotation authorAnnCheck = treeCheck.iterator().next();
    assertEquals(((Struct) authorAnnCheck.getFieldValue()).getFieldValue("exacttime").getWrappedValue(), 79l);
    b.setMystruct(((Ss1) ConcreteDocumentFactory.getStruct("ss1")).setS1("Test s1!"));
    assertEquals(((Struct) b.getFieldValue("mystruct")).getFieldValue("s1").getWrappedValue(), "Test s1!");
    Ss1 fss1 = (Ss1) ConcreteDocumentFactory.getStruct("ss1");
    fss1.setD1(678d);
    b.setMystruct(fss1);
    assertEquals(b.getMystruct().getFieldValue("d1").getWrappedValue(), 678d);
    assertEquals(b.getMystruct().getD1(), (Double) 678d);
    assertEquals(ConcreteDocumentFactory.documentTypeObjects.size(), 10);
    assertEquals(ConcreteDocumentFactory.documentTypeObjects.get("music"), Music.type);
    assertEquals(ConcreteDocumentFactory.documentTypeObjects.get("parent"), Parent.type);
    assertEquals(ConcreteDocumentFactory.documentTypeObjects.get("common"), Common.type);
}
Also used : Ss1(com.yahoo.vespa.documentgen.test.Book.Ss1) Date(com.yahoo.vespa.documentgen.test.annotation.Date) Annotation(com.yahoo.document.annotation.Annotation) SpanTree(com.yahoo.document.annotation.SpanTree) Test(org.junit.Test)

Example 9 with Annotation

use of com.yahoo.document.annotation.Annotation in project vespa by vespa-engine.

the class VespaDocumentDeserializer42 method read.

public void read(FieldBase field, AnnotationReference value) {
    int seqId = buf.getInt1_2_4Bytes();
    try {
        Annotation a = annotations.get(seqId);
        value.setReferenceNoCompatibilityCheck(a);
    } catch (IndexOutOfBoundsException iiobe) {
        throw new SerializationException("Could not serialize AnnotationReference value, reference not found.", iiobe);
    }
}
Also used : Annotation(com.yahoo.document.annotation.Annotation)

Example 10 with Annotation

use of com.yahoo.document.annotation.Annotation in project vespa by vespa-engine.

the class SchemaMappingAndAccessesTest method testMappingSpanTrees.

public void testMappingSpanTrees() {
    Document doc = getDoc();
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("t", "title");
    fieldMap.put("a", "artist");
    fieldMap.put("g", "guitarist");
    ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
    Iterator<SpanTree> itSpanTreesDoc = ((StringFieldValue) doc.getFieldValue("artist")).getSpanTrees().iterator();
    Iterator<Annotation> itAnnotDoc = itSpanTreesDoc.next().iterator();
    Iterator<SpanTree> itSpanTreesMapped = ((StringFieldValue) mapped.getFieldValue("artist")).getSpanTrees().iterator();
    Iterator<Annotation> itAnnotMapped = itSpanTreesMapped.next().iterator();
    assertEquals(itAnnotDoc.next().getType().getName(), "person");
    assertFalse(itAnnotDoc.hasNext());
    assertEquals(itAnnotMapped.next().getType().getName(), "person");
    assertFalse(itAnnotMapped.hasNext());
    AnnotationType guitaristType = new AnnotationType("guitarist");
    Annotation guitarist = new Annotation(guitaristType);
    StringFieldValue bona = new StringFieldValue("Bonamassa");
    bona.setSpanTree(new SpanTree("mytree").annotate(guitarist));
    StringFieldValue clapton = new StringFieldValue("Clapton");
    mapped.setFieldValue("a", bona);
    mapped.setFieldValue("g", clapton);
    itSpanTreesDoc = ((StringFieldValue) doc.getFieldValue("artist")).getSpanTrees().iterator();
    itAnnotDoc = itSpanTreesDoc.next().iterator();
    itSpanTreesMapped = ((StringFieldValue) mapped.getFieldValue("artist")).getSpanTrees().iterator();
    itAnnotMapped = itSpanTreesMapped.next().iterator();
    assertEquals(itAnnotDoc.next().getType().getName(), "guitarist");
    assertFalse(itAnnotDoc.hasNext());
    assertEquals(itAnnotMapped.next().getType().getName(), "guitarist");
    assertFalse(itAnnotMapped.hasNext());
    assertSame(((StringFieldValue) doc.getFieldValue("artist")).getSpanTrees().iterator().next(), ((StringFieldValue) mapped.getFieldValue("a")).getSpanTrees().iterator().next());
// assertSame(clapton, mapped.getFieldValue("g"));
// assertSame(bona, mapped.getFieldValue("a"));
}
Also used : HashMap(java.util.HashMap) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) Document(com.yahoo.document.Document) Annotation(com.yahoo.document.annotation.Annotation) AnnotationType(com.yahoo.document.annotation.AnnotationType) SpanTree(com.yahoo.document.annotation.SpanTree)

Aggregations

Annotation (com.yahoo.document.annotation.Annotation)20 SpanTree (com.yahoo.document.annotation.SpanTree)17 Test (org.junit.Test)13 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)12 AnnotationType (com.yahoo.document.annotation.AnnotationType)6 TokenType (com.yahoo.language.process.TokenType)6 Span (com.yahoo.document.annotation.Span)3 SimpleToken (com.yahoo.language.simple.SimpleToken)3 ProxyDocument (com.yahoo.docproc.proxy.ProxyDocument)2 Document (com.yahoo.document.Document)2 SpanList (com.yahoo.document.annotation.SpanList)2 SpanNode (com.yahoo.document.annotation.SpanNode)2 Linguistics (com.yahoo.language.Linguistics)2 SimpleLinguistics (com.yahoo.language.simple.SimpleLinguistics)2 TestDocumentProcessor1 (com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1)1 DocumentId (com.yahoo.document.DocumentId)1 DocumentType (com.yahoo.document.DocumentType)1 Field (com.yahoo.document.Field)1 StructDataType (com.yahoo.document.StructDataType)1 AbstractTypesTest (com.yahoo.document.annotation.AbstractTypesTest)1