Search in sources :

Example 1 with ChainAdapter

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter in project webanno by webanno.

the class AnnotationDetailEditorPanel method createNewChainElement.

private void createNewChainElement(ChainAdapter aAdapter, JCas aJCas) throws AnnotationException {
    LOG.trace("createNewChainElement()");
    AnnotatorState state = getModelObject();
    Selection selection = state.getSelection();
    List<FeatureState> featureStates = state.getFeatureStates();
    for (FeatureState featureState : featureStates) {
        Serializable spanValue = aAdapter.getSpan(aJCas, selection.getBegin(), selection.getEnd(), featureState.feature, null);
        if (spanValue != null) {
            // allow modification for forward annotation
            if (state.isForwardAnnotation()) {
                featureState.value = spanValue;
                featureStates.get(0).value = spanValue;
                String selectedTag = annotationFeatureForm.getBindTags().entrySet().stream().filter(e -> e.getValue().equals(spanValue)).map(Map.Entry::getKey).findFirst().orElse(null);
                annotationFeatureForm.setSelectedTag(selectedTag);
            }
        }
    }
    selection.setAnnotation(new VID(aAdapter.addSpan(aJCas, selection.getBegin(), selection.getEnd())));
    selection.setText(aJCas.getDocumentText().substring(selection.getBegin(), selection.getEnd()));
}
Also used : TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) SpringBean(org.apache.wicket.spring.injection.annot.SpringBean) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) LoggerFactory(org.slf4j.LoggerFactory) NoResultException(javax.persistence.NoResultException) WebAnnoCasUtil(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) LinkMode(de.tudarmstadt.ukp.clarin.webanno.model.LinkMode) StringUtils(org.apache.commons.lang3.StringUtils) Type(org.apache.uima.cas.Type) Selection(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection) AnnotationEditorExtensionRegistry(de.tudarmstadt.ukp.clarin.webanno.api.annotation.AnnotationEditorExtensionRegistry) Map(java.util.Map) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) WebAnnoCasUtil.isSame(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.isSame) FeatureStructure(org.apache.uima.cas.FeatureStructure) IModel(org.apache.wicket.model.IModel) WebAnnoCasUtil.getSentenceNumber(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.getSentenceNumber) UIMAException(org.apache.uima.UIMAException) JCas(org.apache.uima.jcas.JCas) Tag(de.tudarmstadt.ukp.clarin.webanno.model.Tag) MultiValueMode(de.tudarmstadt.ukp.clarin.webanno.model.MultiValueMode) WebAnnoConst(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst) Set(java.util.Set) Component(org.apache.wicket.Component) ValuesGenerator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator) IFeedback(org.apache.wicket.feedback.IFeedback) Serializable(java.io.Serializable) ArcAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter) Objects(java.util.Objects) List(java.util.List) TypeUtil(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) ProjectService(de.tudarmstadt.ukp.clarin.webanno.api.ProjectService) SourceDocumentState(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocumentState) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) WebAnnoCasUtil.selectByAddr(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectByAddr) CAS(org.apache.uima.cas.CAS) Feature(org.apache.uima.cas.Feature) Mode(de.tudarmstadt.ukp.clarin.webanno.model.Mode) PossibleValue(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue) WebAnnoCasUtil.setFeature(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.setFeature) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AnnotationSchemaService(de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) AjaxFormValidatingBehavior(org.apache.wicket.ajax.form.AjaxFormValidatingBehavior) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) WebAnnoCasUtil.getAddr(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.getAddr) DocumentService(de.tudarmstadt.ukp.clarin.webanno.api.DocumentService) FeatureState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) WebAnnoCasUtil.selectAt(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectAt) CurationDocumentService(de.tudarmstadt.ukp.clarin.webanno.curation.storage.CurationDocumentService) AnnotationActionHandler(de.tudarmstadt.ukp.clarin.webanno.api.annotation.action.AnnotationActionHandler) IOException(java.io.IOException) CasUtil(org.apache.uima.fit.util.CasUtil) Panel(org.apache.wicket.markup.html.panel.Panel) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) Evaluator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.Evaluator) ChainAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) RulesIndicator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.RulesIndicator) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) Serializable(java.io.Serializable) Selection(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) FeatureState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState)

Example 2 with ChainAdapter

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter 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 3 with ChainAdapter

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter in project webanno by webanno.

the class AnnotationDetailEditorPanel method createNewAnnotation.

private void createNewAnnotation(AjaxRequestTarget aTarget, TypeAdapter aAdapter, JCas aJCas) throws AnnotationException, IOException {
    AnnotatorState state = getModelObject();
    if (state.getSelection().isArc()) {
        if (aAdapter instanceof SpanAdapter) {
            error("Layer [" + aAdapter.getLayer().getUiName() + "] does not support arc annotation.");
            aTarget.addChildren(getPage(), IFeedback.class);
        } else if (aAdapter instanceof ArcAdapter) {
            createNewRelationAnnotation((ArcAdapter) aAdapter, aJCas);
        } else if (aAdapter instanceof ChainAdapter) {
            createNewChainLinkAnnotation((ChainAdapter) aAdapter, aJCas);
        } else {
            throw new IllegalStateException("I don't know how to use [" + aAdapter.getClass().getSimpleName() + "] in this situation.");
        }
    } else {
        if (aAdapter instanceof SpanAdapter) {
            createNewSpanAnnotation(aTarget, (SpanAdapter) aAdapter, aJCas);
        } else if (aAdapter instanceof ChainAdapter) {
            createNewChainElement((ChainAdapter) aAdapter, aJCas);
        } else {
            throw new IllegalStateException("I don't know how to use [" + aAdapter.getClass().getSimpleName() + "] in this situation.");
        }
    }
}
Also used : ArcAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) ChainAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter)

Aggregations

ChainAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter)3 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)3 WebAnnoConst (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst)2 ArcAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter)2 SpanAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter)2 VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)2 TypeUtil (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil)2 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)2 AnnotationSchemaService (de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService)1 DocumentService (de.tudarmstadt.ukp.clarin.webanno.api.DocumentService)1 ProjectService (de.tudarmstadt.ukp.clarin.webanno.api.ProjectService)1 AnnotationEditorExtensionRegistry (de.tudarmstadt.ukp.clarin.webanno.api.annotation.AnnotationEditorExtensionRegistry)1 AnnotationActionHandler (de.tudarmstadt.ukp.clarin.webanno.api.annotation.action.AnnotationActionHandler)1 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)1 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)1 FeatureSupportRegistry (de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry)1 FeatureState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState)1 LinkWithRoleModel (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel)1 Selection (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection)1 VArc (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc)1