Search in sources :

Example 11 with Site

use of org.apache.stanbol.entityhub.servicesapi.site.Site in project stanbol by apache.

the class CoreferenceFinder method lookupEntity.

/**
 * Gets an Entity from the configured {@link Site} based on the NER text and type.
 *
 * @param ner
 * @param language
 * @return
 * @throws EngineException
 */
private Entity lookupEntity(Span ner, String language) throws EngineException {
    Site site = getReferencedSite();
    FieldQueryFactory queryFactory = site == null ? entityHub.getQueryFactory() : site.getQueryFactory();
    FieldQuery query = queryFactory.createFieldQuery();
    Constraint labelConstraint;
    String namedEntityLabel = ner.getSpan();
    labelConstraint = new TextConstraint(namedEntityLabel, false, language, null);
    query.setConstraint(RDFS_LABEL.getUnicodeString(), labelConstraint);
    query.setConstraint(RDF_TYPE.getUnicodeString(), new ReferenceConstraint(ner.getAnnotation(NlpAnnotations.NER_ANNOTATION).value().getType().getUnicodeString()));
    query.setLimit(1);
    QueryResultList<Entity> results = // if site is NULL
    site == null ? entityHub.findEntities(query) : // use the Entityhub
    site.findEntities(// else the referenced site
    query);
    if (results.isEmpty())
        return null;
    // We set the limit to 1 so if it found anything it should contain just 1 entry
    return results.iterator().next();
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint) FieldQueryFactory(org.apache.stanbol.entityhub.servicesapi.query.FieldQueryFactory) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) ReferenceConstraint(org.apache.stanbol.entityhub.servicesapi.query.ReferenceConstraint)

Example 12 with Site

use of org.apache.stanbol.entityhub.servicesapi.site.Site in project stanbol by apache.

the class ReferencedSiteSearcher method get.

@Override
public Entity get(IRI id, Set<IRI> fields, String... languages) {
    if (id == null || id.getUnicodeString().isEmpty()) {
        return null;
    }
    org.apache.stanbol.entityhub.servicesapi.model.Entity entity;
    Site site = getSearchService();
    if (site == null) {
        throw new IllegalStateException("ReferencedSite " + siteId + " is currently not available");
    }
    try {
        entity = site.getEntity(id.getUnicodeString());
    } catch (SiteException e) {
        throw new IllegalStateException("Exception while getting " + id + " from the ReferencedSite " + site.getId(), e);
    }
    if (entity != null) {
        Set<String> languageSet;
        if (languages == null || languages.length < 1) {
            languageSet = null;
        } else if (languages.length == 1) {
            languageSet = Collections.singleton(languages[0]);
        } else {
            languageSet = new HashSet<String>(Arrays.asList(languages));
        }
        return new EntityhubEntity(entity.getRepresentation(), fields, languageSet);
    } else {
        return null;
    }
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) HashSet(java.util.HashSet)

Example 13 with Site

use of org.apache.stanbol.entityhub.servicesapi.site.Site in project stanbol by apache.

the class ReferencedSiteSearcher method get.

@Override
public Representation get(String id, Set<String> includeFields) {
    if (id == null || id.isEmpty()) {
        return null;
    }
    Entity entity;
    Site site = getSearchService();
    if (site == null) {
        throw new IllegalStateException("ReferencedSite " + siteId + " is currently not available");
    }
    try {
        entity = site.getEntity(id);
    } catch (SiteException e) {
        throw new IllegalStateException("Exception while getting " + id + " from the ReferencedSite " + site.getId(), e);
    }
    return entity == null ? null : entity.getRepresentation();
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException)

Example 14 with Site

use of org.apache.stanbol.entityhub.servicesapi.site.Site in project stanbol by apache.

the class NamedEntityTaggingEngine method computeEnhancements.

