Search in sources :

Example 1 with JCas

use of org.apache.uima.jcas.JCas in project stanbol by apache.

the class UIMALocal method processText.

/*
     * process a field value executing UIMA the CAS containing it as document
     * text - From SOLR.
     */
private JCas processText(String textFieldValue) throws ResourceInitializationException, AnalysisEngineProcessException {
    logger.info(new StringBuffer("Analazying text").toString());
    /*
         * get the UIMA analysis engine
         */
    AnalysisEngine ae = aeProvider.getAE();
    /*
         * create a JCas which contain the text to analyze
         */
    JCas jcas = ae.newJCas();
    jcas.setDocumentText(textFieldValue);
    /*
         * perform analysis on text field
         */
    ae.process(jcas);
    logger.info(new StringBuilder("Text processing completed").toString());
    return jcas;
}
Also used : JCas(org.apache.uima.jcas.JCas) AnalysisEngine(org.apache.uima.analysis_engine.AnalysisEngine)

Example 2 with JCas

use of org.apache.uima.jcas.JCas in project lucene-solr by apache.

the class UIMAUpdateRequestProcessor method processAdd.

@Override
public void processAdd(AddUpdateCommand cmd) throws IOException {
    String text = null;
    try {
        /* get Solr document */
        SolrInputDocument solrInputDocument = cmd.getSolrInputDocument();
        /* get the fields to analyze */
        String[] texts = getTextsToAnalyze(solrInputDocument);
        for (String currentText : texts) {
            text = currentText;
            if (text != null && text.length() > 0) {
                /* create a JCas which contain the text to analyze */
                JCas jcas = pool.getJCas(0);
                try {
                    /* process the text value */
                    processText(text, jcas);
                    UIMAToSolrMapper uimaToSolrMapper = new UIMAToSolrMapper(solrInputDocument, jcas);
                    /* get field mapping from config */
                    Map<String, Map<String, MapField>> typesAndFeaturesFieldsMap = solrUIMAConfiguration.getTypesFeaturesFieldsMapping();
                    /* map type features on fields */
                    for (Entry<String, Map<String, MapField>> entry : typesAndFeaturesFieldsMap.entrySet()) {
                        uimaToSolrMapper.map(entry.getKey(), entry.getValue());
                    }
                } finally {
                    pool.releaseJCas(jcas);
                }
            }
        }
    } catch (Exception e) {
        String logField = solrUIMAConfiguration.getLogField();
        if (logField == null) {
            SchemaField uniqueKeyField = cmd.getReq().getSchema().getUniqueKeyField();
            if (uniqueKeyField != null) {
                logField = uniqueKeyField.getName();
            }
        }
        String optionalFieldInfo = logField == null ? "." : ". " + logField + "=" + cmd.getSolrInputDocument().getField(logField).getValue() + ", ";
        int len;
        String debugString;
        if (text != null && text.length() > 0) {
            len = Math.min(text.length(), 100);
            debugString = " text=\"" + text.substring(0, len) + "...\"";
        } else {
            debugString = " null text";
        }
        if (solrUIMAConfiguration.isIgnoreErrors()) {
            log.warn("skip the text processing due to {}", new StringBuilder().append(e.getLocalizedMessage()).append(optionalFieldInfo).append(debugString));
        } else {
            throw new SolrException(ErrorCode.SERVER_ERROR, "processing error " + e.getLocalizedMessage() + optionalFieldInfo + debugString, e);
        }
    }
    super.processAdd(cmd);
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SolrInputDocument(org.apache.solr.common.SolrInputDocument) JCas(org.apache.uima.jcas.JCas) Map(java.util.Map) IOException(java.io.IOException) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) SolrException(org.apache.solr.common.SolrException) AnalysisEngineProcessException(org.apache.uima.analysis_engine.AnalysisEngineProcessException) SolrException(org.apache.solr.common.SolrException)

Example 3 with JCas

use of org.apache.uima.jcas.JCas in project stanbol by apache.

the class UIMALocal method computeEnhancements.

@Override
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 an supported Mimetype '" + SUPPORTED_MIMETYPES + "' found for ContentItem " + ci.getUri() + ": This is also checked in the canEnhance method! -> This " + "indicated an Bug in the implementation of the " + "EnhancementJobManager!");
    }
    String text;
    try {
        text = ContentItemHelper.getText(contentPart.getValue());
    } catch (IOException e) {
        throw new InvalidContentException(this, ci, e);
    }
    JCas jcas;
    try {
        logger.info("Processing text with UIMA AE...");
        jcas = processText(text);
    } catch (ResourceInitializationException ex) {
        logger.error("Error initializing UIMA AE", ex);
        throw new EngineException("Error initializing UIMA AE", ex);
    } catch (AnalysisEngineProcessException ex) {
        logger.error("Error running UIMA AE", ex);
        throw new EngineException("Error running UIMA AE", ex);
    }
    //just for being sure
    if (jcas == null) {
        return;
    }
    for (String typeName : uimaTypeNames) {
        List<FeatureStructure> featureSetList = concertToCasLight(jcas, typeName);
        IRI uimaIRI = new IRI(uimaUri);
        FeatureStructureListHolder holder;
        ci.getLock().writeLock().lock();
        try {
            holder = ci.getPart(uimaIRI, FeatureStructureListHolder.class);
        } catch (NoSuchPartException e) {
            holder = new FeatureStructureListHolder();
            logger.info("Adding FeatureSet List Holder content part with uri:" + uimaUri);
            ci.addPart(uimaIRI, holder);
            logger.info(uimaUri + " content part added.");
        } finally {
            ci.getLock().writeLock().unlock();
        }
        ci.getLock().writeLock().lock();
        try {
            holder.addFeatureStructureList(uimaSourceName, featureSetList);
        } finally {
            ci.getLock().writeLock().unlock();
        }
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) JCas(org.apache.uima.jcas.JCas) NoSuchPartException(org.apache.stanbol.enhancer.servicesapi.NoSuchPartException) IOException(java.io.IOException) AnalysisEngineProcessException(org.apache.uima.analysis_engine.AnalysisEngineProcessException) FeatureStructure(org.apache.stanbol.commons.caslight.FeatureStructure) InvalidContentException(org.apache.stanbol.enhancer.servicesapi.InvalidContentException) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) FeatureStructureListHolder(org.apache.stanbol.commons.caslight.FeatureStructureListHolder)

Aggregations

JCas (org.apache.uima.jcas.JCas)3 IOException (java.io.IOException)2 AnalysisEngineProcessException (org.apache.uima.analysis_engine.AnalysisEngineProcessException)2 ResourceInitializationException (org.apache.uima.resource.ResourceInitializationException)2 Map (java.util.Map)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 SolrException (org.apache.solr.common.SolrException)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 SchemaField (org.apache.solr.schema.SchemaField)1 FeatureStructure (org.apache.stanbol.commons.caslight.FeatureStructure)1 FeatureStructureListHolder (org.apache.stanbol.commons.caslight.FeatureStructureListHolder)1 Blob (org.apache.stanbol.enhancer.servicesapi.Blob)1 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)1 InvalidContentException (org.apache.stanbol.enhancer.servicesapi.InvalidContentException)1 NoSuchPartException (org.apache.stanbol.enhancer.servicesapi.NoSuchPartException)1 AnalysisEngine (org.apache.uima.analysis_engine.AnalysisEngine)1