Search in sources :

Example 1 with SpanAdapter

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

the class AnnotationDetailEditorPanel method getAttachedSpans.

private Set<AnnotationFS> getAttachedSpans(AnnotationFS aFs, AnnotationLayer aLayer) {
    CAS cas = aFs.getCAS();
    Set<AnnotationFS> attachedSpans = new HashSet<>();
    TypeAdapter adapter = annotationService.getAdapter(aLayer);
    if (adapter instanceof SpanAdapter && aLayer.getAttachType() != null) {
        Type spanType = CasUtil.getType(cas, aLayer.getAttachType().getName());
        Feature attachFeature = spanType.getFeatureByBaseName(aLayer.getAttachFeature().getName());
        for (AnnotationFS attachedFs : selectAt(cas, spanType, aFs.getBegin(), aFs.getEnd())) {
            if (isSame(attachedFs.getFeatureValue(attachFeature), aFs)) {
                attachedSpans.add(attachedFs);
            }
        }
    }
    return attachedSpans;
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CAS(org.apache.uima.cas.CAS) TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) Feature(org.apache.uima.cas.Feature) WebAnnoCasUtil.setFeature(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.setFeature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) HashSet(java.util.HashSet)

Example 2 with SpanAdapter

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

the class AnnotationDetailEditorPanel method actionFillSlot.

@Override
public void actionFillSlot(AjaxRequestTarget aTarget, JCas aJCas, int aBegin, int aEnd, VID aVID) throws AnnotationException, IOException {
    assert aJCas != null;
    AnnotatorState state = getModelObject();
    // If this method is called when no slot is armed, it must be a bug!
    if (!state.isSlotArmed()) {
        throw new IllegalStateException("No slot is armed.");
    }
    // Fill slot with new annotation (only works if a concrete type is set for the link feature!
    int id;
    if (aVID.isNotSet()) {
        if (!CAS.TYPE_NAME_ANNOTATION.equals(state.getArmedFeature().getType())) {
            SpanAdapter adapter = (SpanAdapter) annotationService.getAdapter(annotationService.getLayer(state.getArmedFeature().getType(), state.getProject()));
            id = adapter.add(state, aJCas, aBegin, aEnd);
        } else {
            throw new AnnotationException("Unable to create annotation of type [" + CAS.TYPE_NAME_ANNOTATION + "]. Please click an annotation in stead of selecting new text.");
        }
    } else {
        id = aVID.getId();
    }
    // Fill the annotation into the slow
    try {
        setSlot(aTarget, aJCas, id);
    } catch (Exception e) {
        handleException(this, aTarget, e);
    }
}
Also used : AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) NoResultException(javax.persistence.NoResultException) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Example 3 with SpanAdapter

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

the class AnnotationDetailEditorPanel method createNewSpanAnnotation.

private void createNewSpanAnnotation(AjaxRequestTarget aTarget, SpanAdapter aAdapter, JCas aJCas) throws IOException, AnnotationException {
    LOG.trace("createNewSpanAnnotation()");
    AnnotatorState state = getModelObject();
    Selection selection = state.getSelection();
    List<FeatureState> featureStates = state.getFeatureStates();
    if ((aAdapter.isAllowMultipleToken() || aAdapter.isLockToTokenOffsets()) && selection.getBegin() == selection.getEnd()) {
        throw new AnnotationException("Cannot create zero-width annotation on layers that lock to token boundaries.");
    }
    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);
            } else {
                actionClear(aTarget);
                throw new AnnotationException("Cannot create another annotation of layer [" + state.getSelectedAnnotationLayer().getUiName() + "] at this" + " location - stacking is not enabled for this layer.");
            }
        }
    }
    int annoId = aAdapter.add(state, aJCas, selection.getBegin(), selection.getEnd());
    AnnotationFS annoFs = WebAnnoCasUtil.selectByAddr(aJCas, annoId);
    selection.selectSpan(new VID(annoId), aJCas, annoFs.getBegin(), annoFs.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) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Serializable(java.io.Serializable) Selection(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) FeatureState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState)