public void computeEnhancements(ContentItem ci) throws EngineException {
    final Site site;
    if (referencedSiteID != null) {
        // lookup the referenced site
        site = siteManager.getSite(referencedSiteID);
        // ensure that it is present
        if (site == null) {
            String msg = String.format("Unable to enhance %s because Referenced Site %s is currently not active!", ci.getUri().getUnicodeString(), referencedSiteID);
            log.warn(msg);
            // throw new EngineException(msg);
            return;
        }
        // and that it supports offline mode if required
        if (isOfflineMode() && !site.supportsLocalMode()) {
            log.warn("Unable to enhance ci {} because OfflineMode is not supported by ReferencedSite {}.", ci.getUri().getUnicodeString(), site.getId());
            return;
        }
    } else {
        // null indicates to use the Entityhub to lookup Entities
        site = null;
    }
    Graph graph = ci.getMetadata();
    LiteralFactory literalFactory = LiteralFactory.getInstance();
    // Retrieve the existing text annotations (requires read lock)
    Map<NamedEntity, List<IRI>> textAnnotations = new HashMap<NamedEntity, List<IRI>>();
    // the language extracted for the parsed content or NULL if not
    // available
    String contentLangauge;
    ci.getLock().readLock().lock();
    try {
        contentLangauge = EnhancementEngineHelper.getLanguage(ci);
        for (Iterator<Triple> it = graph.filter(null, RDF_TYPE, TechnicalClasses.ENHANCER_TEXTANNOTATION); it.hasNext(); ) {
            IRI uri = (IRI) it.next().getSubject();
            if (graph.filter(uri, Properties.DC_RELATION, null).hasNext()) {
                // skip
                continue;
            }
            NamedEntity namedEntity = NamedEntity.createFromTextAnnotation(graph, uri);
            if (namedEntity != null) {
                // This is a first occurrence, collect any subsumed
                // annotations
                List<IRI> subsumed = new ArrayList<IRI>();
                for (Iterator<Triple> it2 = graph.filter(null, Properties.DC_RELATION, uri); it2.hasNext(); ) {
                    subsumed.add((IRI) it2.next().getSubject());
                }
                textAnnotations.put(namedEntity, subsumed);
            }
        }
    } finally {
        ci.getLock().readLock().unlock();
    }
    // search the suggestions
    Map<NamedEntity, List<Suggestion>> suggestions = new HashMap<NamedEntity, List<Suggestion>>(textAnnotations.size());
    for (Entry<NamedEntity, List<IRI>> entry : textAnnotations.entrySet()) {
        try {
            List<Suggestion> entitySuggestions = computeEntityRecommentations(site, entry.getKey(), entry.getValue(), contentLangauge);
            if (entitySuggestions != null && !entitySuggestions.isEmpty()) {
                suggestions.put(entry.getKey(), entitySuggestions);
            }
        } catch (EntityhubException e) {
            throw new EngineException(this, ci, e);
        }
    }
    // now write the results (requires write lock)
    ci.getLock().writeLock().lock();
    try {
        RdfValueFactory factory = RdfValueFactory.getInstance();
        Map<String, Representation> entityData = new HashMap<String, Representation>();
        for (Entry<NamedEntity, List<Suggestion>> entitySuggestions : suggestions.entrySet()) {
            List<IRI> subsumed = textAnnotations.get(entitySuggestions.getKey());
            List<BlankNodeOrIRI> annotationsToRelate = new ArrayList<BlankNodeOrIRI>(subsumed);
            annotationsToRelate.add(entitySuggestions.getKey().getEntity());
            for (Suggestion suggestion : entitySuggestions.getValue()) {
                log.debug("Add Suggestion {} for {}", suggestion.getEntity().getId(), entitySuggestions.getKey());
                EnhancementRDFUtils.writeEntityAnnotation(this, literalFactory, graph, ci.getUri(), annotationsToRelate, suggestion, nameField, // header)?!
                contentLangauge == null ? DEFAULT_LANGUAGE : contentLangauge);
                if (dereferenceEntities) {
                    entityData.put(suggestion.getEntity().getId(), suggestion.getEntity().getRepresentation());
                }
            }
        }
        // Representations to add! If false entityData will be empty
        for (Representation rep : entityData.values()) {
            graph.addAll(factory.toRdfRepresentation(rep).getRdfGraph());
        }
    } finally {
        ci.getLock().writeLock().unlock();
    }
}
Also used : Site(org.apache.stanbol.entityhub.servicesapi.site.Site) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) List(java.util.List) ArrayList(java.util.ArrayList) QueryResultList(org.apache.stanbol.entityhub.servicesapi.query.QueryResultList) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory) Triple(org.apache.clerezza.commons.rdf.Triple) Graph(org.apache.clerezza.commons.rdf.Graph) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)

