Search in sources :

Example 11 with AnnotatorState

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.

the class AnnotationDetailEditorPanel method actionCreateForward.

private void actionCreateForward(AjaxRequestTarget aTarget, JCas aJCas, boolean aIsForwarded) throws IOException, AnnotationException {
    LOG.trace("actionForward(isForwarded: {})", aIsForwarded);
    if (isAnnotationFinished()) {
        throw new AnnotationException("This document is already closed. Please ask your " + "project manager to re-open it via the Monitoring page");
    }
    AnnotatorState state = getModelObject();
    // Re-set the selected layer from the drop-down since it might have changed if we
    // have previously created a relation annotation
    state.setSelectedAnnotationLayer(annotationFeatureForm.getLayerSelector().getModelObject());
    internalCommitAnnotation(aTarget, aJCas);
    if (!aIsForwarded) {
        if (state.getSelection().getEnd() >= state.getWindowEndOffset()) {
            autoScroll(aJCas, true);
        }
        List<FeatureState> featureStates = getModelObject().getFeatureStates();
        if (featureStates.get(0).value != null) {
            LOG.info("BEGIN auto-forward annotation for tagset-based annotation");
            AnnotationFS nextToken = WebAnnoCasUtil.getNextToken(aJCas, state.getSelection().getBegin(), state.getSelection().getEnd());
            if (nextToken != null) {
                if (getModelObject().getWindowEndOffset() > nextToken.getBegin()) {
                    state.getSelection().selectSpan(aJCas, nextToken.getBegin(), nextToken.getEnd());
                    actionCreateForward(aTarget, aJCas, true);
                }
            }
            LOG.info("END auto-forward annotation for tagset-based annotation");
        } else {
            LOG.info("BEGIN auto-forward annotation for free-text annotation");
            // remove the entire annotation.
            if (featureStates.get(0).value == null) {
                TypeAdapter adapter = annotationService.getAdapter(state.getSelectedAnnotationLayer());
                AnnotationFS fs = selectByAddr(aJCas, state.getSelection().getAnnotation().getId());
                deleteAnnotation(aJCas, state, fs, featureStates.get(0).feature.getLayer(), adapter);
            }
            AnnotationFS nextToken = WebAnnoCasUtil.getNextToken(aJCas, state.getSelection().getBegin(), state.getSelection().getEnd());
            if (nextToken != null) {
                if (getModelObject().getWindowEndOffset() > nextToken.getBegin()) {
                    state.getSelection().selectSpan(aJCas, nextToken.getBegin(), nextToken.getEnd());
                    actionCreateForward(aTarget, aJCas, true);
                }
            }
            LOG.info("END auto-forward annotation for free-text annotation");
        }
        LOG.trace("onAutoForward()");
        onAutoForward(aTarget);
    }
    aTarget.add(annotationFeatureForm);
    internalCompleteAnnotation(aTarget, aJCas);
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) FeatureState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState)

Example 12 with AnnotatorState

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.

the class AnnotationDetailEditorPanel method actionCreateOrUpdate.

@Override
public void actionCreateOrUpdate(AjaxRequestTarget aTarget, JCas aJCas) throws IOException, AnnotationException {
    LOG.trace("actionAnnotate");
    if (isAnnotationFinished()) {
        throw new AnnotationException("This document is already closed. Please ask your " + "project manager to re-open it via the Monitoring page");
    }
    AnnotatorState state = getModelObject();
    // switches from the selected span layer to the relation layer that is attached to the span
    if (state.getSelection().isArc()) {
        LOG.trace("actionAnnotate() relation annotation - looking for attached layer");
        // FIXME REC I think this whole section which meddles around with the selected
        // annotation layer should be moved out of there to the place where we originally set
        // the annotation layer...!
        AnnotationFS originFS = selectByAddr(aJCas, state.getSelection().getOrigin());
        AnnotationLayer spanLayer = annotationService.getLayer(state.getProject(), originFS);
        if (state.getPreferences().isRememberLayer() && // i.e. new annotation
        state.getSelection().getAnnotation().isNotSet() && !spanLayer.equals(state.getDefaultAnnotationLayer())) {
            throw new AnnotationException("No relation annotation allowed on layer [" + state.getDefaultAnnotationLayer().getUiName() + "]");
        }
        AnnotationLayer previousLayer = state.getSelectedAnnotationLayer();
        // Chain layers consist of arcs and spans
        if (spanLayer.getType().equals(WebAnnoConst.CHAIN_TYPE)) {
            // one layer both for the span and arc annotation
            state.setSelectedAnnotationLayer(spanLayer);
        } else // Otherwise, look up the possible relation layer(s) in the database.
        {
            for (AnnotationLayer l : annotationService.listAnnotationLayer(state.getProject())) {
                if ((l.getAttachType() != null && l.getAttachType().equals(spanLayer)) || (l.getAttachFeature() != null && l.getAttachFeature().getType().equals(spanLayer.getName()))) {
                    if (state.getAnnotationLayers().contains(l)) {
                        state.setSelectedAnnotationLayer(l);
                    } else {
                        state.setSelectedAnnotationLayer(null);
                    }
                    break;
                }
            }
        }
        state.setDefaultAnnotationLayer(spanLayer);
        // If we switched layers, we need to initialize the feature editors for the new layer
        if (!Objects.equals(previousLayer, state.getSelectedAnnotationLayer())) {
            LOG.trace("Layer changed from {} to {} - need to reload feature editors", previousLayer, state.getSelectedAnnotationLayer());
            loadFeatureEditorModels(aJCas, aTarget);
        }
    } else {
        // Re-set the selected layer from the drop-down since it might have changed if we
        // have previously created a relation annotation
        state.setSelectedAnnotationLayer(annotationFeatureForm.getLayerSelector().getModelObject());
    }
    internalCommitAnnotation(aTarget, aJCas);
    internalCompleteAnnotation(aTarget, aJCas);
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)

