Search in sources :

Example 41 with Graph

use of org.apache.clerezza.commons.rdf.Graph in project stanbol by apache.

the class UIMAToTriples method computeEnhancements.

public void computeEnhancements(ContentItem ci) throws EngineException {
    FeatureStructureListHolder holder;
    LiteralFactory literalFactory = LiteralFactory.getInstance();
    try {
        IRI uimaIRI = new IRI(uimaUri);
        logger.info(new StringBuilder("Trying to load holder for ref:").append(uimaUri).toString());
        holder = ci.getPart(uimaIRI, FeatureStructureListHolder.class);
        for (String source : sourceNames) {
            logger.info(new StringBuilder("Processing UIMA source:").append(source).toString());
            List<FeatureStructure> sourceList = holder.getFeatureStructureList(source);
            if (sourceList != null) {
                logger.info(new StringBuilder("UIMA source:").append(source).append(" contains ").append(sourceList.size()).append(" annotations.").toString());
            } else {
                logger.info(new StringBuilder("Source list is null:").append(source).toString());
                continue;
            }
            for (FeatureStructure fs : sourceList) {
                String typeName = fs.getTypeName();
                logger.debug(new StringBuilder("Checking ").append(typeName).toString());
                if (tnfs.checkFeatureStructureAllowed(typeName, fs.getFeatures())) {
                    logger.debug(new StringBuilder("Adding ").append(typeName).toString());
                    IRI textAnnotation = EnhancementEngineHelper.createTextEnhancement(ci, this);
                    Graph metadata = ci.getMetadata();
                    String uriRefStr = uimaUri + ":" + typeName;
                    if (mappings.containsKey(typeName)) {
                        uriRefStr = mappings.get(typeName);
                    }
                    metadata.add(new TripleImpl(textAnnotation, DC_TYPE, new IRI(uriRefStr)));
                    if (fs.getFeature("begin") != null) {
                        metadata.add(new TripleImpl(textAnnotation, ENHANCER_START, literalFactory.createTypedLiteral(fs.getFeature("begin").getValueAsInteger())));
                    }
                    if (fs.getFeature("end") != null) {
                        metadata.add(new TripleImpl(textAnnotation, ENHANCER_END, literalFactory.createTypedLiteral(fs.getFeature("end").getValueAsInteger())));
                    }
                    if (fs.getCoveredText() != null && !fs.getCoveredText().isEmpty()) {
                        metadata.add(new TripleImpl(textAnnotation, ENHANCER_SELECTED_TEXT, new PlainLiteralImpl(fs.getCoveredText())));
                    }
                    for (Feature f : fs.getFeatures()) {
                        if (!f.getName().equals("begin") && !f.getName().equals("end") && tnfs.checkFeatureToConvert(typeName, f)) {
                            String predRefStr = uimaUri + ":" + f.getName();
                            if (mappings.containsKey(f.getName())) {
                                predRefStr = mappings.get(f.getName());
                            }
                            IRI predicate = new IRI(predRefStr);
                            metadata.add(new TripleImpl(textAnnotation, predicate, new PlainLiteralImpl(f.getValueAsString())));
                        }
                    }
                }
            }
        }
    } catch (NoSuchPartException e) {
        logger.error(new StringBuilder("No UIMA results found with ref:").append(uimaUri).toString(), e);
    }
}
Also used : FeatureStructure(org.apache.stanbol.commons.caslight.FeatureStructure) IRI(org.apache.clerezza.commons.rdf.IRI) Graph(org.apache.clerezza.commons.rdf.Graph) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) FeatureStructureListHolder(org.apache.stanbol.commons.caslight.FeatureStructureListHolder) NoSuchPartException(org.apache.stanbol.enhancer.servicesapi.NoSuchPartException) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) Feature(org.apache.stanbol.commons.caslight.Feature) LiteralFactory(org.apache.clerezza.rdf.core.LiteralFactory)

