Search in sources :

Example 41 with Feature

use of org.apache.uima.cas.Feature in project webanno by webanno.

the class AutomationUtil method repeateRelationAnnotation.

public static void repeateRelationAnnotation(AnnotatorState aState, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, AnnotationFS fs, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
    for (SourceDocument d : aDocumentService.listSourceDocuments(aState.getProject())) {
        loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aState.getUser());
        JCas jCas = aCorrectionDocumentService.readCorrectionCas(d);
        ArcAdapter adapter = (ArcAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
        String sourceFName = adapter.getSourceFeatureName();
        String targetFName = adapter.getTargetFeatureName();
        Type type = getType(jCas.getCas(), aFeature.getLayer().getName());
        Type spanType = getType(jCas.getCas(), adapter.getAttachTypeName());
        Feature arcSpanFeature = spanType.getFeatureByBaseName(adapter.getAttachFeatureName());
        Feature dependentFeature = type.getFeatureByBaseName(targetFName);
        Feature governorFeature = type.getFeatureByBaseName(sourceFName);
        AnnotationFS dependentFs = null;
        AnnotationFS governorFs = null;
        if (adapter.getAttachFeatureName() != null) {
            dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
            governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
        } else {
            dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
            governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature);
        }
        if (adapter.isCrossMultipleSentence()) {
            List<AnnotationFS> mSpanAnnos = new ArrayList<>(getAllAnnoFss(jCas, governorFs.getType()));
            repeatRelation(aState, 0, jCas.getDocumentText().length() - 1, aFeature, aValue, jCas, adapter, dependentFs, governorFs, mSpanAnnos);
        } else {
            for (Sentence sent : select(jCas, Sentence.class)) {
                List<AnnotationFS> spanAnnos = selectCovered(governorFs.getType(), sent);
                repeatRelation(aState, sent.getBegin(), sent.getEnd(), aFeature, aValue, jCas, adapter, dependentFs, governorFs, spanAnnos);
            }
        }
        aCorrectionDocumentService.writeCorrectionCas(jCas, d);
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) ArcAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ArrayList(java.util.ArrayList) JCas(org.apache.uima.jcas.JCas) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)

Example 42 with Feature

use of org.apache.uima.cas.Feature in project webanno by webanno.

the class ImportExportServiceImpl method updateCasWithTagSet.

/**
 * A Helper method to add {@link TagsetDescription} to {@link CAS}
 *
 * @param aCas
 *            the CAA.
 * @param aLayer
 *            the layer.
 * @param aTagSetName
 *            the tagset.
 */
private static void updateCasWithTagSet(CAS aCas, String aLayer, String aTagSetName) {
    Type TagsetType = CasUtil.getType(aCas, TagsetDescription.class);
    Feature layerFeature = TagsetType.getFeatureByBaseName("layer");
    Feature nameFeature = TagsetType.getFeatureByBaseName("name");
    boolean tagSetModified = false;
    // modify existing tagset Name
    for (FeatureStructure fs : CasUtil.select(aCas, TagsetType)) {
        String layer = fs.getStringValue(layerFeature);
        String tagSetName = fs.getStringValue(nameFeature);
        if (layer.equals(aLayer)) {
            // only if the tagset name is changed
            if (!aTagSetName.equals(tagSetName)) {
                fs.setStringValue(nameFeature, aTagSetName);
                aCas.addFsToIndexes(fs);
            }
            tagSetModified = true;
            break;
        }
    }
    if (!tagSetModified) {
        FeatureStructure fs = aCas.createFS(TagsetType);
        fs.setStringValue(layerFeature, aLayer);
        fs.setStringValue(nameFeature, aTagSetName);
        aCas.addFsToIndexes(fs);
    }
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) Type(org.apache.uima.cas.Type) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 43 with Feature

use of org.apache.uima.cas.Feature in project webanno by webanno.

the class ChainRenderer method render.

