Search in sources :

Example 1 with Feature

use of org.apache.stanbol.commons.caslight.Feature 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 2 with Feature

use of org.apache.stanbol.commons.caslight.Feature in project stanbol by apache.

the class SaxUIMAServletResult2Offsets method startElement.

/*
     * (non-Javadoc)
     *
     * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
     * java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
@Override
public void startElement(String uri, String localName, String qname, Attributes attrs) throws SAXException {
    if (!localName.equals("result")) {
        elementCounter++;
        String type = localName;
        FeatureStructure fs = new FeatureStructure(sourceName + "." + localName + "#" + elementCounter, type);
        for (int i = 0; i < attrs.getLength(); i++) {
            String name = attrs.getQName(i);
            String value = attrs.getValue(i);
            if (checkIfInteger(value)) {
                Feature<Integer> f = new Feature(name, value);
                fs.addFeature(f);
            } else {
                Feature<String> f = new Feature(name, value);
                fs.addFeature(f);
            }
        }
        fsList.add(fs);
    }
}
Also used : FeatureStructure(org.apache.stanbol.commons.caslight.FeatureStructure) Feature(org.apache.stanbol.commons.caslight.Feature)

Example 3 with Feature

use of org.apache.stanbol.commons.caslight.Feature 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

Feature (org.apache.stanbol.commons.caslight.Feature)3 FeatureStructure (org.apache.stanbol.commons.caslight.FeatureStructure)3 ArrayList (java.util.ArrayList)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)1 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)1 LiteralFactory (org.apache.clerezza.rdf.core.LiteralFactory)1 FeatureStructureListHolder (org.apache.stanbol.commons.caslight.FeatureStructureListHolder)1 NoSuchPartException (org.apache.stanbol.enhancer.servicesapi.NoSuchPartException)1 Type (org.apache.uima.cas.Type)1 Annotation (org.apache.uima.jcas.tcas.Annotation)1