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;
}
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);
}
}
}
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));
}
}
}
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());
}
}
}
}
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;
}
Aggregations