Search in sources :

Example 11 with FeatureStructure

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

the class CasDoctorUtils method getNonIndexedFSes.

public static Set<FeatureStructure> getNonIndexedFSes(CAS aCas) {
    TypeSystem ts = aCas.getTypeSystem();
    Set<FeatureStructure> allIndexedFS = collectIndexed(aCas);
    Set<FeatureStructure> allReachableFS = collectReachable(aCas);
    // Remove all that are indexed
    allReachableFS.removeAll(allIndexedFS);
    // Remove all that are not annotations
    allReachableFS.removeIf(fs -> !ts.subsumes(aCas.getAnnotationType(), fs.getType()));
    // All that is left are non-index annotations
    return allReachableFS;
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) TypeSystem(org.apache.uima.cas.TypeSystem)

Example 12 with FeatureStructure

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

the class RelationRenderer method render.

@Override
public void render(final JCas aJcas, List<AnnotationFeature> aFeatures, VDocument aResponse, AnnotatorState aBratAnnotatorModel) {
    List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
    ArcAdapter typeAdapter = getTypeAdapter();
    Type type = getType(aJcas.getCas(), typeAdapter.getAnnotationTypeName());
    int windowBegin = aBratAnnotatorModel.getWindowBeginOffset();
    int windowEnd = aBratAnnotatorModel.getWindowEndOffset();
    Feature dependentFeature = type.getFeatureByBaseName(typeAdapter.getTargetFeatureName());
    Feature governorFeature = type.getFeatureByBaseName(typeAdapter.getSourceFeatureName());
    Type spanType = getType(aJcas.getCas(), typeAdapter.getAttachTypeName());
    Feature arcSpanFeature = spanType.getFeatureByBaseName(typeAdapter.getAttachFeatureName());
    FeatureStructure dependentFs;
    FeatureStructure governorFs;
    Map<Integer, Set<Integer>> relationLinks = getRelationLinks(aJcas, windowBegin, windowEnd, type, dependentFeature, governorFeature, arcSpanFeature);
    // if this is a governor for more than one dependent, avoid duplicate yield
    List<Integer> yieldDeps = new ArrayList<>();
    for (AnnotationFS fs : selectCovered(aJcas.getCas(), type, windowBegin, windowEnd)) {
        if (typeAdapter.getAttachFeatureName() != null) {
            dependentFs = fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
            governorFs = fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
        } else {
            dependentFs = fs.getFeatureValue(dependentFeature);
            governorFs = fs.getFeatureValue(governorFeature);
        }
        String bratTypeName = TypeUtil.getUiTypeName(typeAdapter);
        Map<String, String> features = getFeatures(typeAdapter, fs, visibleFeatures);
        if (dependentFs == null || governorFs == null) {
            RequestCycle requestCycle = RequestCycle.get();
            IPageRequestHandler handler = PageRequestHandlerTracker.getLastHandler(requestCycle);
            Page page = (Page) handler.getPage();
            StringBuilder message = new StringBuilder();
            message.append("Relation [" + typeAdapter.getLayer().getName() + "] with id [" + getAddr(fs) + "] has loose ends - cannot render.");
            if (typeAdapter.getAttachFeatureName() != null) {
                message.append("\nRelation [" + typeAdapter.getLayer().getName() + "] attached to feature [" + typeAdapter.getAttachFeatureName() + "].");
            }
            message.append("\nDependent: " + dependentFs);
            message.append("\nGovernor: " + governorFs);
            page.warn(message.toString());
            continue;
        }
        aResponse.add(new VArc(typeAdapter.getLayer(), fs, bratTypeName, governorFs, dependentFs, features));
        // Render errors if required features are missing
        renderRequiredFeatureErrors(visibleFeatures, fs, aResponse);
        if (relationLinks.keySet().contains(getAddr(governorFs)) && !yieldDeps.contains(getAddr(governorFs))) {
            yieldDeps.add(getAddr(governorFs));
            // sort the annotations (begin, end)
            List<Integer> sortedDepFs = new ArrayList<>(relationLinks.get(getAddr(governorFs)));
            sortedDepFs.sort(comparingInt(arg0 -> selectByAddr(aJcas, arg0).getBegin()));
            String cm = getYieldMessage(aJcas, sortedDepFs);
            aResponse.add(new VComment(governorFs, VCommentType.YIELD, cm));
        }
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Page(org.apache.wicket.Page) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) VCommentType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType) WebAnnoCasUtil.selectByAddr(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectByAddr) LoggerFactory(org.slf4j.LoggerFactory) Feature(org.apache.uima.cas.Feature) ArrayList(java.util.ArrayList) Type(org.apache.uima.cas.Type) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Map(java.util.Map) PageRequestHandlerTracker(org.apache.wicket.request.cycle.PageRequestHandlerTracker) FeatureStructure(org.apache.uima.cas.FeatureStructure) JCas(org.apache.uima.jcas.JCas) WebAnnoCasUtil.getAddr(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.getAddr) Comparator.comparingInt(java.util.Comparator.comparingInt) Logger(org.slf4j.Logger) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) ArcAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter) CasUtil.selectCovered(org.apache.uima.fit.util.CasUtil.selectCovered) List(java.util.List) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) TypeUtil(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) IPageRequestHandler(org.apache.wicket.core.request.handler.IPageRequestHandler) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) FeatureSupportRegistry(de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry) Set(java.util.Set) ConcurrentSkipListSet(java.util.concurrent.ConcurrentSkipListSet) ArcAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) ArrayList(java.util.ArrayList) IPageRequestHandler(org.apache.wicket.core.request.handler.IPageRequestHandler) Page(org.apache.wicket.Page) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) VCommentType(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VCommentType) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 13 with FeatureStructure

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

