Search in sources :

Example 21 with Reference

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

the class JsonModelWriter method convertFieldValueToJSON.

/**
     * The value to write. Special support for  {@link Reference} and {@link Text}.
     * The {@link #toString()} Method is used to write the "value" key.
     *
     * @param value the value
     * @return the {@link JSONObject} representing the value
     * @throws JSONException
     */
private JSONObject convertFieldValueToJSON(Object value) throws JSONException {
    JSONObject jValue = new JSONObject();
    if (value instanceof Reference) {
        jValue.put("type", "reference");
        jValue.put("xsd:datatype", DataTypeEnum.AnyUri.getShortName());
        jValue.put("value", ((Reference) value).getReference());
    } else if (value instanceof Text) {
        jValue.put("type", "text");
        jValue.put("xml:lang", ((Text) value).getLanguage());
        jValue.put("value", ((Text) value).getText());
    } else if (value instanceof Date) {
        jValue.put("type", "value");
        jValue.put("value", TimeUtils.toString(DataTypeEnum.DateTime, (Date) value));
        jValue.put("xsd:datatype", DataTypeEnum.DateTime.getShortName());
    } else {
        jValue.put("type", "value");
        Set<DataTypeEnum> dataTypes = DataTypeEnum.getPrimaryDataTypes(value.getClass());
        if (!dataTypes.isEmpty()) {
            jValue.put("xsd:datatype", dataTypes.iterator().next().getShortName());
        } else {
            jValue.put("xsd:datatype", DataTypeEnum.String.getShortName());
        }
        jValue.put("value", value);
    }
    return jValue;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) DataTypeEnum(org.apache.stanbol.entityhub.servicesapi.defaults.DataTypeEnum) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) Date(java.util.Date)

Example 22 with Reference

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

the class RdfRepresentation method add.