@Override
public void render(JCas aJcas, List<AnnotationFeature> aFeatures, VDocument aResponse, AnnotatorState aState) {
    List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
    // Find the features for the arc and span labels - it is possible that we do not find a
    // feature for arc/span labels because they may have been disabled.
    AnnotationFeature spanLabelFeature = null;
    AnnotationFeature arcLabelFeature = null;
    for (AnnotationFeature f : visibleFeatures) {
        if (WebAnnoConst.COREFERENCE_TYPE_FEATURE.equals(f.getName())) {
            spanLabelFeature = f;
        }
        if (WebAnnoConst.COREFERENCE_RELATION_FEATURE.equals(f.getName())) {
            arcLabelFeature = f;
        }
    }
    // At this point arc and span feature labels must have been found! If not, the later code
    // will crash.
    ChainAdapter typeAdapter = getTypeAdapter();
    Type chainType = typeAdapter.getAnnotationType(aJcas.getCas());
    Feature chainFirst = chainType.getFeatureByBaseName(typeAdapter.getChainFirstFeatureName());
    int colorIndex = 0;
    // Iterate over the chains
    for (FeatureStructure chainFs : selectFS(aJcas.getCas(), chainType)) {
        AnnotationFS linkFs = (AnnotationFS) chainFs.getFeatureValue(chainFirst);
        AnnotationFS prevLinkFs = null;
        // Iterate over the links of the chain
        while (linkFs != null) {
            Feature linkNext = linkFs.getType().getFeatureByBaseName(typeAdapter.getLinkNextFeatureName());
            AnnotationFS nextLinkFs = (AnnotationFS) linkFs.getFeatureValue(linkNext);
            // Is link after window? If yes, we can skip the rest of the chain
            if (linkFs.getBegin() >= aState.getWindowEndOffset()) {
                // Go to next chain
                break;
            }
            // end within the window
            if (!(linkFs.getBegin() >= aState.getWindowBeginOffset()) && (linkFs.getEnd() <= aState.getWindowEndOffset())) {
                // prevLinkFs remains null until we enter the window
                linkFs = nextLinkFs;
                // Go to next link
                continue;
            }
            String bratTypeName = TypeUtil.getUiTypeName(typeAdapter);
            // Render span
            {
                String bratLabelText = TypeUtil.getUiLabelText(typeAdapter, linkFs, (spanLabelFeature != null) ? asList(spanLabelFeature) : emptyList());
                String bratHoverText = TypeUtil.getUiHoverText(typeAdapter, linkFs, (spanLabelFeature != null) ? asList(spanLabelFeature) : emptyList());
                VRange offsets = new VRange(linkFs.getBegin() - aState.getWindowBeginOffset(), linkFs.getEnd() - aState.getWindowBeginOffset());
                aResponse.add(new VSpan(typeAdapter.getLayer(), linkFs, bratTypeName, offsets, colorIndex, singletonMap("label", bratLabelText), singletonMap("label", bratHoverText)));
            }
            // and last link are within the window ;)
            if (prevLinkFs != null) {
                String bratLabelText = null;
                if (typeAdapter.isLinkedListBehavior() && arcLabelFeature != null) {
                    // Render arc label
                    bratLabelText = TypeUtil.getUiLabelText(typeAdapter, prevLinkFs, asList(arcLabelFeature));
                } else {
                    // Render only chain type
                    bratLabelText = TypeUtil.getUiLabelText(typeAdapter, prevLinkFs, emptyList());
                }
                aResponse.add(new VArc(typeAdapter.getLayer(), new VID(prevLinkFs, 1, VID.NONE, VID.NONE), bratTypeName, prevLinkFs, linkFs, colorIndex, singletonMap("label", bratLabelText)));
            }
            // Render errors if required features are missing
            renderRequiredFeatureErrors(visibleFeatures, linkFs, aResponse);
            // if (BratAjaxCasUtil.isSame(linkFs, nextLinkFs)) {
            // log.error("Loop in CAS detected, aborting rendering of chains");
            // break;
            // }
            prevLinkFs = linkFs;
            linkFs = nextLinkFs;
        }
        // The color index is updated even for chains that have no visible links in the current
        // window because we would like the chain color to be independent of visibility. In
        // particular the color of a chain should not change when switching pages/scrolling.
        colorIndex++;
    }
}
Also used : JCas(org.apache.uima.jcas.JCas) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) Collections.emptyList(java.util.Collections.emptyList) WebAnnoConst(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) Feature(org.apache.uima.cas.Feature) Collectors(java.util.stream.Collectors) Type(org.apache.uima.cas.Type) VSpan(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSpan) List(java.util.List) TypeUtil(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) VRange(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VRange) Arrays.asList(java.util.Arrays.asList) ChainAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter) Collections.singletonMap(java.util.Collections.singletonMap) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) FeatureStructure(org.apache.uima.cas.FeatureStructure) CasUtil.selectFS(org.apache.uima.fit.util.CasUtil.selectFS) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) FeatureSupportRegistry(de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry) VSpan(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSpan) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) FeatureStructure(org.apache.uima.cas.FeatureStructure) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) VRange(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VRange) ChainAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 44 with Feature

use of org.apache.uima.cas.Feature in project webanno by webanno.

the class Renderer method getHoverFeatures.

