Search in sources :

Example 1 with SearchRequestPropertyEnum

use of org.apache.stanbol.enhancer.engines.geonames.impl.GeonamesAPIWrapper.SearchRequestPropertyEnum in project stanbol by apache.

the class LocationEnhancementEngine method computeEnhancements.

@Override
public void computeEnhancements(ContentItem ci) throws EngineException {
    IRI contentItemId = ci.getUri();
    Graph graph = ci.getMetadata();
    LiteralFactory literalFactory = LiteralFactory.getInstance();
    // get all the textAnnotations
    /*
         * this Map holds the name as key and all the text annotations of
         * dc:type dbpedia:Place that select this name as value
         * this map is used to avoid multiple lookups for text annotations
         * selecting the same name.
         */
    Map<String, Collection<BlankNodeOrIRI>> name2placeEnhancementMap = new HashMap<String, Collection<BlankNodeOrIRI>>();
    Iterator<Triple> iterator = graph.filter(null, DC_TYPE, DBPEDIA_PLACE);
    while (iterator.hasNext()) {
        // the enhancement annotating an place
        BlankNodeOrIRI placeEnhancement = iterator.next().getSubject();
        // this can still be an TextAnnotation of an EntityAnnotation
        // so we need to filter TextAnnotation
        Triple isTextAnnotation = new TripleImpl(placeEnhancement, RDF_TYPE, ENHANCER_TEXTANNOTATION);
        if (graph.contains(isTextAnnotation)) {
            // now get the name
            String name = EnhancementEngineHelper.getString(graph, placeEnhancement, ENHANCER_SELECTED_TEXT);
            if (name == null) {
                log.warn("Unable to process TextAnnotation " + placeEnhancement + " because property" + ENHANCER_SELECTED_TEXT + " is not present");
            } else {
                Collection<BlankNodeOrIRI> placeEnhancements = name2placeEnhancementMap.get(name);
                if (placeEnhancements == null) {
                    placeEnhancements = new ArrayList<BlankNodeOrIRI>();
                    name2placeEnhancementMap.put(name, placeEnhancements);
                }
                placeEnhancements.add(placeEnhancement);
            }
        } else {
        // TODO: if we also ant to process EntityAnnotations with the dc:type dbpedia:Place
        // than we need to parse the name based on the enhancer:entity-name property
        }
    }
    // Now we do have all the names we need to lookup
    Map<SearchRequestPropertyEnum, Collection<String>> requestParams = new EnumMap<SearchRequestPropertyEnum, Collection<String>>(SearchRequestPropertyEnum.class);
    if (getMaxLocationEnhancements() != null) {
        requestParams.put(SearchRequestPropertyEnum.maxRows, Collections.singleton(getMaxLocationEnhancements().toString()));
    }
    for (Map.Entry<String, Collection<BlankNodeOrIRI>> entry : name2placeEnhancementMap.entrySet()) {
        List<Toponym> results;
        try {
            requestParams.put(SearchRequestPropertyEnum.name, Collections.singleton(entry.getKey()));
            results = geonamesService.searchToponyms(requestParams);
        } catch (Exception e) {
            /*
                     * TODO: Review if it makes sense to catch here for each name, or
                     * to catch the whole loop.
                     * This depends if single requests can result in Exceptions
                     * (e.g. because of encoding problems) or if usually Exceptions
                     * are thrown because of general things like connection issues
                     * or service unavailability.
                     */
            throw new EngineException(this, ci, e);
        }
        if (results != null) {
            Double maxScore = results.isEmpty() ? null : results.get(0).getScore();
            for (Toponym result : results) {
                log.debug("process result {} {}", result.getGeoNameId(), result.getName());
                Double score = getToponymScore(result, maxScore);
                log.debug("  > score {}", score);
                if (score != null) {
                    if (score < minScore) {
                        // if score is lower than the under bound, than stop
                        break;
                    }
                } else {
                    log.warn("NULL returned as Score for " + result.getGeoNameId() + " " + result.getName());
                /*
                         * NOTE: If score is not present all suggestions are
                         * added as enhancements to the metadata of the content
                         * item.
                         */
                }
                // write the enhancement!
                BlankNodeOrIRI locationEnhancement = writeEntityEnhancement(contentItemId, graph, literalFactory, result, entry.getValue(), null, score);
                log.debug("  > {}  >= {}", score, minHierarchyScore);
                if (score != null && score >= minHierarchyScore) {
                    log.debug("  > getHierarchy for {} {}", result.getGeoNameId(), result.getName());
                    // get the hierarchy
                    try {
                        Iterator<Toponym> hierarchy = getHierarchy(result).iterator();
                        for (int level = 0; hierarchy.hasNext(); level++) {
                            Toponym hierarchyEntry = hierarchy.next();
                            // maybe add an configuration
                            if (level == 0) {
                                // Mother earth -> ignore
                                continue;
                            }
                            // write it as dependent to the locationEnhancement
                            if (result.getGeoNameId() != hierarchyEntry.getGeoNameId()) {
                                // TODO: add additional checks based on possible
                                // configuration here!
                                log.debug("    - write hierarchy {} {}", hierarchyEntry.getGeoNameId(), hierarchyEntry.getName());
                                /*
                                     * The hierarchy service dose not provide a score, because it would be 1.0
                                     * so we need to set the score to this value.
                                     * Currently is is set to the value of the suggested entry
                                     */
                                writeEntityEnhancement(contentItemId, graph, literalFactory, hierarchyEntry, null, Collections.singletonList(locationEnhancement), 1.0);
                            }
                        }
                    } catch (Exception e) {
                        log.warn("Unable to get Hierarchy for " + result.getGeoNameId() + " " + result.getName(), e);
                    }
                }
            }
        }
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HashMap(java.util.HashMap) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) EnumMap(java.util.EnumMap) SearchRequestPropertyEnum(org.apache.stanbol.enhancer.engines.geonames.impl.GeonamesAPIWrapper.SearchRequestPropertyEnum) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) ConfigurationException(org.osgi.service.cm.ConfigurationException) IOException(java.io.IOException) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory) Triple(org.apache.clerezza.commons.rdf.Triple) Graph(org.apache.clerezza.commons.rdf.Graph) Collection(java.util.Collection) Map(java.util.Map) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap)

Aggregations

IOException (java.io.IOException)1 Collection (java.util.Collection)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 Triple (org.apache.clerezza.commons.rdf.Triple)1 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)1 LiteralFactory (org.apache.clerezza.rdf.core.LiteralFactory)1 SearchRequestPropertyEnum (org.apache.stanbol.enhancer.engines.geonames.impl.GeonamesAPIWrapper.SearchRequestPropertyEnum)1 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1