Search in sources :

Example 51 with Literal

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

the class Suggestion method getBestLabel.

/**
     * Getter for the best label in the given language
     * @param suggestion the suggestion
     * @param nameField the field used to search for labels
     * @param language the language
     * @return the best match or {@link Suggestion#getMatchedLabel()} if non is found
     */
public Literal getBestLabel(IRI nameField, String language) {
    Entity rep = getEntity();
    //start with the matched label -> so if we do not find a better one
    //we will use the matched!
    Literal matchedLabel = getMatchedLabel();
    Literal label = matchedLabel;
    // 1. check if the returned Entity does has a label -> if not return null
    // add labels (set only a single label. Use "en" if available!
    Iterator<Literal> labels = rep.getText(nameField);
    boolean matchFound = false;
    while (labels.hasNext() && !matchFound) {
        Literal actLabel = labels.next();
        if (label == null) {
            label = actLabel;
        }
        //now we have already a label check the language
        Language actLang = actLabel.getLanguage();
        //use startWith to match also en-GB and en-US ...
        if (actLang != null && actLang.toString().startsWith(language)) {
            //prefer labels with the correct language
            label = actLabel;
            if (matchedLabel != null && matchedLabel.getLexicalForm().equalsIgnoreCase(label.getLexicalForm())) {
                //found label in that language that exactly matches the
                //label used to match the text
                matchFound = true;
            }
        }
    }
    return label;
}
Also used : Entity(org.apache.stanbol.enhancer.engines.entitylinking.Entity) Language(org.apache.clerezza.commons.rdf.Language) Literal(org.apache.clerezza.commons.rdf.Literal)

Example 52 with Literal

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

the class EntityLinkingEngineTest 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(NamespaceEnum.entityhub + "site");
        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 53 with Literal

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

the class TestSearcherImpl method addEntity.

public void addEntity(Entity rep) {
    entities.put(rep.getUri(), rep);
    Iterator<Literal> labels = rep.getText(nameField);
    while (labels.hasNext()) {
        Literal label = labels.next();
        for (String token : tokenizer.tokenize(label.getLexicalForm(), null)) {
            Collection<Entity> values = data.get(token);
            if (values == null) {
                values = new ArrayList<Entity>();
                data.put(label.getLexicalForm(), values);
            }
            values.add(rep);
        }
    }
}
Also used : Entity(org.apache.stanbol.enhancer.engines.entitylinking.Entity) Literal(org.apache.clerezza.commons.rdf.Literal)

Example 54 with Literal

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

the class ExecutionPlanHelper method writeEnhancementProperty.

/**
     * Writes enhancement property value(s) for the parsed node, property to the
     * execution plan graph.
     * @param ep the RDF graph holding the execution plan
     * @param epNode the execution node
     * @param property the property
     * @param value the value(s). {@link Collection} and <code>Object[]</code> are
     * supported for multiple values.
     * @throws NullPointerException if any of the parsed parameter is <code>null</code>
     */
@SuppressWarnings("unchecked")
private static void writeEnhancementProperty(Graph ep, BlankNodeOrIRI epNode, IRI property, Object value) {
    Collection<Object> values;
    if (value instanceof Collection<?>) {
        values = (Collection<Object>) value;
    } else if (value instanceof Object[]) {
        values = Arrays.asList((Object[]) value);
    } else {
        values = Collections.singleton(value);
    }
    for (Object v : values) {
        if (v != null) {
            Literal literal;
            if (v instanceof String) {
                literal = new PlainLiteralImpl((String) v);
            } else {
                try {
                    literal = lf.createTypedLiteral(v);
                } catch (NoConvertorException e) {
                    log.warn("Use toString() value '{}' for EnhancementProperty " + "'{}' as no TypedLiteral converter is registered for " + "class {}", new Object[] { v, property, v.getClass().getName() });
                    literal = new PlainLiteralImpl(v.toString());
                }
            }
            ep.add(new TripleImpl(epNode, property, literal));
        }
    }
}
Also used : PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) Literal(org.apache.clerezza.commons.rdf.Literal) NoConvertorException(org.apache.clerezza.rdf.core.NoConvertorException) Collection(java.util.Collection) EnhancementEngineHelper.getString(org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 55 with Literal

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

the class ExecutionMetadataHelper method setExecutionFaild.

/**
     * Set the parsed execution node to failed.
     * @param graph
     * @param execution
     * @param message An message describing why the execution failed
     */
public static void setExecutionFaild(Graph graph, BlankNodeOrIRI execution, String message) {
    Literal dateTime = lf.createTypedLiteral(new Date());
    setStatus(graph, execution, STATUS_FAILED);
    graph.add(new TripleImpl(execution, COMPLETED, dateTime));
    if (message != null) {
        graph.add(new TripleImpl(execution, STATUS_MESSAGE, new PlainLiteralImpl(message)));
    } else {
        throw new IllegalArgumentException("For faild Execution a STATUS message is required!");
    }
}
Also used : PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) Literal(org.apache.clerezza.commons.rdf.Literal) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Date(java.util.Date)

Aggregations

Literal (org.apache.clerezza.commons.rdf.Literal)71 IRI (org.apache.clerezza.commons.rdf.IRI)35 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)35 Triple (org.apache.clerezza.commons.rdf.Triple)30 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)22 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)20 ArrayList (java.util.ArrayList)16 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)16 Language (org.apache.clerezza.commons.rdf.Language)12 Graph (org.apache.clerezza.commons.rdf.Graph)11 Test (org.junit.Test)10 HashSet (java.util.HashSet)9 Date (java.util.Date)8 Lock (java.util.concurrent.locks.Lock)6 Entity (org.apache.stanbol.enhancer.engines.entitylinking.Entity)5 HashMap (java.util.HashMap)4 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)4 NoConvertorException (org.apache.clerezza.rdf.core.NoConvertorException)4 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)4 Collection (java.util.Collection)3