Search in sources :

Example 66 with RDFTerm

use of org.apache.clerezza.commons.rdf.RDFTerm in project stanbol by apache.

the class TestKuromojiNlpEngine method testEngine.

@Test
public void testEngine() throws EngineException {
    LiteralFactory lf = LiteralFactory.getInstance();
    Assert.assertEquals(EnhancementEngine.ENHANCE_ASYNC, engine.canEnhance(contentItem));
    engine.computeEnhancements(contentItem);
    //assert the results
    Map<IRI, RDFTerm> expected = new HashMap<IRI, RDFTerm>();
    expected.put(Properties.DC_CREATOR, lf.createTypedLiteral(engine.getClass().getName()));
    expected.put(Properties.ENHANCER_EXTRACTED_FROM, contentItem.getUri());
    Assert.assertEquals(16, EnhancementStructureHelper.validateAllTextAnnotations(contentItem.getMetadata(), text, expected));
    AnalysedText at = AnalysedTextUtils.getAnalysedText(contentItem);
    Assert.assertNotNull(at);
    List<Sentence> sentences = AnalysedTextUtils.asList(at.getSentences());
    Assert.assertNotNull(sentences);
    Assert.assertEquals(7, sentences.size());
    //TODO: values in the following arrays are based on the first run of the
    // engine. So this is only to detect changes in results. It can not validate
    // that the tokenization and NER detections are correct - sorry I do not
    // speak Japanese ...
    int[] expectedChunks = new int[] { 5, 3, 1, 0, 1, 2, 4 };
    int[] expectedTokens = new int[] { 25, 25, 25, 24, 33, 17, 32 };
    int sentIndex = 0;
    for (Sentence sent : sentences) {
        List<Chunk> sentenceNer = AnalysedTextUtils.asList(sent.getChunks());
        Assert.assertEquals(expectedChunks[sentIndex], sentenceNer.size());
        for (Chunk chunk : sentenceNer) {
            Value<NerTag> nerValue = chunk.getAnnotation(NlpAnnotations.NER_ANNOTATION);
            Assert.assertNotNull(nerValue);
            Assert.assertNotNull(nerValue.value().getType());
        }
        List<Token> tokens = AnalysedTextUtils.asList(sent.getTokens());
        Assert.assertEquals(expectedTokens[sentIndex], tokens.size());
        for (Token token : tokens) {
            Value<PosTag> posValue = token.getAnnotation(NlpAnnotations.POS_ANNOTATION);
            Assert.assertNotNull(posValue);
        }
        sentIndex++;
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) NerTag(org.apache.stanbol.enhancer.nlp.ner.NerTag) HashMap(java.util.HashMap) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) Token(org.apache.stanbol.enhancer.nlp.model.Token) Chunk(org.apache.stanbol.enhancer.nlp.model.Chunk) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory) AnalysedText(org.apache.stanbol.enhancer.nlp.model.AnalysedText) PosTag(org.apache.stanbol.enhancer.nlp.pos.PosTag) Sentence(org.apache.stanbol.enhancer.nlp.model.Sentence) Test(org.junit.Test)

Example 67 with RDFTerm

use of org.apache.clerezza.commons.rdf.RDFTerm in project stanbol by apache.

the class LanguageDetectionEngineTest method testEngine.

/**
     * Test the engine and validates the created enhancements
     * @throws EngineException
     * @throws IOException
     * @throws ConfigurationException
     * @throws LangDetectException 
     */