Example 42 with Graph

use of org.apache.clerezza.commons.rdf.Graph in project stanbol by apache.

the class XmpExtractorEngine method computeEnhancements.

@Override
public void computeEnhancements(ContentItem ci) throws EngineException {
    InputStream in = ci.getBlob().getStream();
    XMPPacketScanner scanner = new XMPPacketScanner();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        scanner.parse(in, baos);
    } catch (IOException e) {
        throw new EngineException(e);
    }
    byte[] bytes = baos.toByteArray();
    if (bytes.length > 0) {
        Graph model = new IndexedGraph();
        parser.parse(model, new ByteArrayInputStream(bytes), "application/rdf+xml");
        GraphNode gn = new GraphNode(new IRI("http://relative-uri.fake/"), model);
        gn.replaceWith(ci.getUri());
        ci.getLock().writeLock().lock();
        try {
            LOG.info("Model: {}", model);
            ci.getMetadata().addAll(model);
        } finally {
            ci.getLock().writeLock().unlock();
        }
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) XMPPacketScanner(org.apache.tika.parser.image.xmp.XMPPacketScanner) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) GraphNode(org.apache.clerezza.rdf.utils.GraphNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph)

Example 43 with Graph

use of org.apache.clerezza.commons.rdf.Graph in project stanbol by apache.

the class ZemantaAPIWrapper method parseResponse.

private ImmutableGraph parseResponse(InputStream is) {
    JenaParserProvider jenaParserProvider = new JenaParserProvider();
    //NOTE(rw): the new third parameter is the base URI used to resolve relative paths
    Graph g = new SimpleGraph();
    jenaParserProvider.parse(g, is, SupportedFormat.RDF_XML, null);
    log.debug("graph: " + g.toString());
    return g.getImmutableGraph();
}
Also used : JenaParserProvider(org.apache.clerezza.rdf.jena.parser.JenaParserProvider) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Graph(org.apache.clerezza.commons.rdf.Graph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)

Example 44 with Graph

use of org.apache.clerezza.commons.rdf.Graph in project stanbol by apache.

the class ZemantaEnhancementEngine method computeEnhancements.

public void computeEnhancements(ContentItem ci) throws EngineException {
    Entry<IRI, Blob> contentPart = ContentItemHelper.getBlob(ci, SUPPORTED_MIMETYPES);
    if (contentPart == null) {
        throw new IllegalStateException("No ContentPart with a supported Mime Type" + "found for ContentItem " + ci.getUri() + "(supported: '" + SUPPORTED_MIMETYPES + "') -> this indicates that canEnhance was" + "NOT called and indicates a bug in the used EnhancementJobManager!");
    }
    String text;
    try {
        text = ContentItemHelper.getText(contentPart.getValue());
    } catch (IOException e) {
        throw new InvalidContentException(this, ci, e);
    }
    if (text.trim().length() == 0) {
        log.warn("ContentPart {} of ContentItem {} does not contain any text to enhance", contentPart.getKey(), ci.getUri());
        return;
    }
    Graph graph = ci.getMetadata();
    IRI ciId = ci.getUri();
    //we need to store the results of Zemanta in an temp graph
    Graph results = new SimpleGraph();
    ZemantaAPIWrapper zemanta = new ZemantaAPIWrapper(key);
    try {
        results.addAll(zemanta.enhance(text));
    } catch (IOException e) {
        throw new EngineException("Unable to get Enhancement from remote Zemanta Service", e);
    }
    //now we need to process the results and convert them into the Enhancer
    //annotation structure
    ci.getLock().writeLock().lock();
    try {
        processRecognition(results, graph, text, ciId);
        processCategories(results, graph, ciId);
    } finally {
        ci.getLock().writeLock().unlock();
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) InvalidContentException(org.apache.stanbol.enhancer.servicesapi.InvalidContentException) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) IOException(java.io.IOException)