Example 13 with AnnotatorState

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.

the class AnnotationDetailEditorPanel method populateTagsBasedOnRules.

/**
 * Adds and sorts tags based on Constraints rules
 */
private void populateTagsBasedOnRules(JCas aJCas, FeatureState aModel) {
    LOG.trace("populateTagsBasedOnRules(feature: " + aModel.feature.getUiName() + ")");
    AnnotatorState state = getModelObject();
    // Add values from rules
    String restrictionFeaturePath;
    switch(aModel.feature.getLinkMode()) {
        case WITH_ROLE:
            restrictionFeaturePath = aModel.feature.getName() + "." + aModel.feature.getLinkTypeRoleFeatureName();
            break;
        case NONE:
            restrictionFeaturePath = aModel.feature.getName();
            break;
        default:
            throw new IllegalArgumentException("Unsupported link mode [" + aModel.feature.getLinkMode() + "] on feature [" + aModel.feature.getName() + "]");
    }
    aModel.indicator.reset();
    // Fetch possible values from the constraint rules
    List<PossibleValue> possibleValues;
    try {
        FeatureStructure featureStructure = selectByAddr(aJCas, state.getSelection().getAnnotation().getId());
        Evaluator evaluator = new ValuesGenerator();
        // Only show indicator if this feature can be affected by Constraint rules!
        aModel.indicator.setAffected(evaluator.isThisAffectedByConstraintRules(featureStructure, restrictionFeaturePath, state.getConstraints()));
        possibleValues = evaluator.generatePossibleValues(featureStructure, restrictionFeaturePath, state.getConstraints());
        LOG.debug("Possible values for [" + featureStructure.getType().getName() + "] [" + restrictionFeaturePath + "]: " + possibleValues);
    } catch (Exception e) {
        error("Unable to evaluate constraints: " + ExceptionUtils.getRootCauseMessage(e));
        LOG.error("Unable to evaluate constraints: " + e.getMessage(), e);
        possibleValues = new ArrayList<>();
    }
    // Fetch actual tagset
    List<Tag> valuesFromTagset = annotationService.listTags(aModel.feature.getTagset());
    // First add tags which are suggested by rules and exist in tagset
    List<Tag> tagset = compareSortAndAdd(possibleValues, valuesFromTagset, aModel.indicator);
    // Then add the remaining tags
    for (Tag remainingTag : valuesFromTagset) {
        if (!tagset.contains(remainingTag)) {
            tagset.add(remainingTag);
        }
    }
    // Record the possible values and the (re-ordered) tagset in the feature state
    aModel.possibleValues = possibleValues;
    aModel.tagset = tagset;
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) ArrayList(java.util.ArrayList) ValuesGenerator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.ValuesGenerator) PossibleValue(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue) Tag(de.tudarmstadt.ukp.clarin.webanno.model.Tag) Evaluator(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.Evaluator) NoResultException(javax.persistence.NoResultException) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Example 14 with AnnotatorState

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.

the class AnnotationFeatureForm method createDeleteButton.

private LambdaAjaxLink createDeleteButton() {
    return new LambdaAjaxLink("delete", this::actionDelete).onConfigure((_this) -> {
        AnnotatorState state = AnnotationFeatureForm.this.getModelObject();
        _this.setVisible(state.getSelection().getAnnotation().isSet());
        // Avoid deleting in read-only layers
        _this.setEnabled(state.getSelectedAnnotationLayer() != null && !state.getSelectedAnnotationLayer().isReadonly());
    });
}
Also used : LambdaAjaxLink(de.tudarmstadt.ukp.clarin.webanno.support.lambda.LambdaAjaxLink) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)

Example 15 with AnnotatorState

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState in project webanno by webanno.

the class AnnotationFeatureForm method isForwardable.

private boolean isForwardable() {
    AnnotatorState state = getModelObject();
    AnnotationLayer selectedLayer = state.getSelectedAnnotationLayer();
    if (isNull(selectedLayer) || isNull(selectedLayer.getId())) {
        return false;
    }
    if (!selectedLayer.getType().equals(WebAnnoConst.SPAN_TYPE)) {
        return false;
    }
    if (!selectedLayer.isLockToTokenOffset()) {
        return false;
    }
    // which are are both enabled and visible).
    if (getEnabledFeatures(selectedLayer).size() != 1) {
        return false;
    }
    // we allow forward annotation only for a feature with a tagset
    if (annotationService.listAnnotationFeature(selectedLayer).get(0).getTagset() != null) {
        // there should be at least one tag in the tagset
        TagSet tagSet = annotationService.listAnnotationFeature(selectedLayer).get(0).getTagset();
        return !annotationService.listTags(tagSet).isEmpty();
    }
    // Or layers with a single visible/enabled free-text feature.
    return true;
}
Also used : TagSet(de.tudarmstadt.ukp.clarin.webanno.model.TagSet) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)

Aggregations

AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)84 JCas (org.apache.uima.jcas.JCas)37 IOException (java.io.IOException)26 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)23 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)22 UIMAException (org.apache.uima.UIMAException)20 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)16 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)16 ArrayList (java.util.ArrayList)16 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)14 List (java.util.List)13 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)12 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)9 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)9 SuggestionBuilder (de.tudarmstadt.ukp.clarin.webanno.ui.curation.component.model.SuggestionBuilder)9 Map (java.util.Map)9 NoResultException (javax.persistence.NoResultException)9 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)9 FeatureState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState)8 Selection (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection)8