@Test
public void testEngine() throws EngineException, ConfigurationException, LangDetectException, IOException {
    LOG.info("Testing engine: {}", TEST_FILE_NAMES[0]);
    InputStream in = LanguageDetectionEngineTest.class.getClassLoader().getResourceAsStream(TEST_FILE_NAMES[0]);
    assertNotNull("failed to load resource " + TEST_FILE_NAMES[0], in);
    String text = IOUtils.toString(in, "UTF-8");
    in.close();
    LanguageDetectionEnhancementEngine langIdEngine = new LanguageDetectionEnhancementEngine();
    ComponentContext context = new MockComponentContext();
    context.getProperties().put(EnhancementEngine.PROPERTY_NAME, "langdetect");
    langIdEngine.activate(context);
    ContentItem ci = ciFactory.createContentItem(new StringSource(text));
    langIdEngine.computeEnhancements(ci);
    HashMap<IRI, RDFTerm> expectedValues = new HashMap<IRI, RDFTerm>();
    expectedValues.put(Properties.ENHANCER_EXTRACTED_FROM, ci.getUri());
    expectedValues.put(Properties.DC_CREATOR, LiteralFactory.getInstance().createTypedLiteral(langIdEngine.getClass().getName()));
    int textAnnotationCount = validateAllTextAnnotations(ci.getMetadata(), text, expectedValues);
    assertTrue("A TextAnnotation is expected", textAnnotationCount > 0);
    //even through this tests do not validate detection quality
    //we expect the "en" is detected as best guess for the parsed text
    assertEquals("The detected language for text '" + text + "' MUST BE 'en'", "en", EnhancementEngineHelper.getLanguage(ci));
    int entityAnnoNum = validateAllEntityAnnotations(ci.getMetadata(), expectedValues);
    assertEquals("No EntityAnnotations are expected", 0, entityAnnoNum);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) ComponentContext(org.osgi.service.component.ComponentContext) HashMap(java.util.HashMap) InputStream(java.io.InputStream) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) Test(org.junit.Test)

Example 68 with RDFTerm

use of org.apache.clerezza.commons.rdf.RDFTerm in project stanbol by apache.

the class KeywordLinkingEngineTest method validateAllEntityAnnotations.

/**
     * Similar to {@link EnhancementStructureHelper#validateAllEntityAnnotations(org.apache.clerezza.commons.rdf.Graph, Map)}
     * but in addition checks fise:confidence [0..1] and entityhub:site properties
     * @param ci
     * @param expectedValues
     * @return
     */
private static int validateAllEntityAnnotations(ContentItem ci, Map<IRI, RDFTerm> expectedValues) {
    Iterator<Triple> entityAnnotationIterator = ci.getMetadata().filter(null, RDF_TYPE, ENHANCER_ENTITYANNOTATION);
    int entityAnnotationCount = 0;
    while (entityAnnotationIterator.hasNext()) {
        IRI entityAnnotation = (IRI) entityAnnotationIterator.next().getSubject();
        // test if selected Text is added
        validateEntityAnnotation(ci.getMetadata(), entityAnnotation, expectedValues);
        //validate also that the confidence is between [0..1]
        Iterator<Triple> confidenceIterator = ci.getMetadata().filter(entityAnnotation, ENHANCER_CONFIDENCE, null);
        //Confidence is now checked by the EnhancementStructureHelper (STANBOL-630)
        //            assertTrue("Expected fise:confidence value is missing (entityAnnotation "
        //                    +entityAnnotation+")",confidenceIterator.hasNext());
        //            Double confidence = LiteralFactory.getInstance().createObject(Double.class,
        //                (TypedLiteral)confidenceIterator.next().getObject());
        //            assertTrue("fise:confidence MUST BE <= 1 (value= '"+confidence
        //                    + "',entityAnnotation " +entityAnnotation+")",
        //                    1.0 >= confidence.doubleValue());
        //            assertTrue("fise:confidence MUST BE >= 0 (value= '"+confidence
        //                    +"',entityAnnotation "+entityAnnotation+")",
        //                    0.0 <= confidence.doubleValue());
        //Test the entityhub:site property (STANBOL-625)
        IRI ENTITYHUB_SITE = new IRI(RdfResourceEnum.site.getUri());
        Iterator<Triple> entitySiteIterator = ci.getMetadata().filter(entityAnnotation, ENTITYHUB_SITE, null);
        assertTrue("Expected entityhub:site value is missing (entityAnnotation " + entityAnnotation + ")", entitySiteIterator.hasNext());
        RDFTerm siteResource = entitySiteIterator.next().getObject();
        assertTrue("entityhub:site values MUST BE Literals", siteResource instanceof Literal);
        assertEquals("'" + TEST_REFERENCED_SITE_NAME + "' is expected as " + "entityhub:site value", TEST_REFERENCED_SITE_NAME, ((Literal) siteResource).getLexicalForm());
        assertFalse("entityhub:site MUST HAVE only a single value", entitySiteIterator.hasNext());
        entityAnnotationCount++;
    }
    return entityAnnotationCount;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) Literal(org.apache.clerezza.commons.rdf.Literal) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm)

Example 69 with RDFTerm