//    protected IRI getRepresentationType(){
//        Iterator<IRI> it = this.graphNode.getIRIObjects(REPRESENTATION_TYPE_PROPERTY);
//        return it.hasNext()?it.next():null;
//    }
@Override
public void add(String field, Object value) {
    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 (value == null) {
        throw new IllegalArgumentException("NULL values are not supported by Representations");
    }
    IRI fieldIRI = new IRI(field);
    Collection<Object> values = new ArrayList<Object>();
    //process the parsed value with the Utility Method ->
    // this converts Objects as defined in the specification
    ModelUtils.checkValues(valueFactory, value, values);
    //We still need to implement support for specific types supported by this implementation
    for (Object current : values) {
        if (current instanceof RDFTerm) {
            //native support for Clerezza types!
            graphNode.addProperty(fieldIRI, (RDFTerm) current);
        } else if (current instanceof RdfReference) {
            //treat RDF Implementations special to avoid creating new instances
            graphNode.addProperty(fieldIRI, ((RdfReference) current).getIRI());
        } else if (current instanceof Reference) {
            graphNode.addProperty(fieldIRI, new IRI(((Reference) current).getReference()));
        } else if (current instanceof RdfText) {
            //treat RDF Implementations special to avoid creating new instances
            graphNode.addProperty(fieldIRI, ((RdfText) current).getLiteral());
        } else if (current instanceof Text) {
            addNaturalText(fieldIRI, ((Text) current).getText(), ((Text) current).getLanguage());
        } else {
            //else add an typed Literal!
            addTypedLiteral(fieldIRI, current);
        }
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ArrayList(java.util.ArrayList) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) Text(org.apache.stanbol.entityhub.servicesapi.model.Text)

Example 23 with Reference

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

the class RdfRepresentation method add.

@Override
public void add(String field, Object value) throws IllegalArgumentException {
    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 (value == null) {
        throw new IllegalArgumentException("NULL values are not supported by Representations");
    }
    URI property = sesameFactory.createURI(field);
    Collection<Object> values = new ArrayList<Object>();
    //process the parsed value with the Utility Method ->
    // this converts Objects as defined in the specification
    ModelUtils.checkValues(factory, value, values);
    //We still need to implement support for specific types supported by this implementation
    for (Object current : values) {
        if (current instanceof Value) {
            //native support for Sesame types!
            addValue(property, (Value) current);
        } else if (current instanceof RdfWrapper) {
            //for Sesame RDF wrapper we can directly use the Value
            addValue(property, ((RdfWrapper) current).getValue());
        } else if (current instanceof Reference) {
            addValue(property, sesameFactory.createURI(((Reference) current).getReference()));
        } else if (current instanceof Text) {
            addValue(property, sesameFactory.createLiteral(((Text) current).getText(), ((Text) current).getLanguage()));
        } else {
            //else add an typed Literal!
            addValue(property, createTypedLiteral(current));
        }
    }
}
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 24 with Reference

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

the class KeywordLinkingEngine method writeEnhancements.

/**
     * Writes the Enhancements for the {@link LinkedEntity LinkedEntities}
     * extracted from the parsed ContentItem
     * @param ci
     * @param linkedEntities
     * @param language
     */
private void writeEnhancements(ContentItem ci, Collection<LinkedEntity> linkedEntities, String language) {
    Language languageObject = null;
    if (language != null && !language.isEmpty()) {
        languageObject = new Language(language);
    }
    Graph metadata = ci.getMetadata();
    for (LinkedEntity linkedEntity : linkedEntities) {
        Collection<IRI> textAnnotations = new ArrayList<IRI>(linkedEntity.getOccurrences().size());
        //first create the TextAnnotations for the Occurrences
        for (Occurrence occurrence : linkedEntity.getOccurrences()) {
            IRI textAnnotation = EnhancementEngineHelper.createTextEnhancement(ci, this);
            textAnnotations.add(textAnnotation);
            metadata.add(new TripleImpl(textAnnotation, Properties.ENHANCER_START, literalFactory.createTypedLiteral(occurrence.getStart())));
            metadata.add(new TripleImpl(textAnnotation, Properties.ENHANCER_END, literalFactory.createTypedLiteral(occurrence.getEnd())));
            metadata.add(new TripleImpl(textAnnotation, Properties.ENHANCER_SELECTION_CONTEXT, new PlainLiteralImpl(occurrence.getContext(), languageObject)));
            metadata.add(new TripleImpl(textAnnotation, Properties.ENHANCER_SELECTED_TEXT, new PlainLiteralImpl(occurrence.getSelectedText(), languageObject)));
            metadata.add(new TripleImpl(textAnnotation, Properties.ENHANCER_CONFIDENCE, literalFactory.createTypedLiteral(linkedEntity.getScore())));
            for (IRI dcType : linkedEntity.getTypes()) {
                metadata.add(new TripleImpl(textAnnotation, Properties.DC_TYPE, dcType));
            }
        }
        //now the EntityAnnotations for the Suggestions
        for (Suggestion suggestion : linkedEntity.getSuggestions()) {
            IRI entityAnnotation = EnhancementEngineHelper.createEntityEnhancement(ci, this);
            //should we use the label used for the match, or search the
            //representation for the best label ... currently its the matched one
            Text label = suggestion.getBestLabel(linkerConfig.getNameField(), language);
            metadata.add(new TripleImpl(entityAnnotation, Properties.ENHANCER_ENTITY_LABEL, label.getLanguage() == null ? new PlainLiteralImpl(label.getText()) : new PlainLiteralImpl(label.getText(), new Language(label.getLanguage()))));
            metadata.add(new TripleImpl(entityAnnotation, Properties.ENHANCER_ENTITY_REFERENCE, new IRI(suggestion.getRepresentation().getId())));
            Iterator<Reference> suggestionTypes = suggestion.getRepresentation().getReferences(linkerConfig.getTypeField());
            while (suggestionTypes.hasNext()) {
                metadata.add(new TripleImpl(entityAnnotation, Properties.ENHANCER_ENTITY_TYPE, new IRI(suggestionTypes.next().getReference())));
            }
            metadata.add(new TripleImpl(entityAnnotation, Properties.ENHANCER_CONFIDENCE, literalFactory.createTypedLiteral(suggestion.getScore())));
            for (IRI textAnnotation : textAnnotations) {
                metadata.add(new TripleImpl(entityAnnotation, Properties.DC_RELATION, textAnnotation));
            }
            //add the name of the ReferencedSite providing this suggestion
            metadata.add(new TripleImpl(entityAnnotation, new IRI(RdfResourceEnum.site.getUri()), new PlainLiteralImpl(referencedSiteName)));
            //add the RDF data for entities
            if (dereferenceEntitiesState) {
                metadata.addAll(RdfValueFactory.getInstance().toRdfRepresentation(suggestion.getRepresentation()).getRdfGraph());
            }
        }
    }
}
Also used : LinkedEntity(org.apache.stanbol.enhancer.engines.keywordextraction.impl.LinkedEntity) IRI(org.apache.clerezza.commons.rdf.IRI) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ArrayList(java.util.ArrayList) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) Suggestion(org.apache.stanbol.enhancer.engines.keywordextraction.impl.Suggestion) Graph(org.apache.clerezza.commons.rdf.Graph) Language(org.apache.clerezza.commons.rdf.Language) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Occurrence(org.apache.stanbol.enhancer.engines.keywordextraction.impl.LinkedEntity.Occurrence)

Example 25 with Reference

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

the class ValueFactoryTest method testRef.

/**
     * Internally used to create and test {@link Reference}s for the different tests
     * 
     * @param refObject
     *            the object representing the reference
     * @return the created {@link Reference} that can be used to perform further tests.
     */
private Reference testRef(Object refObject) {
    ValueFactory vf = getValueFactory();
    Reference ref = vf.createReference(refObject);
    // check not null
    assertNotNull(ref);
    // check reference is not null
    assertNotNull(ref.getReference());
    return ref;
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)

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