Example 4 with SpanAdapter

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

the class AutomationUtil method addOtherFeatureFromAnnotation.

/**
 * If the training file or the test file already contain the "Other layer" annotations, get the
 * UIMA annotation and add it as a feature - no need to train and predict for this "other layer"
 */
private static void addOtherFeatureFromAnnotation(AnnotationFeature aFeature, DocumentService aRepository, AutomationService aAutomationServic, AnnotationSchemaService aAnnotationService, UserDao aUserDao, List<List<String>> aPredictions, SourceDocument aSourceDocument) throws UIMAException, ClassNotFoundException, IOException {
    AutomationTypeAdapter adapter = (AutomationTypeAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
    List<String> annotations = new ArrayList<>();
    // this is training - all training documents will be converted to a single training file
    if (aSourceDocument == null) {
        for (TrainingDocument trainingDocument : aAutomationServic.listTrainingDocuments(aFeature.getProject())) {
            JCas jCas = aAutomationServic.readTrainingAnnotationCas(trainingDocument);
            for (Sentence sentence : select(jCas, Sentence.class)) {
                if (aFeature.getLayer().isMultipleTokens()) {
                    annotations.addAll(((SpanAdapter) adapter).getMultipleAnnotation(sentence, aFeature).values());
                } else {
                    annotations.addAll(adapter.getAnnotation(sentence, aFeature));
                }
            }
        }
        aPredictions.add(annotations);
    } else // This is SourceDocument to predict (in the suggestion pane)
    {
        User user = aUserDao.getCurrentUser();
        AnnotationDocument annodoc = aRepository.createOrGetAnnotationDocument(aSourceDocument, user);
        JCas jCas = aRepository.readAnnotationCas(annodoc);
        for (Sentence sentence : select(jCas, Sentence.class)) {
            if (aFeature.getLayer().isMultipleTokens()) {
                annotations.addAll(((SpanAdapter) adapter).getMultipleAnnotation(sentence, aFeature).values());
            } else {
                annotations.addAll(adapter.getAnnotation(sentence, aFeature));
            }
        }
        aPredictions.add(annotations);
    }
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) ArrayList(java.util.ArrayList) JCas(org.apache.uima.jcas.JCas) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) AutomationTypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.AutomationTypeAdapter) TrainingDocument(de.tudarmstadt.ukp.clarin.webanno.model.TrainingDocument)

Example 5 with SpanAdapter

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

the class SpanRenderer method render.

