Package org.apache.clerezza.rdf.core

Examples of org.apache.clerezza.rdf.core.LiteralFactory


      throw new EngineException("Error wile encoding/decoding the request/" + "response to the CELI lemmatizer service!", e);
    }
    // get a write lock before writing the enhancements
    ci.getLock().writeLock().lock();
    try {
      LiteralFactory literalFactory = LiteralFactory.getInstance();
      for (LexicalEntry le : terms) {

        List<CeliMorphoFeatures> mFeatures = this.convertLexicalEntryToMorphFeatures(le, language);
        for (CeliMorphoFeatures feat : mFeatures) {
          // Create a text annotation for each interpretation produced by the morphological analyzer
          UriRef textAnnotation = EnhancementEngineHelper.createTextEnhancement(ci, this);
          g.add(new TripleImpl(textAnnotation, ENHANCER_SELECTED_TEXT, new PlainLiteralImpl(le.getWordForm(), lang)));
          if (le.from >= 0 && le.to > 0) {
            g.add(new TripleImpl(textAnnotation, ENHANCER_START, literalFactory.createTypedLiteral(le.from)));
            g.add(new TripleImpl(textAnnotation, ENHANCER_END, literalFactory.createTypedLiteral(le.to)));
            g.add(new TripleImpl(textAnnotation, ENHANCER_SELECTION_CONTEXT, new PlainLiteralImpl(getSelectionContext(text, le.getWordForm(), le.from), lang)));
          }
          g.addAll(feat.featuresAsTriples(textAnnotation, lang));
        }
      }
View Full Code Here


            throw new IllegalStateException("Unable to extract Language for " + "ContentItem " + ci.getUri() + ": This is also checked in the canEnhance " + "method! -> This indicated an Bug in the implementation of the " + "EnhancementJobManager!");
        }
        Language lang = new Language(language); //used for the palin literals in TextAnnotations
    try {
      List<SentimentExpression> lista = this.client.extractSentimentExpressions(text, language);
      LiteralFactory literalFactory = LiteralFactory.getInstance();

      MGraph g = ci.getMetadata();

      for (SentimentExpression se : lista) {
        try {
          UriRef textAnnotation = EnhancementEngineHelper.createTextEnhancement(ci, this);
          //add selected text as PlainLiteral in the language extracted from the text
          g.add(new TripleImpl(textAnnotation, ENHANCER_SELECTED_TEXT,  new PlainLiteralImpl(se.getSnippetStr(),lang)));
          g.add(new TripleImpl(textAnnotation, DC_TYPE, CeliConstants.SENTIMENT_EXPRESSION));
          if (se.getStartSnippet() != null && se.getEndSnippet() != null) {
            g.add(new TripleImpl(textAnnotation, ENHANCER_START, literalFactory.createTypedLiteral(se.getStartSnippet().intValue())));
            g.add(new TripleImpl(textAnnotation, ENHANCER_END, literalFactory.createTypedLiteral(se.getEndSnippet().intValue())));
            g.add(new TripleImpl(textAnnotation, ENHANCER_SELECTION_CONTEXT,
                  new PlainLiteralImpl(getSelectionContext(text, se.getSnippetStr(), se.getStartSnippet()), lang)));
            g.add(new TripleImpl(textAnnotation, CeliConstants.HAS_SENTIMENT_EXPRESSION_POLARITY, literalFactory.createTypedLiteral(se.getSentimentPolarityAsDoubleValue())));
          }
        } catch (NoConvertorException e) {
          log.error(e.getMessage(), e);
        }
      }
View Full Code Here

        return ENHANCE_SYNCHRONOUS;
    }

    public void computeEnhancements(ContentItem ci) throws EngineException {
        FeatureStructureListHolder holder;
        LiteralFactory literalFactory = LiteralFactory.getInstance();


        try {
            UriRef uimaUriRef = new UriRef(uimaUri);
            logger.info(new StringBuilder("Trying to load holder for ref:").append(uimaUri).toString());
            holder = ci.getPart(uimaUriRef, 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());
                        UriRef textAnnotation = EnhancementEngineHelper.createTextEnhancement(
                                ci, this);
                        MGraph metadata = ci.getMetadata();
                        String uriRefStr = uimaUri + ":" + typeName;
                        if (mappings.containsKey(typeName)) {
                            uriRefStr = mappings.get(typeName);
                        }
                        metadata.add(new TripleImpl(textAnnotation, DC_TYPE, new UriRef(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())));
                        }
View Full Code Here

        }
        UriRef precision = new UriRef(NamespaceEnum.fise + "classifier/precision");
        UriRef recall = new UriRef(NamespaceEnum.fise + "classifier/recall");
        UriRef f1 = new UriRef(NamespaceEnum.fise + "classifier/f1");

        LiteralFactory lf = LiteralFactory.getInstance();
        ci.getLock().writeLock().lock();
        try {
            // Global text annotation to attach all the topic annotation to it.
            UriRef textAnnotation = EnhancementEngineHelper.createTextEnhancement(ci, this);
            metadata.add(new TripleImpl(textAnnotation,
                    org.apache.stanbol.enhancer.servicesapi.rdf.Properties.DC_TYPE,
                    OntologicalClasses.SKOS_CONCEPT));
            for (TopicSuggestion topic : topics) {
                UriRef enhancement = EnhancementEngineHelper.createEntityEnhancement(ci, this);
                metadata.add(new TripleImpl(enhancement,
                        org.apache.stanbol.enhancer.servicesapi.rdf.Properties.RDF_TYPE,
                        TechnicalClasses.ENHANCER_TOPICANNOTATION));
                metadata.add(new TripleImpl(enhancement,
                        org.apache.stanbol.enhancer.servicesapi.rdf.Properties.DC_RELATION, textAnnotation));

                // add link to entity
                metadata.add(new TripleImpl(enhancement,
                        org.apache.stanbol.enhancer.servicesapi.rdf.Properties.ENHANCER_ENTITY_REFERENCE,
                        new UriRef(topic.conceptUri)));
                metadata.add(new TripleImpl(enhancement,
                        org.apache.stanbol.enhancer.servicesapi.rdf.Properties.ENHANCER_ENTITY_TYPE,
                        OntologicalClasses.SKOS_CONCEPT));

                // add confidence information
                metadata.add(new TripleImpl(enhancement,
                        org.apache.stanbol.enhancer.servicesapi.rdf.Properties.ENHANCER_CONFIDENCE, lf
                                .createTypedLiteral(Double.valueOf(topic.score))));

                // add performance estimates of the classifier if available
                ClassificationReport perf = getPerformanceEstimates(topic.conceptUri);
                if (perf.uptodate) {
                    metadata.add(new TripleImpl(enhancement, precision, lf.createTypedLiteral(Double
                            .valueOf(perf.precision))));
                    metadata.add(new TripleImpl(enhancement, recall, lf.createTypedLiteral(Double
                            .valueOf(perf.recall))));
                    metadata.add(new TripleImpl(enhancement, f1, lf.createTypedLiteral(Double
                            .valueOf(perf.f1))));
                }
                // fetch concept label from the entityhub or a referenced site if available
                Entity entity = entityhub.getEntity(topic.conceptUri);
                if (entity == null) {
View Full Code Here

                new Mapping.Converter(){//we need to convert from MByte/min to kByte/sec
                    @Override
                    public Resource convert(Resource value) {
                        if(value instanceof TypedLiteral &&
                                XSD.double_.equals(((TypedLiteral)value).getDataType())){
                            LiteralFactory lf = LiteralFactory.getInstance();
                            double mm = lf.createObject(Double.class, (TypedLiteral)value);
                            return lf.createTypedLiteral(Double.valueOf(
                                mm*1024/60));
                        } else {
                            return value; //do not convert
                        }
                    }
View Full Code Here

        confidence
    }

    private void initOccurrences() {
        MGraph graph = contentItem.getMetadata();
        LiteralFactory lf = LiteralFactory.getInstance();
        Map<UriRef,Collection<NonLiteral>> suggestionMap = new HashMap<UriRef,Collection<NonLiteral>>();
        // 1) get Entity Annotations
        Map<NonLiteral,Map<EAProps,Object>> entitySuggestionMap = new HashMap<NonLiteral,Map<EAProps,Object>>();
        Iterator<Triple> entityAnnotations = graph.filter(null, RDF.type, ENHANCER_ENTITYANNOTATION);
        while(entityAnnotations.hasNext()){
View Full Code Here

    /**
     * @return an RDF/JSON descriptions of places for the word map widget
     */
    public String getPlacesAsJSON() throws ParseException, UnsupportedEncodingException {
        MGraph g = new IndexedMGraph();
        LiteralFactory lf = LiteralFactory.getInstance();
        MGraph metadata = contentItem.getMetadata();
        for (EntityExtractionSummary p : getPlaceOccurrences()) {
            EntitySuggestion bestGuess = p.getBestGuess();
            if (bestGuess == null) {
                continue;
            }
            UriRef uri = new UriRef(bestGuess.getUri());
            Iterator<Triple> latitudes = metadata.filter(uri, GEO_LAT, null);
            if (latitudes.hasNext()) {
                g.add(latitudes.next());
            }
            Iterator<Triple> longitutes = metadata.filter(uri, GEO_LONG, null);
            if (longitutes.hasNext()) {
                g.add(longitutes.next());
                g.add(new TripleImpl(uri, Properties.RDFS_LABEL, lf.createTypedLiteral(bestGuess.getLabel())));
            }
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        serializer.serialize(out, g, SupportedFormat.RDF_JSON);
       
View Full Code Here

        } else {
            language = null;
        }
        log.debug("findNamedEntities typeUri={}, type={}, text=",
                new Object[]{ typeUri, typeLabel, StringUtils.abbreviate(text, 100) });
        LiteralFactory literalFactory = LiteralFactory.getInstance();
        MGraph g = ci.getMetadata();
        Map<String,List<NameOccurrence>> entityNames = extractNameOccurrences(nameFinderModel, text);
        //lock the ContentItem while writing the RDF data for found Named Entities
        ci.getLock().writeLock().lock();
        try {
            Map<String,UriRef> previousAnnotations = new LinkedHashMap<String,UriRef>();
            for (Map.Entry<String,List<NameOccurrence>> nameInContext : entityNames.entrySet()) {
   
                String name = nameInContext.getKey();
                List<NameOccurrence> occurrences = nameInContext.getValue();
   
                UriRef firstOccurrenceAnnotation = null;
   
                for (NameOccurrence occurrence : occurrences) {
                    UriRef textAnnotation = EnhancementEngineHelper.createTextEnhancement(ci, this);
                    g.add(new TripleImpl(textAnnotation, ENHANCER_SELECTED_TEXT,
                        new PlainLiteralImpl(name, language)));
                    g.add(new TripleImpl(textAnnotation, ENHANCER_SELECTION_CONTEXT,
                        new PlainLiteralImpl(occurrence.context, language)));
                    g.add(new TripleImpl(textAnnotation, DC_TYPE, typeUri));
                    g.add(new TripleImpl(textAnnotation, ENHANCER_CONFIDENCE, literalFactory
                            .createTypedLiteral(occurrence.confidence)));
                    if (occurrence.start != null && occurrence.end != null) {
                        g.add(new TripleImpl(textAnnotation, ENHANCER_START, literalFactory
                                .createTypedLiteral(occurrence.start)));
                        g.add(new TripleImpl(textAnnotation, ENHANCER_END, literalFactory
                                .createTypedLiteral(occurrence.end)));
                    }
   
                    // add the subsumption relationship among occurrences of the same
                    // name
View Full Code Here

     *
     * @param occs a Collection of entity information
     * @param ci the content item
     */
    public void createEnhancements(Collection<CalaisEntityOccurrence> occs, ContentItem ci) {
        LiteralFactory literalFactory = LiteralFactory.getInstance();
        final Language language; // used for plain literals representing parts fo the content
        String langString = getMetadataLanguage(ci.getMetadata(), null);
        if(langString != null && !langString.isEmpty()){
            language = new Language(langString);
        } else {
            language = null;
        }
        //TODO create TextEnhancement (form, start, end, type?) and EntityAnnotation (id, name, type)
        HashMap<Resource, UriRef> entityAnnotationMap = new HashMap<Resource, UriRef>();
        for (CalaisEntityOccurrence occ : occs) {
            UriRef textAnnotation = EnhancementEngineHelper.createTextEnhancement(
                    ci, this);
            MGraph model = ci.getMetadata();
            model.add(new TripleImpl(textAnnotation, DC_TYPE, occ.type));
            // for autotagger use the name instead of the matched term (that might be a pronoun!)
            if (onlyNERMode) {
                model.add(new TripleImpl(textAnnotation, ENHANCER_SELECTED_TEXT,new PlainLiteralImpl(occ.name,language)));
            }
            else {
                model.add(new TripleImpl(textAnnotation, ENHANCER_SELECTED_TEXT, new PlainLiteralImpl(occ.exact,language)));
            }
            model.add(new TripleImpl(textAnnotation, ENHANCER_START, literalFactory.createTypedLiteral(occ.offset)));
            model.add(new TripleImpl(textAnnotation, ENHANCER_END, literalFactory.createTypedLiteral(occ.offset + occ.length)));
            model.add(new TripleImpl(textAnnotation, ENHANCER_SELECTION_CONTEXT, new PlainLiteralImpl(occ.context,language)));
            //create EntityAnnotation only once but add a reference to the textAnnotation
            if (entityAnnotationMap.containsKey(occ.id)) {
                model.add(new TripleImpl(entityAnnotationMap.get(occ.id), DC_RELATION, textAnnotation));
            } else {
View Full Code Here

     *
     * @return the URI of the new enhancement instance
     */
    protected static UriRef createEnhancement(MGraph metadata,
            EnhancementEngine engine, UriRef contentItemId){
        LiteralFactory literalFactory = LiteralFactory.getInstance();

        UriRef enhancement = new UriRef("urn:enhancement-"
                + EnhancementEngineHelper.randomUUID());
        //add the Enhancement Type
        metadata.add(new TripleImpl(enhancement, Properties.RDF_TYPE,
                TechnicalClasses.ENHANCER_ENHANCEMENT));
        //add the extracted from content item
        metadata.add(new TripleImpl(enhancement,
                Properties.ENHANCER_EXTRACTED_FROM, contentItemId));
        // creation date
        metadata.add(new TripleImpl(enhancement, Properties.DC_CREATED,
                literalFactory.createTypedLiteral(new Date())));

        // the engines that extracted the data
        // TODO: add some kind of versioning info for the extractor?
        // TODO: use a public dereferencing URI instead? that would allow for
        // explicit versioning too
        /* NOTE (Rupert Westenthaler 2010-05-26):
         * The Idea is to use the  ComponentContext in the activate() method of
         * an Enhancer to get the bundle name/version and use that as an
         * URI for the creator.
         * We would need to add getEnhancerID() method to the enhancer interface
         * to access this information
          */
        metadata.add(new TripleImpl(enhancement, Properties.DC_CREATOR,
                literalFactory.createTypedLiteral(engine.getClass().getName())));
        return enhancement;
    }
View Full Code Here

TOP

Related Classes of org.apache.clerezza.rdf.core.LiteralFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.