Search in sources :

Example 1 with Reference

use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.

the class EntityLinker method processRedirects.

/**
 * Processes {@link EntitySearcher#getRedirectField() redirect field} values for
 * the parsed suggestions based on the {@link RedirectProcessingMode}
 * as configured in the {@link #config}.<p>
 * The results of this method are stored within the parsed {@link Suggestion}s
 * @param suggestion The suggestion to process.
 */
private void processRedirects(Suggestion suggestion) {
    // if mode is IGNORE -> nothing to do
    if (config.getRedirectProcessingMode() == RedirectProcessingMode.IGNORE) {
        return;
    }
    // therefore there is a small internal state that stores this information
    if (suggestion.isRedirectedProcessed()) {
        // Redirects for ResultMatch are already processed ... ignore
        return;
    }
    Representation result = suggestion.getResult();
    Iterator<Reference> redirects = result.getReferences(config.getRedirectField());
    switch(config.getRedirectProcessingMode()) {
        case ADD_VALUES:
            while (redirects.hasNext()) {
                Reference redirect = redirects.next();
                if (redirect != null) {
                    Representation redirectedEntity = entitySearcher.get(redirect.getReference(), config.getSelectedFields());
                    if (redirectedEntity != null) {
                        for (Iterator<String> fields = redirectedEntity.getFieldNames(); fields.hasNext(); ) {
                            String field = fields.next();
                            result.add(field, redirectedEntity.get(field));
                        }
                    }
                    // set that the redirects where searched for this result
                    suggestion.setRedirectProcessed(true);
                }
            }
        case FOLLOW:
            while (redirects.hasNext()) {
                Reference redirect = redirects.next();
                if (redirect != null) {
                    Representation redirectedEntity = entitySearcher.get(redirect.getReference(), config.getSelectedFields());
                    if (redirectedEntity != null) {
                        // copy the original result score
                        redirectedEntity.set(RdfResourceEnum.resultScore.getUri(), result.get(RdfResourceEnum.resultScore.getUri()));
                        // set the redirect
                        suggestion.setRedirect(redirectedEntity);
                    }
                }
            }
        // nothing to do
        default:
    }
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation)

Example 2 with Reference

use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.

the class RdfRepresentation method remove.

@Override
public void remove(String field, Object parsedValue) {
    if (field == null) {
        throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
    } else if (field.isEmpty()) {
        throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
    }
    if (parsedValue == null) {
        log.warn("NULL parsed as value in remove method for symbol " + getId() + " and field " + field + " -> call ignored");
        return;
    }
    URI property = sesameFactory.createURI(field);
    Collection<Object> values = new ArrayList<Object>();
    ModelUtils.checkValues(factory, parsedValue, values);
    for (Object value : values) {
        if (value instanceof Value) {
            // native support for Sesame types!
            removeValue(property, (Value) value);
        } else if (value instanceof RdfWrapper) {
            // for Sesame RDF wrapper we can directly use the Value
            removeValue(property, ((RdfWrapper) value).getValue());
        } else if (value instanceof Reference) {
            removeValue(property, sesameFactory.createURI(((Reference) value).getReference()));
        } else if (value instanceof Text) {
            removeValue(property, sesameFactory.createLiteral(((Text) value).getText(), ((Text) value).getLanguage()));
        } else {
            // else add an typed Literal!
            removeValue(property, createTypedLiteral(value));
        }
    }
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ArrayList(java.util.ArrayList) Value(org.openrdf.model.Value) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) URI(org.openrdf.model.URI)

Example 3 with Reference

use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.

the class RdfRepresentationTest method testBNodeFiltering.

/**
 * Test for STANBOL-1301
 */
