
io.redlink.sdk.impl.analysis.model.Enhancements Maven / Gradle / Ivy
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.redlink.sdk.impl.analysis.model;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.*;
import com.google.common.primitives.Doubles;
import java.util.*;
/**
* Analysis Result API. This class eases the management of the RedLink analysis service results, providing
* an API that hides the complexity of the Enhancement Structure returned by the service
*
* @author [email protected]
* @author [email protected]
*/
public class Enhancements implements Iterable {
/**
* Map
*/
private Multimap, Enhancement> enhancements;
/**
* Map
*/
private Map entities;
/**
* Annotations' languages (LanguageAnnotation)
*/
private Collection languages;
Enhancements() {
this.enhancements = ArrayListMultimap.create();
this.entities = Maps.newLinkedHashMap();
languages = Sets.newHashSet();
}
@Override
public Iterator iterator() {
return getEnhancements().iterator();
}
/**
* Returns the {@link Collection} of all {@link Enhancement}s, including Text, Entity and Topic annotations
*
* @return
*/
public Collection getEnhancements() {
return Collections.unmodifiableCollection(enhancements.values());
}
void setEnhancements(Collection enhancements) {
for (Enhancement e : enhancements) {
addEnhancement(e);
}
}
void addEnhancement(Enhancement enhancement) {
enhancements.put(enhancement.getClass(), enhancement);
if (enhancement instanceof EntityAnnotation) {
EntityAnnotation ea = (EntityAnnotation) enhancement;
entities.put(ea.getEntityReference().getUri(),
ea.getEntityReference());
}
}
/**
* Returns the {@link Collection} of extracted {@link TextAnnotation}s
*
* @return
*/
@SuppressWarnings("unchecked")
public Collection getTextAnnotations() {
Collection extends Enhancement> result = enhancements
.get(TextAnnotation.class);
return (Collection) result; // Should be safe. Need to
// be tested
}
/**
* Returns the {@link Collection} of extracted {@link EntityAnnotation}s
*
* @return
*/
@SuppressWarnings("unchecked")
public Collection getEntityAnnotations() {
Collection extends Enhancement> result = enhancements.get(EntityAnnotation.class);
return (Collection) result; // Should be safe. Needs to be tested
}
/**
* Returns the {@link Collection} of dereferenced {@link Entity}s
*
* @return
*/
public Collection getEntities() {
return Collections.unmodifiableCollection(entities.values());
}
/**
* Returns a dereferenced entity by its URI
*
* @param URI
* @return
*/
public Entity getEntity(String URI) {
return entities.get(URI);
}
/**
* Returns a {@link Collection} of {@link EntityAnnotation}s associated to the {@link TextAnnotation} passed by parameter
*
* @param ta {@link TextAnnotation}
* @return
*/
public Collection getEntityAnnotations(TextAnnotation ta) {
Collection eas = getEntityAnnotations();
Collection result = Sets.newHashSet();
for (EntityAnnotation ea : eas) {
if (ea.getRelations() != null && ea.getRelations().contains(ta)) {
result.add(ea);
}
}
return result;
}
/**
* Returns a {@link Collection} of {@link Entity}s for which associated {@link EntityAnnotation}s has a confidence value
* greater than or equal to the value passed by parameter
*
* @param confidenceValue Threshold confidence value
* @return
*/
public Collection getEntitiesByConfidenceValue(final Double confidenceValue) {
Collection sortedEas = getEntityAnnotationsByConfidenceValue(confidenceValue);
return Collections2.transform(sortedEas,
new Function() {
@Override
public Entity apply(final EntityAnnotation ea) {
return ea.getEntityReference();
}
}
);
}
/**
* Returns a {@link Collection} of {@link TextAnnotation}s which confidences values are greater than or equal
* to the value passed by parameter
*
* @param confidenceValue Threshold confidence value
* @return
*/
public Collection getTextAnnotationsByConfidenceValue(final Double confidenceValue) {
return FluentIterable.from(getTextAnnotations())
.filter(new Predicate() {
@Override
public boolean apply(TextAnnotation e) {
return e.confidence.doubleValue() >= confidenceValue
.doubleValue();
}
}).toImmutableList();
}
/**
* Returns a {@link Collection} of {@link EntityAnnotation}s which confidences values are greater than or equal
* to the value passed by parameter
*
* @param confidenceValue Threshold confidence value
* @return
*/
public Collection getEntityAnnotationsByConfidenceValue(
final Double confidenceValue) {
return FluentIterable.from(getEntityAnnotations())
.filter(new Predicate() {
@Override
public boolean apply(EntityAnnotation e) {
return e.confidence.doubleValue() >= confidenceValue
.doubleValue();
}
}).toImmutableList();
}
public Multimap getEntityAnnotationsByTextAnnotation() {
Multimap map = ArrayListMultimap.create();
Collection eas = getEntityAnnotations();
for (EntityAnnotation ea : eas) {
if (ea.relations != null) {
for (Enhancement e : ea.relations) {
if (e instanceof TextAnnotation) {
map.put((TextAnnotation) e, ea);
}
}
}
}
return map;
}
/**
* Returns the best {@link EntityAnnotation}s (those with the highest confidence value) for each extracted {@link TextAnnotation}
*
* @return best annotations
*/
public Multimap getBestAnnotations() {
Ordering o = new Ordering() {
@Override
public int compare(EntityAnnotation left, EntityAnnotation right) {
return Doubles.compare(left.confidence, right.confidence);
}
}.reverse();
Multimap result = ArrayListMultimap.create();
for (TextAnnotation ta : getTextAnnotations()) {
List eas = o.sortedCopy(getEntityAnnotations(ta));
if (!eas.isEmpty()) {
Collection highest = new HashSet<>();
Double confidence = eas.get(0).getConfidence();
for (EntityAnnotation ea : eas) {
if (ea.confidence < confidence) {
break;
} else {
highest.add(ea);
}
}
result.putAll(ta, highest);
}
}
return result;
}
/**
* Returns an {@link EntityAnnotation} by its associated dereferenced {@link Entity} URI
*
* @param entityUri
* @return
*/
public EntityAnnotation getEntityAnnotation(final String entityUri) {
return Iterables.find(getEntityAnnotations(),
new Predicate() {
@Override
public boolean apply(EntityAnnotation ea) {
return ea.getEntityReference().getUri()
.equals(entityUri);
}
}
);
}
/**
* Returns a {@link Collection} of identified languages in the analyzed content
*
* @return
*/
public Collection getLanguages() {
return languages;
}
/**
* @param languages
*/
void setLanguages(Collection languages) {
this.languages = languages;
}
/**
* @param language
*/
void addLanguage(String language) {
this.languages.add(language);
}
/**
* Returns the {@link Collection} of extracted categories (topics) for the analyzed content
*
* @return
*/
@SuppressWarnings("unchecked")
public Collection getCategories() {
Collection extends Enhancement> result = enhancements.get(TopicAnnotation.class);
return (Collection) result; // Should be safe. Needs to be tested
}
/**
* Returns true if the contents has been categorized with a category identified by the URI passed by parameter
*
* @param conceptURI URI of the category concept
* @return
*/
public boolean hasCategory(final String conceptURI) {
return Iterables.any(getCategories(),
new Predicate() {
@Override
public boolean apply(TopicAnnotation ta) {
return ta.getTopicReference().
equals(conceptURI);
}
}
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy