use of org.apache.stanbol.enhancer.engines.dbpspotlight.model.SurfaceForm in project stanbol by apache.
the class DBPSpotlightSpotEnhancementEngine method createEnhancements.
/**
* The method adds the returned DBpedia Spotlight surface forms to the
* content item's metadata. For each one an TextAnnotation is created.
*
* @param occs
* a Collection of entity information
* @param ci
* the content item
*/
protected void createEnhancements(Collection<SurfaceForm> occs, ContentItem ci, String content, Language lang) {
HashMap<String, IRI> entityAnnotationMap = new HashMap<String, IRI>();
Graph model = ci.getMetadata();
for (SurfaceForm occ : occs) {
IRI textAnnotation = SpotlightEngineUtils.createTextEnhancement(occ, this, ci, content, lang);
if (entityAnnotationMap.containsKey(occ.name)) {
model.add(new TripleImpl(entityAnnotationMap.get(occ.name), DC_RELATION, textAnnotation));
} else {
entityAnnotationMap.put(occ.name, textAnnotation);
}
}
}
use of org.apache.stanbol.enhancer.engines.dbpspotlight.model.SurfaceForm in project stanbol by apache.
the class DBPSpotlightSpotEnhancementEngine method computeEnhancements.
/**
* Calculate the enhancements by doing a POST request to the DBpedia
* Spotlight endpoint and processing the results
*
* @param ci
* the {@link ContentItem}
*/
public void computeEnhancements(ContentItem ci) throws EngineException {
Language language = SpotlightEngineUtils.getContentLanguage(ci);
String text = SpotlightEngineUtils.getPlainContent(ci);
Collection<SurfaceForm> dbpslGraph = doPostRequest(text, ci.getUri());
if (dbpslGraph != null) {
// Acquire a write lock on the ContentItem when adding the
// enhancements
ci.getLock().writeLock().lock();
try {
createEnhancements(dbpslGraph, ci, text, language);
if (log.isDebugEnabled()) {
Serializer serializer = Serializer.getInstance();
ByteArrayOutputStream debugStream = new ByteArrayOutputStream();
serializer.serialize(debugStream, ci.getMetadata(), "application/rdf+xml");
try {
log.debug("DBpedia Spotlight Spot Enhancements:\n{}", debugStream.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
} finally {
ci.getLock().writeLock().unlock();
}
}
}
use of org.apache.stanbol.enhancer.engines.dbpspotlight.model.SurfaceForm in project stanbol by apache.
the class DBPSpotlightAnnotateEnhancementEngine method computeEnhancements.
/**
* Calculate the enhancements by doing a POST request to the DBpedia
* Spotlight endpoint and processing the results
*
* @param ci
* the {@link ContentItem}
*/
public void computeEnhancements(ContentItem ci) throws EngineException {
Language language = SpotlightEngineUtils.getContentLanguage(ci);
String text = SpotlightEngineUtils.getPlainContent(ci);
Collection<Annotation> dbpslGraph = doPostRequest(text, ci.getUri());
Map<SurfaceForm, IRI> surfaceForm2TextAnnotation = new HashMap<SurfaceForm, IRI>();
if (dbpslGraph != null) {
// Acquire a write lock on the ContentItem when adding the
// enhancements
ci.getLock().writeLock().lock();
try {
createEnhancements(dbpslGraph, ci, text, language, surfaceForm2TextAnnotation);
if (log.isDebugEnabled()) {
Serializer serializer = Serializer.getInstance();
ByteArrayOutputStream debugStream = new ByteArrayOutputStream();
serializer.serialize(debugStream, ci.getMetadata(), "application/rdf+xml");
try {
log.debug("DBPedia Spotlight Enhancements:\n{}", debugStream.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
} finally {
ci.getLock().writeLock().unlock();
}
}
}
use of org.apache.stanbol.enhancer.engines.dbpspotlight.model.SurfaceForm in project stanbol by apache.
the class DBPSpotlightCandidatesEnhancementEngine method computeEnhancements.
/**
* Calculate the enhancements by doing a POST request to the DBpedia
* Spotlight endpoint and processing the results
*
* @param ci
* the {@link ContentItem}
*/
public void computeEnhancements(ContentItem ci) throws EngineException {
Language language = SpotlightEngineUtils.getContentLanguage(ci);
String text = SpotlightEngineUtils.getPlainContent(ci);
Collection<SurfaceForm> dbpslGraph = doPostRequest(text, ci.getUri());
if (dbpslGraph != null) {
// Acquire a write lock on the ContentItem when adding the
// enhancements
ci.getLock().writeLock().lock();
try {
createEnhancements(dbpslGraph, ci, text, language);
if (log.isDebugEnabled()) {
Serializer serializer = Serializer.getInstance();
ByteArrayOutputStream debugStream = new ByteArrayOutputStream();
serializer.serialize(debugStream, ci.getMetadata(), "application/rdf+xml");
try {
log.debug("DBpedia Spotlight Spot Enhancements:\n{}", debugStream.toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
} finally {
ci.getLock().writeLock().unlock();
}
}
}
use of org.apache.stanbol.enhancer.engines.dbpspotlight.model.SurfaceForm in project stanbol by apache.
the class DBPSpotlightCandidatesEnhancementEngine method createEnhancements.
/**
* This generates enhancement structures for the entities from DBPedia
* Spotlight and adds them to the content item's metadata. For each surface
* form a TextAnnotation and the according EntityAnnotations are created.
*
* @param occs
* a Collection of entity information
* @param ci
* the content item
*/
protected void createEnhancements(Collection<SurfaceForm> occs, ContentItem ci, String text, Language language) {
// TODO create TextEnhancement (form, start, end, type?)
HashMap<String, IRI> entityAnnotationMap = new HashMap<String, IRI>();
Graph model = ci.getMetadata();
for (SurfaceForm occ : occs) {
IRI textAnnotation = SpotlightEngineUtils.createTextEnhancement(occ, this, ci, text, language);
Iterator<CandidateResource> resources = occ.resources.iterator();
while (resources.hasNext()) {
CandidateResource resource = resources.next();
IRI entityAnnotation = SpotlightEngineUtils.createEntityAnnotation(resource, this, ci, textAnnotation);
entityAnnotationMap.put(resource.localName, entityAnnotation);
}
if (entityAnnotationMap.containsKey(occ.name)) {
model.add(new TripleImpl(entityAnnotationMap.get(occ.name), DC_RELATION, textAnnotation));
} else {
entityAnnotationMap.put(occ.name, textAnnotation);
}
}
}
Aggregations