Example 45 with Graph

use of org.apache.clerezza.commons.rdf.Graph in project stanbol by apache.

the class UserResource method rolesCheckboxes.

/**
     * Provides HTML corresponding to a user's roles
     *
     * all roles are listed with checkboxes, the roles this user has are checked
     *
     * (isn't very pretty but is just a one-off)
     *
     * @param userName the user in question
     * @return HTML checkboxes as HTTP response
     */
@GET
@Path("users/{username}/rolesCheckboxes")
@Produces(MediaType.TEXT_HTML)
public Response rolesCheckboxes(@PathParam("username") String userName) {
    // return new RdfViewable("rolesCheckboxes", getRoleType(), this.getClass());
    StringBuffer html = new StringBuffer();
    Iterator<Triple> allRoleTriples = systemGraph.filter(null, RDF.type, PERMISSION.Role);
    ArrayList<String> allRoleNames = new ArrayList<String>();
    Lock readLock = systemGraph.getLock().readLock();
    readLock.lock();
    try {
        // pulls out all role names
        while (allRoleTriples.hasNext()) {
            Triple triple = allRoleTriples.next();
            GraphNode roleNode = new GraphNode(triple.getSubject(), systemGraph);
            Iterator<Literal> titlesIterator = roleNode.getLiterals(DC.title);
            while (titlesIterator.hasNext()) {
                allRoleNames.add(titlesIterator.next().getLexicalForm());
            }
        }
    } finally {
        readLock.unlock();
    }
    Graph rolesGraph = getUserRolesGraph(userName);
    ArrayList<String> userRoleNames = new ArrayList<String>();
    Iterator<Triple> userRoleTriples = rolesGraph.filter(null, RDF.type, PERMISSION.Role);
    while (userRoleTriples.hasNext()) {
        Triple triple = userRoleTriples.next();
        GraphNode roleNode = new GraphNode(triple.getSubject(), rolesGraph);
        Iterator<Literal> titlesIterator = roleNode.getLiterals(DC.title);
        while (titlesIterator.hasNext()) {
            userRoleNames.add(titlesIterator.next().getLexicalForm());
        }
    }
    for (int i = 0; i < allRoleNames.size(); i++) {
        String role = allRoleNames.get(i);
        if (role.equals("BasePermissionsRole")) {
            // filter out
            continue;
        }
        if (userRoleNames.contains(role)) {
            html.append("<input class=\"role\" type=\"checkbox\" id=\"").append(role).append("\" name=\"").append(role).append("\" value=\"").append(role).append("\" checked=\"checked\" />");
        } else {
            html.append("<input class=\"role\" type=\"checkbox\" id=\"").append(role).append("\" name=\"").append(role).append("\" value=\"").append(role).append("\" />");
        }
        html.append("<label for=\"").append(role).append("\">").append(role).append("</label>");
        html.append("<br />");
    }
    return Response.ok(html.toString()).build();
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Literal(org.apache.clerezza.commons.rdf.Literal) ArrayList(java.util.ArrayList) GraphNode(org.apache.clerezza.rdf.utils.GraphNode) Lock(java.util.concurrent.locks.Lock) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Graph (org.apache.clerezza.commons.rdf.Graph)172 IRI (org.apache.clerezza.commons.rdf.IRI)110 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)66 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)57 Triple (org.apache.clerezza.commons.rdf.Triple)45 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)43 Test (org.junit.Test)38 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)36 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)34 IOException (java.io.IOException)27 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)26 HashSet (java.util.HashSet)24 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)24 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)24 InputStream (java.io.InputStream)21 HashMap (java.util.HashMap)20 Language (org.apache.clerezza.commons.rdf.Language)17 Blob (org.apache.stanbol.enhancer.servicesapi.Blob)17 ArrayList (java.util.ArrayList)16 LiteralFactory (org.apache.clerezza.rdf.core.LiteralFactory)15