default Map<String, String> getHoverFeatures(TypeAdapter aAdapter, AnnotationFS aFs, List<AnnotationFeature> aFeatures) {
    FeatureSupportRegistry fsr = getFeatureSupportRegistry();
    Map<String, String> hoverfeatures = new LinkedHashMap<>();
    if (aAdapter.getLayer().isShowTextInHover())
        hoverfeatures.put("__spantext__", aFs.getCoveredText());
    for (AnnotationFeature feature : aFeatures) {
        if (!feature.isEnabled() || !feature.isIncludeInHover() || !MultiValueMode.NONE.equals(feature.getMultiValueMode())) {
            continue;
        }
        Feature labelFeature = aFs.getType().getFeatureByBaseName(feature.getName());
        String text = defaultString(fsr.getFeatureSupport(feature).renderFeatureValue(feature, aFs, labelFeature));
        hoverfeatures.put(feature.getName(), text);
    }
    return hoverfeatures;
}
Also used : FeatureSupportRegistry(de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry) StringUtils.defaultString(org.apache.commons.lang3.StringUtils.defaultString) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) LinkedHashMap(java.util.LinkedHashMap) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 45 with Feature

use of org.apache.uima.cas.Feature in project webanno by webanno.

the class ArcAdapter method interalAddToCas.

/**
 * @param aWindowBegin
 *            begin offset of the first visible sentence
 * @param aWindowEnd
 *            end offset of the last visible sentence
 */
private AnnotationFS interalAddToCas(JCas aJCas, int aWindowBegin, int aWindowEnd, AnnotationFS aOriginFs, AnnotationFS aTargetFs) throws AnnotationException {
    Type type = getType(aJCas.getCas(), annotationTypeName);
    Feature dependentFeature = type.getFeatureByBaseName(targetFeatureName);
    Feature governorFeature = type.getFeatureByBaseName(sourceFeatureName);
    Type spanType = getType(aJCas.getCas(), attachType);
    AnnotationFS dependentFs = null;
    AnnotationFS governorFs = null;
    // presently visible on screen is sufficient - we don't have to scan the whole CAS.
    for (AnnotationFS fs : selectCovered(aJCas.getCas(), type, aWindowBegin, aWindowEnd)) {
        if (attachFeatureName != null) {
            Feature arcSpanFeature = spanType.getFeatureByBaseName(attachFeatureName);
            dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
            governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
        } else {
            dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
            governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature);
        }
        if (dependentFs == null || governorFs == null) {
            log.warn("Relation [" + getLayer().getName() + "] with id [" + getAddr(fs) + "] has loose ends - ignoring during while checking for duplicates.");
            continue;
        }
        // update the label of the existing arc
        if (!allowStacking && isDuplicate(governorFs, aOriginFs, dependentFs, aTargetFs)) {
            throw new AnnotationException("Cannot create another annotation of layer [" + getLayer().getUiName() + "] at this location - stacking is not " + "enabled for this layer.");
        }
    }
    // It is new ARC annotation, create it
    dependentFs = aTargetFs;
    governorFs = aOriginFs;
    // for POS annotation, since custom span layers do not have attach feature
    if (attachFeatureName != null) {
        dependentFs = selectCovered(aJCas.getCas(), spanType, dependentFs.getBegin(), dependentFs.getEnd()).get(0);
        governorFs = selectCovered(aJCas.getCas(), spanType, governorFs.getBegin(), governorFs.getEnd()).get(0);
    }
    if (dependentFs == null || governorFs == null) {
        throw new AnnotationException("Relation must have a source and a target!");
    }
    // Set the relation offsets in DKPro Core style - the relation recieves the offsets from
    // the dependent
    // If origin and target spans are multiple tokens, dependentFS.getBegin will be the
    // the begin position of the first token and dependentFS.getEnd will be the End
    // position of the last token.
    AnnotationFS newAnnotation = aJCas.getCas().createAnnotation(type, dependentFs.getBegin(), dependentFs.getEnd());
    newAnnotation.setFeatureValue(dependentFeature, dependentFs);
    newAnnotation.setFeatureValue(governorFeature, governorFs);
    aJCas.addFsToIndexes(newAnnotation);
    return newAnnotation;
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Aggregations

Feature (org.apache.uima.cas.Feature)84 Type (org.apache.uima.cas.Type)62 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)50 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)48 ArrayList (java.util.ArrayList)23 FeatureStructure (org.apache.uima.cas.FeatureStructure)18 CasUtil.getType (org.apache.uima.fit.util.CasUtil.getType)18 JCas (org.apache.uima.jcas.JCas)18 List (java.util.List)15 Test (org.junit.Test)14 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)13 WebAnnoCasUtil.setFeature (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.setFeature)12 POS (de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS)12 CAS (org.apache.uima.cas.CAS)10 HashSet (java.util.HashSet)8 LinkedHashMap (java.util.LinkedHashMap)8 Map (java.util.Map)8 HashMap (java.util.HashMap)7 TypeSystem (org.apache.uima.cas.TypeSystem)7 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)6