Search in sources :

Example 6 with Type

use of org.apache.uima.cas.Type in project lucene-solr by apache.

the class UIMAToSolrMapper method map.

/**
   * map features of a certain UIMA type to corresponding Solr fields based on the mapping
   *
   * @param typeName             name of UIMA type to map
   */
void map(String typeName, Map<String, MapField> featureFieldsmapping) throws FieldMappingException {
    try {
        Type type = cas.getTypeSystem().getType(typeName);
        for (FSIterator<FeatureStructure> iterator = cas.getFSIndexRepository().getAllIndexedFS(type); iterator.hasNext(); ) {
            FeatureStructure fs = iterator.next();
            for (String featureName : featureFieldsmapping.keySet()) {
                MapField mapField = featureFieldsmapping.get(featureName);
                String fieldNameFeature = mapField.getFieldNameFeature();
                String fieldNameFeatureValue = fieldNameFeature == null ? null : fs.getFeatureValueAsString(type.getFeatureByBaseName(fieldNameFeature));
                String fieldName = mapField.getFieldName(fieldNameFeatureValue);
                if (log.isInfoEnabled()) {
                    log.info("mapping {}@{} to {}", new Object[] { typeName, featureName, fieldName });
                }
                String featureValue;
                if (fs instanceof Annotation && "coveredText".equals(featureName)) {
                    featureValue = ((Annotation) fs).getCoveredText();
                } else {
                    featureValue = fs.getFeatureValueAsString(type.getFeatureByBaseName(featureName));
                }
                if (log.isDebugEnabled()) {
                    log.debug("writing {} in {}", new Object[] { featureValue, fieldName });
                }
                document.addField(fieldName, featureValue);
            }
        }
    } catch (Exception e) {
        throw new FieldMappingException(e);
    }
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) Type(org.apache.uima.cas.Type) Annotation(org.apache.uima.jcas.tcas.Annotation) MapField(org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField)

Example 7 with Type

use of org.apache.uima.cas.Type in project stanbol by apache.

the class UIMALocal method concertToCasLight.

private List<FeatureStructure> concertToCasLight(JCas jcas, String typeName) {
    List<FeatureStructure> ret = new ArrayList<FeatureStructure>();
    Type type = jcas.getTypeSystem().getType(typeName);
    List<org.apache.uima.cas.Feature> featList = type.getFeatures();
    for (FSIterator<org.apache.uima.cas.FeatureStructure> iterator = jcas.getFSIndexRepository().getAllIndexedFS(type); iterator.hasNext(); ) {
        org.apache.uima.cas.FeatureStructure casFs = iterator.next();
        logger.debug("Processing UIMA CAS FeatureSet:" + casFs.toString());
        FeatureStructure newFs = new FeatureStructure(UUID.randomUUID().toString(), type.getShortName());
        for (org.apache.uima.cas.Feature casF : featList) {
            String fName = casF.getShortName();
            logger.debug("Feature Name:" + fName);
            if (casF.getRange().getName().equals("uima.cas.Sofa")) {
                continue;
            }
            if (casF.getRange().isPrimitive()) {
                logger.debug("Getting primitive value...");
                if (casF.getRange().getName().equals("uima.cas.String")) {
                    String fVal = casFs.getStringValue(casF);
                    newFs.addFeature(new Feature<String>(fName, fVal));
                } else if (casF.getRange().getName().equals("uima.cas.Integer")) {
                    int fVal = casFs.getIntValue(casF);
                    newFs.addFeature(new Feature<Integer>(fName, fVal));
                } else if (casF.getRange().getName().equals("uima.cas.Short")) {
                    short fVal = casFs.getShortValue(casF);
                    newFs.addFeature(new Feature<Integer>(fName, (int) fVal));
                } else if (casF.getRange().getName().equals("uima.cas.Byte")) {
                    byte fVal = casFs.getByteValue(casF);
                    newFs.addFeature(new Feature<Integer>(fName, (int) fVal));
                } else if (casF.getRange().getName().equals("uima.cas.Double")) {
                    double fVal = casFs.getDoubleValue(casF);
                    newFs.addFeature(new Feature<Double>(fName, fVal));
                } else if (casF.getRange().getName().equals("uima.cas.Float")) {
                    float fVal = casFs.getFloatValue(casF);
                    newFs.addFeature(new Feature<Double>(fName, (double) fVal));
                } else {
                    Object fVal = casFs.clone();
                    newFs.addFeature(new Feature<Object>(fName, fVal));
                }
            } else {
                logger.debug("Getting FeatureStructure value...");
                throw new UnsupportedOperationException("This client cannot handle FeatureStructure features");
            }
            if (casFs instanceof Annotation && "coveredText".equals(fName)) {
                newFs.setCoveredText(((Annotation) casFs).getCoveredText());
            }
        }
        logger.debug("FeatureStructure:" + newFs);
        ret.add(newFs);
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) Feature(org.apache.stanbol.commons.caslight.Feature) Annotation(org.apache.uima.jcas.tcas.Annotation) FeatureStructure(org.apache.stanbol.commons.caslight.FeatureStructure) Type(org.apache.uima.cas.Type)

Aggregations

Type (org.apache.uima.cas.Type)7 Annotation (org.apache.uima.jcas.tcas.Annotation)4 IOException (java.io.IOException)2 AnalysisEngineProcessException (org.apache.uima.analysis_engine.AnalysisEngineProcessException)2 Feature (org.apache.uima.cas.Feature)2 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)2 ResourceInitializationException (org.apache.uima.resource.ResourceInitializationException)2 ArrayList (java.util.ArrayList)1 MapField (org.apache.solr.uima.processor.SolrUIMAConfiguration.MapField)1 Feature (org.apache.stanbol.commons.caslight.Feature)1 FeatureStructure (org.apache.stanbol.commons.caslight.FeatureStructure)1 TokenAnnotation (org.apache.uima.TokenAnnotation)1 CASException (org.apache.uima.cas.CASException)1 FeatureStructure (org.apache.uima.cas.FeatureStructure)1