Search in sources :

Example 6 with SpanAdapter

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

the class AnnotationDetailEditorPanel method getAttachedLinks.

private Set<AnnotationFS> getAttachedLinks(AnnotationFS aFs, AnnotationLayer aLayer) {
    CAS cas = aFs.getCAS();
    Set<AnnotationFS> attachedLinks = new HashSet<>();
    TypeAdapter adapter = annotationService.getAdapter(aLayer);
    if (adapter instanceof SpanAdapter) {
        for (AnnotationFeature linkFeature : annotationService.listAttachedLinkFeatures(aLayer)) {
            if (MultiValueMode.ARRAY.equals(linkFeature.getMultiValueMode()) && LinkMode.WITH_ROLE.equals(linkFeature.getLinkMode())) {
                // Fetch slot hosts that could link to the current FS and check if any of
                // them actually links to the current FS
                Type linkType = CasUtil.getType(cas, linkFeature.getLayer().getName());
                for (AnnotationFS linkFS : CasUtil.select(cas, linkType)) {
                    List<LinkWithRoleModel> links = adapter.getFeatureValue(linkFeature, linkFS);
                    for (int li = 0; li < links.size(); li++) {
                        LinkWithRoleModel link = links.get(li);
                        AnnotationFS linkTarget = selectByAddr(cas, AnnotationFS.class, link.targetAddr);
                        // our list of attached links.
                        if (isSame(linkTarget, aFs)) {
                            attachedLinks.add(linkFS);
                        }
                    }
                }
            }
        }
    }
    return attachedLinks;
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) 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) HashSet(java.util.HashSet) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 7 with SpanAdapter

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

the class AnnotationDetailEditorPanel method deleteAnnotation.

private void deleteAnnotation(JCas jCas, AnnotatorState state, AnnotationFS fs, AnnotationLayer layer, TypeAdapter adapter) {
    // is no longer set after UNATTACH SPANS!
    if (adapter instanceof SpanAdapter) {
        for (AnnotationFS attachedFs : getAttachedRels(fs, layer)) {
            jCas.getCas().removeFsFromIndexes(attachedFs);
            info("The attached annotation for relation type [" + annotationService.getLayer(attachedFs.getType().getName(), state.getProject()).getUiName() + "] is deleted");
        }
    }
    // to look at span annotations that have the same offsets as the FS to be deleted.
    if (adapter instanceof SpanAdapter && layer.getAttachType() != null && layer.getAttachFeature() != null) {
        Type spanType = CasUtil.getType(jCas.getCas(), layer.getAttachType().getName());
        Feature attachFeature = spanType.getFeatureByBaseName(layer.getAttachFeature().getName());
        for (AnnotationFS attachedFs : getAttachedSpans(fs, layer)) {
            attachedFs.setFeatureValue(attachFeature, null);
            LOG.debug("Unattached [" + attachFeature.getShortName() + "] on annotation [" + getAddr(attachedFs) + "]");
        }
    }
    // to be deleted: the link feature must be the type of the FS or it must be generic.
    if (adapter instanceof SpanAdapter) {
        for (AnnotationFeature linkFeature : annotationService.listAttachedLinkFeatures(layer)) {
            Type linkType = CasUtil.getType(jCas.getCas(), linkFeature.getLayer().getName());
            for (AnnotationFS linkFS : CasUtil.select(jCas.getCas(), linkType)) {
                List<LinkWithRoleModel> links = adapter.getFeatureValue(linkFeature, linkFS);
                Iterator<LinkWithRoleModel> i = links.iterator();
                boolean modified = false;
                while (i.hasNext()) {
                    LinkWithRoleModel link = i.next();
                    if (link.targetAddr == getAddr(fs)) {
                        i.remove();
                        LOG.debug("Cleared slot [" + link.role + "] in feature [" + linkFeature.getName() + "] on annotation [" + getAddr(linkFS) + "]");
                        modified = true;
                    }
                }
                if (modified) {
                    setFeature(linkFS, linkFeature, links);
                }
            }
        }
    }
    // relation.
    if (adapter instanceof ArcAdapter) {
    // Do nothing ;)
    }
    // Actually delete annotation
    adapter.delete(state, jCas, state.getSelection().getAnnotation());
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) ArcAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter) 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) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 8 with SpanAdapter

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter 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)

Example 9 with SpanAdapter

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

the class AnnotationFeatureForm method actionDelete.

private void actionDelete(AjaxRequestTarget aTarget) throws IOException, AnnotationException {
    AnnotatorState state = AnnotationFeatureForm.this.getModelObject();
    AnnotationLayer layer = state.getSelectedAnnotationLayer();
    TypeAdapter adapter = annotationService.getAdapter(layer);
    JCas jCas = editorPanel.getEditorCas();
    AnnotationFS fs = selectByAddr(jCas, state.getSelection().getAnnotation().getId());
    if (layer.isReadonly()) {
        error("Cannot replace an annotation on a read-only layer.");
        aTarget.addChildren(getPage(), IFeedback.class);
        return;
    }
    AttachStatus attachStatus = editorPanel.checkAttachStatus(aTarget, state.getProject(), fs);
    if (attachStatus.readOnlyAttached) {
        error("Cannot delete an annotation to which annotations on read-only layers attach.");
        aTarget.addChildren(getPage(), IFeedback.class);
        return;
    }
    if (adapter instanceof SpanAdapter && attachStatus.attachCount > 0) {
        deleteAnnotationDialog.setContentModel(new StringResourceModel("DeleteDialog.text", this, Model.of(layer)).setParameters(attachStatus.attachCount));
        deleteAnnotationDialog.setConfirmAction((aCallbackTarget) -> {
            editorPanel.actionDelete(aCallbackTarget);
        });
        deleteAnnotationDialog.show(aTarget);
    } else {
        editorPanel.actionDelete(aTarget);
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) AttachStatus(de.tudarmstadt.ukp.clarin.webanno.ui.annotation.detail.AnnotationDetailEditorPanel.AttachStatus) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) JCas(org.apache.uima.jcas.JCas) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) StringResourceModel(org.apache.wicket.model.StringResourceModel)

Example 10 with SpanAdapter

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

the class AutomationUtil method repeateSpanAnnotation.

public static void repeateSpanAnnotation(AnnotatorState aState, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, int aStart, int aEnd, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
    AnnotationDocument annoDoc = aDocumentService.getAnnotationDocument(aState.getDocument(), aState.getUser());
    JCas annoCas = aDocumentService.readAnnotationCas(annoDoc);
    // get selected text, concatenations of tokens
    String selectedText = WebAnnoCasUtil.getSelectedText(annoCas, aStart, aEnd);
    SpanAdapter adapter = (SpanAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
    for (SourceDocument d : aDocumentService.listSourceDocuments(aState.getProject())) {
        loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aState.getUser());
        JCas jCas = aCorrectionDocumentService.readCorrectionCas(d);
        for (Sentence sentence : select(jCas, Sentence.class)) {
            String sentenceText = sentence.getCoveredText().toLowerCase();
            for (int i = -1; (i = sentenceText.indexOf(selectedText.toLowerCase(), i)) != -1; i = i + selectedText.length()) {
                if (selectCovered(jCas, Token.class, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length()).size() > 0) {
                    int addr = adapter.add(aState, jCas, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length() - 1);
                    adapter.setFeatureValue(aState, jCas, addr, aFeature, aValue);
                }
            }
        }
        aCorrectionDocumentService.writeCorrectionCas(jCas, d);
    }
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) 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)

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