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