@Override
public void render(JCas aJcas, List<AnnotationFeature> aFeatures, VDocument aResponse, AnnotatorState aBratAnnotatorModel) {
    List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
    SpanAdapter typeAdapter = getTypeAdapter();
    Type type = getType(aJcas.getCas(), typeAdapter.getAnnotationTypeName());
    int windowBegin = aBratAnnotatorModel.getWindowBeginOffset();
    int windowEnd = aBratAnnotatorModel.getWindowEndOffset();
    List<Sentence> visibleSentences = selectCovered(aJcas, Sentence.class, windowBegin, windowEnd);
    for (AnnotationFS fs : selectCovered(aJcas.getCas(), type, windowBegin, windowEnd)) {
        String bratTypeName = TypeUtil.getUiTypeName(typeAdapter);
        Map<String, String> features = getFeatures(typeAdapter, fs, visibleFeatures);
        Map<String, String> hoverFeatures = getHoverFeatures(typeAdapter, fs, aFeatures);
        Sentence beginSent = null;
        Sentence endSent = null;
        // the visible window
        for (Sentence sent : visibleSentences) {
            if (beginSent == null) {
                // offset of the current annotation.
                if (sent.getBegin() <= fs.getBegin() && fs.getBegin() <= sent.getEnd()) {
                    beginSent = sent;
                }
                // second sentence.
                if (fs.getBegin() == fs.getEnd()) {
                    endSent = sent;
                }
            }
            if (endSent == null) {
                if (sent.getBegin() <= fs.getEnd() && fs.getEnd() <= sent.getEnd()) {
                    endSent = sent;
                }
            }
            if (beginSent != null && endSent != null) {
                break;
            }
        }
        if (beginSent == null || endSent == null) {
            throw new IllegalStateException("Unable to determine sentences in which the annotation starts/ends: " + fs);
        }
        List<Sentence> sentences = selectCovered(aJcas, Sentence.class, beginSent.getBegin(), endSent.getEnd());
        List<VRange> ranges = new ArrayList<>();
        if (sentences.size() > 1) {
            for (Sentence sentence : sentences) {
                if (sentence.getBegin() <= fs.getBegin() && fs.getBegin() < sentence.getEnd()) {
                    ranges.add(new VRange(fs.getBegin() - windowBegin, sentence.getEnd() - windowBegin));
                } else if (sentence.getBegin() <= fs.getEnd() && fs.getEnd() <= sentence.getEnd()) {
                    ranges.add(new VRange(sentence.getBegin() - windowBegin, fs.getEnd() - windowBegin));
                } else {
                    ranges.add(new VRange(sentence.getBegin() - windowBegin, sentence.getEnd() - windowBegin));
                }
            }
            aResponse.add(new VSpan(typeAdapter.getLayer(), fs, bratTypeName, ranges, features, hoverFeatures));
        } else {
            // FIXME It should be possible to remove this case and the if clause because
            // the case that a FS is inside a single sentence is just a special case
            aResponse.add(new VSpan(typeAdapter.getLayer(), fs, bratTypeName, new VRange(fs.getBegin() - windowBegin, fs.getEnd() - windowBegin), features, hoverFeatures));
        }
        // Render errors if required features are missing
        renderRequiredFeatureErrors(visibleFeatures, fs, aResponse);
        // Render slots
        int fi = 0;
        for (AnnotationFeature feat : typeAdapter.listFeatures()) {
            if (MultiValueMode.ARRAY.equals(feat.getMultiValueMode()) && LinkMode.WITH_ROLE.equals(feat.getLinkMode())) {
                List<LinkWithRoleModel> links = typeAdapter.getFeatureValue(feat, fs);
                for (int li = 0; li < links.size(); li++) {
                    LinkWithRoleModel link = links.get(li);
                    FeatureStructure targetFS = selectByAddr(fs.getCAS(), link.targetAddr);
                    aResponse.add(new VArc(typeAdapter.getLayer(), new VID(fs, fi, li), bratTypeName, fs, targetFS, link.role, features));
                }
            }
            fi++;
        }
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) WebAnnoCasUtil.selectByAddr(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectByAddr) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) LinkMode(de.tudarmstadt.ukp.clarin.webanno.model.LinkMode) ArrayList(java.util.ArrayList) Type(org.apache.uima.cas.Type) VRange(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VRange) Map(java.util.Map) FeatureStructure(org.apache.uima.cas.FeatureStructure) JCas(org.apache.uima.jcas.JCas) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) MultiValueMode(de.tudarmstadt.ukp.clarin.webanno.model.MultiValueMode) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) Collectors(java.util.stream.Collectors) VSpan(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSpan) CasUtil.selectCovered(org.apache.uima.fit.util.CasUtil.selectCovered) List(java.util.List) TypeUtil(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) JCasUtil.selectCovered(org.apache.uima.fit.util.JCasUtil.selectCovered) 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) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) VSpan(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSpan) ArrayList(java.util.ArrayList) 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) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) VRange(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VRange) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Aggregations

SpanAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter)11 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)6 Type (org.apache.uima.cas.Type)6 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)6 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)5 JCas (org.apache.uima.jcas.JCas)5 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)4 LinkWithRoleModel (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel)4 WebAnnoCasUtil.setFeature (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.setFeature)4 Feature (org.apache.uima.cas.Feature)4 ArcAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 CAS (org.apache.uima.cas.CAS)3 ChainAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter)2 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)2 VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)2 TypeUtil (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil)2 WebAnnoCasUtil.selectByAddr (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectByAddr)2 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)2