the class SlotFeatureSupport method getFeatureValue.

@Override
public <T> T getFeatureValue(AnnotationFeature aFeature, FeatureStructure aFS) {
    // Get type and features - we need them later in the loop
    Feature linkFeature = aFS.getType().getFeatureByBaseName(aFeature.getName());
    Type linkType = aFS.getCAS().getTypeSystem().getType(aFeature.getLinkTypeName());
    Feature roleFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeRoleFeatureName());
    Feature targetFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeTargetFeatureName());
    List<LinkWithRoleModel> links = new ArrayList<>();
    ArrayFS array = (ArrayFS) aFS.getFeatureValue(linkFeature);
    if (array != null) {
        for (FeatureStructure link : array.toArray()) {
            LinkWithRoleModel m = new LinkWithRoleModel();
            m.role = link.getStringValue(roleFeat);
            m.targetAddr = WebAnnoCasUtil.getAddr(link.getFeatureValue(targetFeat));
            m.label = ((AnnotationFS) link.getFeatureValue(targetFeat)).getCoveredText();
            links.add(m);
        }
    }
    return (T) links;
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) Type(org.apache.uima.cas.Type) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) ArrayFS(org.apache.uima.cas.ArrayFS) ArrayList(java.util.ArrayList) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 14 with FeatureStructure

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

the class ArcAdapter method delete.

@Override
public void delete(AnnotatorState aState, JCas aJCas, VID aVid) {
    FeatureStructure fs = selectByAddr(aJCas, FeatureStructure.class, aVid.getId());
    aJCas.removeFsFromIndexes(fs);
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure)

Example 15 with FeatureStructure

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

the class ChainAdapter method addArc.

public int addArc(JCas aJCas, AnnotationFS aOriginFs, AnnotationFS aTargetFs) {
    // Determine if the links are adjacent. If so, just update the arc label
    AnnotationFS originNext = getNextLink(aOriginFs);
    AnnotationFS targetNext = getNextLink(aTargetFs);
    // adjacent - origin links to target
    if (WebAnnoCasUtil.isSame(originNext, aTargetFs)) {
    } else // adjacent - target links to origin
    if (WebAnnoCasUtil.isSame(targetNext, aOriginFs)) {
        if (linkedListBehavior) {
            throw new IllegalStateException("Cannot change direction of a link within a chain");
        // BratAjaxCasUtil.setFeature(aTargetFs, aFeature, aValue);
        } else {
        // in set mode there are no arc labels anyway
        }
    } else // if origin and target are not adjacent
    {
        FeatureStructure originChain = getChainForLink(aJCas, aOriginFs);
        FeatureStructure targetChain = getChainForLink(aJCas, aTargetFs);
        AnnotationFS targetPrev = getPrevLink(targetChain, aTargetFs);
        if (!WebAnnoCasUtil.isSame(originChain, targetChain)) {
            if (linkedListBehavior) {
                // the rest becomes its own chain
                if (originNext != null) {
                    newChain(aJCas, originNext);
                // we set originNext below
                // we set the arc label below
                }
                // if targetFs has a prev, then split it off
                if (targetPrev != null) {
                    setNextLink(targetPrev, null);
                } else // if it has no prev then we fully append the target chain to the origin chain
                // and we can remove the target chain head
                {
                    aJCas.removeFsFromIndexes(targetChain);
                }
                // connect the rest of the target chain to the origin chain
                setNextLink(aOriginFs, aTargetFs);
            } else {
                // collect all the links
                List<AnnotationFS> links = new ArrayList<>();
                links.addAll(collectLinks(originChain));
                links.addAll(collectLinks(targetChain));
                // sort them ascending by begin and descending by end (default UIMA order)
                links.sort(new AnnotationComparator());
                // thread them
                AnnotationFS prev = null;
                for (AnnotationFS link : links) {
                    if (prev != null) {
                        // Set next link
                        setNextLink(prev, link);
                    // // Clear arc label - it makes no sense in this mode
                    // setLabel(prev, aFeature, null);
                    }
                    prev = link;
                }
                // make sure the last link terminates the chain
                setNextLink(links.get(links.size() - 1), null);
                // the chain head needs to point to the first link
                setFirstLink(originChain, links.get(0));
                // we don't need the second chain head anymore
                aJCas.removeFsFromIndexes(targetChain);
            }
        } else {
            // if the two links are in the same chain, we just ignore the action
            if (linkedListBehavior) {
                throw new IllegalStateException("Cannot connect two spans that are already part of the same chain");
            }
        }
    }
    // We do not actually create a new FS for the arc. Features are set on the originFS.
    return WebAnnoCasUtil.getAddr(aOriginFs);
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

FeatureStructure (org.apache.uima.cas.FeatureStructure)60 Type (org.apache.uima.cas.Type)38 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)37 ArrayList (java.util.ArrayList)29 JCas (org.apache.uima.jcas.JCas)20 Feature (org.apache.uima.cas.Feature)17 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)16 CAS (org.apache.uima.cas.CAS)16 Test (org.junit.Test)16 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)13 List (java.util.List)12 ArrayFS (org.apache.uima.cas.ArrayFS)8 Arrays.asList (java.util.Arrays.asList)6 LinkedHashMap (java.util.LinkedHashMap)6 CasUtil.getType (org.apache.uima.fit.util.CasUtil.getType)6 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)5 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)4 LogMessage (de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage)4 TsvColumn (de.tudarmstadt.ukp.clarin.webanno.tsv.internal.tsv3x.model.TsvColumn)4 Map (java.util.Map)4