@Test
public void testBNodeFiltering() {
    URI concept = new URIImpl("http://example.org/mySkos#Concept123");
    Representation r = createRepresentation(concept.stringValue());
    assertTrue(r instanceof RdfRepresentation);
    RdfRepresentation rep = (RdfRepresentation) r;
    // add the example as listed in STANBOL-1301 to directly to the
    // Sesame Model backing the created Representation
    Model m = rep.getModel();
    m.add(concept, RDF.TYPE, SKOS.CONCEPT);
    m.add(concept, DCTERMS.IDENTIFIER, new LiteralImpl("123"));
    m.add(concept, SKOS.PREF_LABEL, new LiteralImpl("Concept123", "en"));
    BNode note1 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19576");
    m.add(concept, SKOS.SCOPE_NOTE, note1);
    m.add(note1, DCTERMS.CREATOR, new LiteralImpl("User1"));
    m.add(note1, DCTERMS.CREATED, new LiteralImpl("2013-03-03T02:02:02Z", XMLSchema.DATETIME));
    m.add(note1, RDFS.COMMENT, new LiteralImpl("The scope of this example global", "en"));
    BNode note2 = new BNodeImpl("5d8580be71044a88bcfe9852d1e9cfb6node17c4j452vx19634");
    m.add(concept, SKOS.SCOPE_NOTE, note2);
    m.add(note2, DCTERMS.CREATOR, new LiteralImpl("User2"));
    m.add(note2, DCTERMS.CREATED, new LiteralImpl("2013-03-03T04:04:04Z", XMLSchema.DATETIME));
    m.add(note2, RDFS.COMMENT, new LiteralImpl("Der Geltungsbereich ist Global", "de"));
    // now assert that BNodes are not reported via the Representation API
    Iterator<Object> scopeNotes = rep.get(SKOS.SCOPE_NOTE.stringValue());
    assertFalse(scopeNotes.hasNext());
    Iterator<Reference> scopeNoteRefs = rep.getReferences(SKOS.SCOPE_NOTE.stringValue());
    assertFalse(scopeNoteRefs.hasNext());
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) BNode(org.openrdf.model.BNode) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Model(org.openrdf.model.Model) URIImpl(org.openrdf.model.impl.URIImpl) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) URI(org.openrdf.model.URI) BNodeImpl(org.openrdf.model.impl.BNodeImpl) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 4 with Reference

use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.

the class RdfIndexingSourceTest method testReference.

private void testReference(Representation rep) {
    for (Iterator<String> fields = rep.getFieldNames(); fields.hasNext(); ) {
        String field = fields.next();
        Iterator<Reference> values = rep.getReferences(field);
        // assertTrue(values.hasNext());
        while (values.hasNext()) {
            Reference ref = values.next();
            assertNotNull(ref);
        }
    }
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference)

Example 5 with Reference

use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.

the class BaseGoogleRefineReconcileResource method reconcile.

private JSONObject reconcile(String siteId, ReconcileQuery rQuery) throws JSONException, EntityhubException {
    FieldQuery query = createFieldQuery(siteId);
    query.addSelectedFields(SELECTED_FIELDS);
    addNameConstraint(rQuery, query);
    addTypeConstraint(rQuery, query);
    addPropertyConstraints(rQuery, query);
    query.setLimit(query.getLimit());
    QueryResultList<Representation> results = performQuery(siteId, query);
    List<JSONObject> jResultList = new ArrayList<JSONObject>(results.size());
    // we need to know the highest score to normalise between [0..1]
    double maxQueryScore = -1;
    if (!results.isEmpty()) {
        for (Representation r : results) {
            if (maxQueryScore < 0) {
                maxQueryScore = r.getFirst(resultScore.getUri(), Number.class).doubleValue();
            }
            JSONObject jResult = new JSONObject();
            jResult.put("id", r.getId());
            double similarity = 0.0;
            // the name returned for the entity
            String name = null;
            for (Iterator<Text> labels = r.getText(NAME_FIELD); labels.hasNext(); ) {
                Text label = labels.next();
                if (label.getText().equalsIgnoreCase(rQuery.getQuery())) {
                    name = label.getText();
                    similarity = 1.0;
                    break;
                }
                double curSimilarity = Utils.levenshtein(rQuery.getQuery(), label.getText());
                if (similarity < curSimilarity) {
                    name = label.getText();
                    similarity = curSimilarity;
                }
            }
            // set the selected name
            jResult.put("name", name);
            Iterator<Reference> types = r.getReferences(TYPE_FIELD);
            if (types != null && types.hasNext()) {
                jResult.put("type", new JSONArray(ModelUtils.asCollection(types)));
            }
            double normalisedScore = r.getFirst(resultScore.getUri(), Number.class).doubleValue();
            normalisedScore = normalisedScore * similarity / maxQueryScore;
            jResult.put("score", normalisedScore);
            jResult.put("match", similarity >= 0);
            jResultList.add(jResult);
        }
    }
    // else no results ... nothing todo
    // sort results based on score
    Collections.sort(jResultList, resultScoreComparator);
    JSONObject jResultContainer = new JSONObject();
    jResultContainer.put("result", new JSONArray(jResultList));
    return jResultContainer;
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ArrayList(java.util.ArrayList) JSONArray(org.codehaus.jettison.json.JSONArray) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) JSONObject(org.codehaus.jettison.json.JSONObject)

Aggregations

Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)30 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)12 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)7 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)7 IRI (org.apache.clerezza.commons.rdf.IRI)5 URI (java.net.URI)4 URL (java.net.URL)3 URI (org.openrdf.model.URI)3 HashSet (java.util.HashSet)2 Language (org.apache.clerezza.commons.rdf.Language)2 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)2 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)2 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)2 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)2 YardTest (org.apache.stanbol.entityhub.test.yard.YardTest)2 JSONObject (org.codehaus.jettison.json.JSONObject)2 Value (org.openrdf.model.Value)2 Date (java.util.Date)1