use of org.apache.clerezza.commons.rdf.RDFTerm in project stanbol by apache.

the class EnhancementStructureHelper method validateTextAnnotation.

/**
     * Validates fise:TextAnnotations. If <code>validatePrefixSuffix</code> is
     * enabled the fise:selection-prefix and fise:selection-suffix (as defined by
     * <a href="https://issues.apache.org/jira/browse/STANBOL-987">STANBOL-987</a>
     * are enforced and validated. If disabled those properties are not enforced but still
     * validated when present.
     * @param enhancements the enhancements graph containing the text annotation
     * @param textAnnotation the TextAnnotation to validate
     * @param content the enhanced content
     * @param expectedValues expected values (properties for the values are used as keys)
     * @param validatePrefixSuffix enforce the presence of fise:selection-prefix and 
     * fise:selection-suffix if fise:start and fise:end are set.
     */
public static void validateTextAnnotation(Graph enhancements, IRI textAnnotation, String content, Map<IRI, RDFTerm> expectedValues, boolean validatePrefixSuffix) {
    //validate the rdf:type
    Iterator<Triple> rdfTypeIterator = enhancements.filter(textAnnotation, RDF_TYPE, ENHANCER_TEXTANNOTATION);
    assertTrue("Parsed Enhancement " + textAnnotation + " is missing the fise:TextAnnotation type ", rdfTypeIterator.hasNext());
    Iterator<Triple> selectedTextIterator = enhancements.filter(textAnnotation, ENHANCER_SELECTED_TEXT, null);
    // check if the selected text is added (or not)
    RDFTerm selectedTextResource;
    if (selectedTextIterator.hasNext()) {
        // test if the selected text is part of the TEXT_TO_TEST
        selectedTextResource = selectedTextIterator.next().getObject();
        assertTrue("fise:selected-text MUST BE of type PlainLiteral (uri: " + textAnnotation + ")", selectedTextResource instanceof Literal);
        Literal selectedText = (Literal) selectedTextResource;
        assertTrue("The parsed content MUST contain the fise:selected-text value '" + selectedText.getLexicalForm() + "' (uri: " + textAnnotation + ")!", content.contains(selectedText.getLexicalForm()));
        Assert.assertFalse("fise:selected-text MUST be single valued (uri: " + textAnnotation + ")", selectedTextIterator.hasNext());
    } else {
        //no selected text
        selectedTextResource = null;
    }
    //check against an expected value
    RDFTerm expectedSelectedText = expectedValues.get(ENHANCER_SELECTED_TEXT);
    if (expectedSelectedText != null) {
        assertEquals("The fise:selected-text is not the expected value " + expectedSelectedText + " (uri: " + textAnnotation + ")!", expectedSelectedText, selectedTextResource);
    }
    //check for fise:selection-head and fise:selection-tail (STANBOL-987)
    Iterator<Triple> selectionHeadIterator = enhancements.filter(textAnnotation, Properties.ENHANCER_SELECTION_HEAD, null);
    if (selectedTextResource != null) {
        Assert.assertFalse("If fise:selected-text is present fise:selection-head MUST NOT be present", selectionHeadIterator.hasNext());
    }
    RDFTerm selectionHeadResource;
    if (selectionHeadIterator.hasNext()) {
        // test if the selected text is part of the TEXT_TO_TEST
        selectionHeadResource = selectionHeadIterator.next().getObject();
        assertTrue("fise:selection-head MUST BE of type PlainLiteral (uri: " + textAnnotation + ")", selectionHeadResource instanceof Literal);
        Literal selectionHeadText = (Literal) selectionHeadResource;
        assertTrue("The parsed content MUST contain the fise:selected-head value '" + selectionHeadText.getLexicalForm() + "' (uri: " + textAnnotation + ")!", content.contains(selectionHeadText.getLexicalForm()));
        Assert.assertFalse("fise:selection-head MUST be single valued (uri: " + textAnnotation + ")", selectionHeadIterator.hasNext());
    } else {
        selectionHeadResource = null;
    }
    Iterator<Triple> selectionTailIterator = enhancements.filter(textAnnotation, Properties.ENHANCER_SELECTION_TAIL, null);
    if (selectedTextResource != null) {
        Assert.assertFalse("If fise:selected-text is present fise:selection-tail MUST NOT be present", selectionTailIterator.hasNext());
    }
    RDFTerm selectionTailResource;
    if (selectionTailIterator.hasNext()) {
        // test if the selected text is part of the TEXT_TO_TEST
        selectionTailResource = selectionTailIterator.next().getObject();
        assertTrue("fise:selection-head MUST BE of type PlainLiteral (uri: " + textAnnotation + ")", selectionTailResource instanceof Literal);
        Literal selectionTailText = (Literal) selectionTailResource;
        assertTrue("The parsed content MUST contain the fise:selected-tail value '" + selectionTailText.getLexicalForm() + "' (uri: " + textAnnotation + ")!", content.contains(selectionTailText.getLexicalForm()));
        Assert.assertFalse("fise:selection-tail MUST be single valued (uri: " + textAnnotation + ")", selectionTailIterator.hasNext());
    } else {
        selectionTailResource = null;
    }
    Assert.assertTrue("Both fise:selection-tail AND fise:selection-head MUST BE defined " + "(if one of them is present) (uri: " + textAnnotation + ")", (selectionHeadResource != null && selectionTailResource != null) || (selectionHeadResource == null && selectionTailResource == null));
    RDFTerm selectionContextResource;
    // test if context is added
    Iterator<Triple> selectionContextIterator = enhancements.filter(textAnnotation, ENHANCER_SELECTION_CONTEXT, null);
    if (selectionContextIterator.hasNext()) {
        //context is optional
        //selection context is not allowed without selected-text
        assertTrue("If fise:selection-context is present also fise:selected-text or fise:selection-head and fise:selection-tail MUST BE present (uri: " + textAnnotation + ")", selectedTextResource != null || (selectionHeadResource != null && selectionTailResource != null));
        // test if the selected text is part of the TEXT_TO_TEST
        selectionContextResource = selectionContextIterator.next().getObject();
        assertTrue("The fise:selection-context MUST BE of type PlainLiteral (uri: " + textAnnotation + ")", selectionContextResource instanceof Literal);
        //check that the content contains the context
        assertTrue("The fise:selection-context MUST BE contained in the Content | context= " + selectionContextResource, content.contains(((Literal) selectionContextResource).getLexicalForm()));
        //check that the context contains the selected text
        if (selectedTextResource != null) {
            assertTrue("The fise:selected-text value MUST BE containted within the fise:selection-context value", ((Literal) selectionContextResource).getLexicalForm().contains(((Literal) selectedTextResource).getLexicalForm()));
        }
        if (selectionHeadResource != null) {
            assertTrue("The fise:selection-head value MUST BE containted within the fise:selection-context value", ((Literal) selectionContextResource).getLexicalForm().contains(((Literal) selectionHeadResource).getLexicalForm()));
        }
        if (selectionTailResource != null) {
            assertTrue("The fise:selection-tail value MUST BE containted within the fise:selection-context value", ((Literal) selectionContextResource).getLexicalForm().contains(((Literal) selectionTailResource).getLexicalForm()));
        }
    } else {
        assertNull("If no fise:selection-context is present also fise:selected-text MUST BE NOT present!", selectedTextResource);
        selectionContextResource = null;
    }
    RDFTerm expectedSelectionContext = expectedValues.get(ENHANCER_SELECTION_CONTEXT);
    if (expectedSelectionContext != null) {
        assertEquals("The value of fise:selection-context has not the expected value " + expectedSelectionContext, expectedSelectionContext, selectionContextResource);
    }
    //test start/end if present
    Iterator<Triple> startPosIterator = enhancements.filter(textAnnotation, ENHANCER_START, null);
    Iterator<Triple> endPosIterator = enhancements.filter(textAnnotation, ENHANCER_END, null);
    //start end is optional, but if start is present, that also end needs to be set
    Literal startPosLiteral;
    Literal endPosLiteral;
    if (startPosIterator.hasNext()) {
        //NOTE: TextAnnotations might be use to select whole sections of a text
        //      (e.g. see STANBOL-617) in those cases adding the text of the
        //      whole section is not feasible.
        //assertNotNull("If fise:start is present the fise:selection-context MUST also be present (uri: "+textAnnotation+")!",
        //    selectionContextResource);
        RDFTerm resource = startPosIterator.next().getObject();
        //only a single start position is supported
        assertFalse("fise:start MUST HAVE only a single value (uri: " + textAnnotation + ")!", startPosIterator.hasNext());
        assertTrue("fise:start MUST be a typed Literal (uri: " + textAnnotation + ")!", resource instanceof Literal);
        startPosLiteral = (Literal) resource;
        assertEquals("fise:start MUST use xsd:int as data type (uri: " + textAnnotation + ")", XSD.int_, startPosLiteral.getDataType());
        resource = null;
        Integer start = LiteralFactory.getInstance().createObject(Integer.class, startPosLiteral);
        assertNotNull("Unable to parse Integer from TypedLiteral " + startPosLiteral, start);
        //now get the end
        //end must be defined if start is present
        assertTrue("If fise:start is present also fise:end MUST BE defined (uri: " + textAnnotation + ")!", endPosIterator.hasNext());
        resource = endPosIterator.next().getObject();
        //only a single end position is supported
        assertFalse("fise:end MUST HAVE only a single value (uri: " + textAnnotation + ")!", endPosIterator.hasNext());
        assertTrue("fise:end values MUST BE TypedLiterals (uri: " + textAnnotation + ")", resource instanceof Literal);
        endPosLiteral = (Literal) resource;
        assertEquals("fise:end MUST use xsd:int as data type (uri: " + textAnnotation + ")", XSD.int_, endPosLiteral.getDataType());
        resource = null;
        Integer end = LiteralFactory.getInstance().createObject(Integer.class, endPosLiteral);
        assertNotNull("Unable to parse Integer from TypedLiteral " + endPosLiteral, end);
        //System.out.println("TA ["+start+"|"+end+"]"+selectedText.getLexicalForm()+"<->"+content.substring(start,end));
        if (selectedTextResource != null) {
            assertEquals("the substring [fise:start,fise:end] does not correspond to " + "the fise:selected-text value '" + ((Literal) selectedTextResource).getLexicalForm() + "' of this TextAnnotation!", content.substring(start, end), ((Literal) selectedTextResource).getLexicalForm());
        }
    // else no selected-text present ... unable to test this
    } else {
        assertNull("if fise:selected-text is present also fise:start AND fise:end MUST BE present!", selectedTextResource);
        assertNull("If fise:selection-context is present also fise:start AND fise:end MUST BE present!", selectionContextResource);
        assertFalse("if fise:end is presnet also fise:start AND fise:selection-context MUST BE present!", endPosIterator.hasNext());
        startPosLiteral = null;
        endPosLiteral = null;
    }
    RDFTerm expectedStartPos = expectedValues.get(ENHANCER_START);
    if (expectedStartPos != null) {
        assertEquals("The fise:start value is not the expected " + expectedStartPos, expectedStartPos, startPosLiteral);
    }
    RDFTerm expectedEndPos = expectedValues.get(ENHANCER_END);
    if (expectedEndPos != null) {
        assertEquals("The fise:end value is not the expected " + expectedEndPos, expectedEndPos, endPosLiteral);
    }
    //fise:selection-prefix and fise:selection-suffix (STANBOL-987)
    Literal prefixLiteral;
    Iterator<Triple> selectionPrefixIterator = enhancements.filter(textAnnotation, Properties.ENHANCER_SELECTION_PREFIX, null);
    if (startPosLiteral != null) {
        // check if the selectionPrefix text is present
        assertTrue("fise:selection-prefix property is missing for fise:TextAnnotation " + textAnnotation, selectionPrefixIterator.hasNext() || //to support old and new fise:TextAnnotation model
        !validatePrefixSuffix);
        // test if the selected text is part of the TEXT_TO_TEST
        if (selectionPrefixIterator.hasNext()) {
            RDFTerm selectionPrefixResource = selectionPrefixIterator.next().getObject();
            assertTrue("fise:selection-prefix MUST BE of type PlainLiteral (uri: " + textAnnotation + ")", selectionPrefixResource instanceof Literal);
            prefixLiteral = (Literal) selectionPrefixResource;
            assertTrue("The parsed content MUST contain the fise:selection-prefix value '" + prefixLiteral.getLexicalForm() + "' (uri: " + textAnnotation + ")!", content.contains(prefixLiteral.getLexicalForm()));
            assertFalse("fise:selection-prefix MUST BE single valued (uri: " + textAnnotation + ")!", selectionPrefixIterator.hasNext());
        } else {
            prefixLiteral = null;
        }
    } else {
        prefixLiteral = null;
    }
    Literal suffixLiteral;
    Iterator<Triple> selectionSuffixIterator = enhancements.filter(textAnnotation, Properties.ENHANCER_SELECTION_SUFFIX, null);
    if (endPosLiteral != null) {
        // check if the selectionPrefix text is present
        assertTrue("fise:selection-suffix property is missing for fise:TextAnnotation " + textAnnotation, selectionSuffixIterator.hasNext() || //to support old and new fise:TextAnnotation model
        !validatePrefixSuffix);
        if (selectionSuffixIterator.hasNext()) {
            // test if the selected text is part of the TEXT_TO_TEST
            RDFTerm selectionSuffixResource = selectionSuffixIterator.next().getObject();
            assertTrue("fise:selection-suffix MUST BE of type PlainLiteral (uri: " + textAnnotation + ")", selectionSuffixResource instanceof Literal);
            suffixLiteral = (Literal) selectionSuffixResource;
            assertTrue("The parsed content MUST contain the fise:selection-suffix value '" + suffixLiteral.getLexicalForm() + "' (uri: " + textAnnotation + ")!", content.contains(suffixLiteral.getLexicalForm()));
            assertFalse("fise:selection-suffix MUST BE single valued (uri: " + textAnnotation + ")!", selectionSuffixIterator.hasNext());
        } else {
            suffixLiteral = null;
        }
    } else {
        suffixLiteral = null;
    }
    Assert.assertTrue("Both fise:selection-prefix AND fise:selection-suffix need to be present " + "(if one of them is present) (uri: " + textAnnotation + ")", (suffixLiteral != null && prefixLiteral != null) || (suffixLiteral == null && prefixLiteral == null));
    if (prefixLiteral != null && selectedTextResource != null) {
        String occurrence = prefixLiteral.getLexicalForm() + ((Literal) selectedTextResource).getLexicalForm() + suffixLiteral.getLexicalForm();
        assertTrue("The parsed content MUST contain the concated value of fise:selection-prefix," + "fise:selected-text and fise:selection-suffix (value: '" + occurrence + "' (uri: " + textAnnotation + ")!", content.contains(occurrence));
    }
    if (prefixLiteral != null && selectionHeadResource != null) {
        String occurrence = prefixLiteral.getLexicalForm() + ((Literal) selectionHeadResource).getLexicalForm();
        assertTrue("The parsed content MUST contain the concated value of fise:selection-prefix," + "fise:selection-head (value: '" + occurrence + "' (uri: " + textAnnotation + ")!", content.contains(occurrence));
        occurrence = ((Literal) selectionTailResource).getLexicalForm() + suffixLiteral.getLexicalForm();
        assertTrue("The parsed content MUST contain the concated value of fise:selection-tail " + "and fise:selection-suffix (value: '" + occurrence + "' (uri: " + textAnnotation + ")!", content.contains(occurrence));
    }
    //validate fise:Enhancement specific rules
    validateEnhancement(enhancements, textAnnotation, expectedValues);
    //validate for special TextAnnotations
    validateLanguageAnnotations(enhancements, textAnnotation);
    validateNERAnnotations(enhancements, textAnnotation, selectedTextResource);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) Literal(org.apache.clerezza.commons.rdf.Literal) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm)