Example 15 with Site

use of org.apache.stanbol.entityhub.servicesapi.site.Site in project stanbol by apache.

the class ReferencedSiteRootResource method getEntityById.

/**
 * Cool URI handler for Signs.
 *
 * @param id
 *            The id of the entity (required)
 * @param headers
 *            the request headers used to get the requested {@link MediaType}
 * @return a redirection to either a browser view, the RDF meta data or the raw binary content
 */
@GET
@Path("/entity")
public Response getEntityById(@PathParam(value = "site") String siteId, @QueryParam(value = "id") String id, @Context HttpHeaders headers) {
    Site site = getSite(siteId);
    log.debug("site/{}/entity Request", site.getId());
    log.debug("  > id       : " + id);
    log.debug("  > accept   : " + headers.getAcceptableMediaTypes());
    log.debug("  > mediaType: " + headers.getMediaType());
    Collection<String> supported = new HashSet<String>(JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES);
    supported.add(TEXT_HTML);
    final MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, MediaType.APPLICATION_JSON_TYPE);
    if (id == null || id.isEmpty()) {
        if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            ResponseBuilder rb = Response.ok(new Viewable("entity", new SiteResultData(site)));
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
            // addCORSOrigin(servletContext, rb, headers);
            return rb.build();
        } else {
            return Response.status(Status.BAD_REQUEST).entity("No or empty ID was parsed. Missing parameter id.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    log.debug("handle Request for Entity {} of Site {}", id, site.getId());
    Entity entity;
    try {
        entity = site.getEntity(id);
    } catch (SiteException e) {
        log.error("ReferencedSiteException while accessing Site " + site.getConfiguration().getName() + " (id=" + site.getId() + ")", e);
        throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
    }
    if (entity != null) {
        ResponseBuilder rb = Response.ok(entity);
        rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
        // addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    } else {
        // TODO: How to parse an ErrorMessage?
        // create an Response with the the Error?
        log.debug(" ... Entity {} not found on referenced site {}", id, site.getId());
        return Response.status(Status.NOT_FOUND).entity("Entity '" + id + "' not found on referenced site '" + site.getId() + "'\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
    }
}
Also used : ManagedSite(org.apache.stanbol.entityhub.servicesapi.site.ManagedSite) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) WebApplicationException(javax.ws.rs.WebApplicationException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) GET(javax.ws.rs.GET)

Aggregations

Site (org.apache.stanbol.entityhub.servicesapi.site.Site)20 SiteException (org.apache.stanbol.entityhub.servicesapi.site.SiteException)12 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)9 HashSet (java.util.HashSet)8 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)6 Path (javax.ws.rs.Path)5 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)5 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)5 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)5 ManagedSite (org.apache.stanbol.entityhub.servicesapi.site.ManagedSite)5 MediaType (javax.ws.rs.core.MediaType)4 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)4 IRI (org.apache.clerezza.commons.rdf.IRI)3 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)3 ArrayList (java.util.ArrayList)2 GET (javax.ws.rs.GET)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Graph (org.apache.clerezza.commons.rdf.Graph)2 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)2 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)2