Example 70 with RDFTerm

use of org.apache.clerezza.commons.rdf.RDFTerm in project stanbol by apache.

the class EnhancementStructureHelper method validateEntityAnnotation.

/**
     * Checks if a fise:EntityAnnotation is valid. NOTE that this also validates
     * all fise:Enhancement related requirements by calling
     * {@link #validateEnhancement(Graph, IRI, Map)}
     * @param enhancements the enhancements graph
     * @param entityAnnotation the entity annotation to validate
     * @param expectedValues expected values (properties for the values are used as keys)
     */
public static void validateEntityAnnotation(Graph enhancements, IRI entityAnnotation, Map<IRI, RDFTerm> expectedValues) {
    Iterator<Triple> relationToTextAnnotationIterator = enhancements.filter(entityAnnotation, DC_RELATION, null);
    // check if the relation to the text annotation is set
    assertTrue(relationToTextAnnotationIterator.hasNext());
    while (relationToTextAnnotationIterator.hasNext()) {
        // test if the referred annotations are text annotations or
        // the referenced annotations is a fise:EntityAnnotation AND also a
        // dc:requires link is defined (STANBOL-766)
        IRI referredTextAnnotation = (IRI) relationToTextAnnotationIterator.next().getObject();
        assertTrue("fise:EntityAnnotations MUST BE dc:related to a fise:TextAnnotation OR dc:requires and dc:related to the same fise:EntityAnnotation", enhancements.filter(referredTextAnnotation, RDF_TYPE, ENHANCER_TEXTANNOTATION).hasNext() || (enhancements.filter(referredTextAnnotation, RDF_TYPE, ENHANCER_ENTITYANNOTATION).hasNext() && enhancements.filter(entityAnnotation, Properties.DC_REQUIRES, referredTextAnnotation).hasNext()));
    }
    // test if an entity is referred
    Iterator<Triple> entityReferenceIterator = enhancements.filter(entityAnnotation, ENHANCER_ENTITY_REFERENCE, null);
    assertTrue("fise:entity-reference MUST BE present! (EntityAnnotation: '" + entityAnnotation + "')'", entityReferenceIterator.hasNext());
    RDFTerm expectedReferencedEntity = expectedValues.get(ENHANCER_ENTITY_REFERENCE);
    while (entityReferenceIterator.hasNext()) {
        //check possible multiple references
        RDFTerm entityReferenceResource = entityReferenceIterator.next().getObject();
        // test if the reference is an URI
        assertTrue("fise:entity-reference value MUST BE of URIs", entityReferenceResource instanceof IRI);
        if (expectedReferencedEntity != null && expectedReferencedEntity.equals(entityReferenceResource)) {
            //found
            expectedReferencedEntity = null;
        }
    }
    assertNull("EntityAnnotation " + entityAnnotation + "fise:entity-reference has not the expected value " + expectedReferencedEntity + "!", expectedReferencedEntity);
    //test if the entity label is set
    Iterator<Triple> entityLabelIterator = enhancements.filter(entityAnnotation, ENHANCER_ENTITY_LABEL, null);
    assertTrue(entityLabelIterator.hasNext());
    RDFTerm expectedEntityLabel = expectedValues.get(ENHANCER_ENTITY_LABEL);
    while (entityLabelIterator.hasNext()) {
        RDFTerm entityLabelResource = entityLabelIterator.next().getObject();
        assertTrue("fise:entity-label values MUST BE PlainLiterals (EntityAnnotation: " + entityAnnotation + ")!", entityLabelResource instanceof Literal);
        if (expectedEntityLabel != null && expectedEntityLabel.equals(entityLabelResource)) {
            expectedEntityLabel = null;
        }
    }
    assertNull("The expected EntityLabel " + expectedEntityLabel + " was not found", expectedEntityLabel);
    //test the optional entity types
    Iterator<Triple> entityTypeIterator = enhancements.filter(entityAnnotation, Properties.ENHANCER_ENTITY_TYPE, null);
    RDFTerm expectedEntityType = expectedValues.get(Properties.ENHANCER_ENTITY_TYPE);
    if (entityTypeIterator.hasNext()) {
        RDFTerm entityTypeResource = entityTypeIterator.next().getObject();
        assertTrue("fise:entity-type values MUST BE URIs", entityTypeResource instanceof IRI);
        if (expectedEntityType != null && expectedEntityType.equals(entityTypeResource)) {
            //found
            expectedEntityType = null;
        }
    }
    assertNull("The expected fise:entity-type value " + expectedEntityType + " was not found!", expectedEntityType);
    //test all properties required by fise:Enhancement
    validateEnhancement(enhancements, entityAnnotation, expectedValues);
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Literal(org.apache.clerezza.commons.rdf.Literal) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm)

Aggregations

RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)126 IRI (org.apache.clerezza.commons.rdf.IRI)84 Triple (org.apache.clerezza.commons.rdf.Triple)70 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)48 Literal (org.apache.clerezza.commons.rdf.Literal)35 Test (org.junit.Test)35 HashSet (java.util.HashSet)30 HashMap (java.util.HashMap)28 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)26 Graph (org.apache.clerezza.commons.rdf.Graph)24 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)18 ArrayList (java.util.ArrayList)17 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)16 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)13 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)13 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)12 Collection (java.util.Collection)10 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)10 Lock (java.util.concurrent.locks.Lock)9 IOException (java.io.IOException)5