Search in sources :

Example 1 with Offsets

use of de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Offsets in project webanno by webanno.

the class BratAnnotationEditor method actionSpan.

private SpanAnnotationResponse actionSpan(AjaxRequestTarget aTarget, IRequestParameters request, JCas jCas, VID paramId) throws IOException, AnnotationException {
    Offsets offsets = getOffsetsFromRequest(request, jCas, paramId);
    AnnotatorState state = getModelObject();
    Selection selection = state.getSelection();
    if (state.isSlotArmed()) {
        // When filling a slot, the current selection is *NOT* changed. The
        // Span annotation which owns the slot that is being filled remains
        // selected!
        getActionHandler().actionFillSlot(aTarget, jCas, offsets.getBegin(), offsets.getEnd(), paramId);
    } else {
        if (!paramId.isSynthetic()) {
            selection.selectSpan(paramId, jCas, offsets.getBegin(), offsets.getEnd());
            if (selection.getAnnotation().isNotSet()) {
                // Create new annotation
                getActionHandler().actionCreateOrUpdate(aTarget, jCas);
            } else {
                getActionHandler().actionSelect(aTarget, jCas);
            }
        }
    }
    return new SpanAnnotationResponse();
}
Also used : SpanAnnotationResponse(de.tudarmstadt.ukp.clarin.webanno.brat.message.SpanAnnotationResponse) Selection(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) Offsets(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Offsets)

Example 2 with Offsets

use of de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Offsets in project webanno by webanno.

the class BratAnnotationEditor method getOffsetsFromRequest.

/**
 * Extract offset information from the current request. These are either offsets of an existing
 * selected annotations or offsets contained in the request for the creation of a new
 * annotation.
 */
private Offsets getOffsetsFromRequest(IRequestParameters request, JCas jCas, VID aVid) throws IOException {
    if (aVid.isNotSet() || aVid.isSynthetic()) {
        // Create new span annotation - in this case we get the offset information from the
        // request
        String offsets = request.getParameterValue(PARAM_OFFSETS).toString();
        OffsetsList offsetLists = JSONUtil.getJsonConverter().getObjectMapper().readValue(offsets, OffsetsList.class);
        int annotationBegin = getModelObject().getWindowBeginOffset() + offsetLists.get(0).getBegin();
        int annotationEnd = getModelObject().getWindowBeginOffset() + offsetLists.get(offsetLists.size() - 1).getEnd();
        return new Offsets(annotationBegin, annotationEnd);
    } else {
        // Edit existing span annotation - in this case we look up the offsets in the CAS
        // Let's not trust the client in this case.
        AnnotationFS fs = WebAnnoCasUtil.selectByAddr(jCas, aVid.getId());
        return new Offsets(fs.getBegin(), fs.getEnd());
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Offsets(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Offsets) OffsetsList(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.OffsetsList)

Example 3 with Offsets

use of de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Offsets in project webanno by webanno.

the class EntityTest method toJsonTest.

@Test
public void toJsonTest() throws IOException {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    String json = JSONUtil.toPrettyJsonString(jsonConverter, new Entity(new VID(1, 2), "type", new Offsets(1, 2), "label", "color", "somehoverspantext"));
    assertEquals("[ \"1.2\", \"type\", [ [ 1, 2 ] ], \"label\", \"color\", \"somehoverspantext\" ]", json);
}
Also used : VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) Entity(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Entity) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Offsets(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Offsets) Test(org.junit.Test)

Example 4 with Offsets

use of de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Offsets in project webanno by webanno.

the class BratRenderer method render.

/**
 * wrap JSON responses to BRAT visualizer
 *
 * @param aResponse
 *            the response.
 * @param aState
 *            the annotator model.
 * @param aJCas
 *            the JCas.
 * @param aAnnotationService
 *            the annotation service.s
 */
public static void render(GetDocumentResponse aResponse, AnnotatorState aState, VDocument aVDoc, JCas aJCas, AnnotationSchemaService aAnnotationService, ColoringStrategy aColoringStrategy) {
    aResponse.setRtlMode(ScriptDirection.RTL.equals(aState.getScriptDirection()));
    aResponse.setFontZoom(aState.getPreferences().getFontZoom());
    // Render invisible baseline annotations (sentence, tokens)
    renderTokenAndSentence(aJCas, aResponse, aState);
    // Render visible (custom) layers
    Map<String[], Queue<String>> colorQueues = new HashMap<>();
    for (AnnotationLayer layer : aVDoc.getAnnotationLayers()) {
        ColoringStrategy coloringStrategy = aColoringStrategy != null ? aColoringStrategy : ColoringStrategy.getStrategy(aAnnotationService, layer, aState.getPreferences(), colorQueues);
        TypeAdapter typeAdapter = aAnnotationService.getAdapter(layer);
        for (VSpan vspan : aVDoc.spans(layer.getId())) {
            List<Offsets> offsets = toOffsets(vspan.getRanges());
            String bratLabelText = TypeUtil.getUiLabelText(typeAdapter, vspan.getFeatures());
            String bratHoverText = TypeUtil.getUiHoverText(typeAdapter, vspan.getHoverFeatures());
            String color;
            if (vspan.getColorHint() == null) {
                color = getColor(vspan, coloringStrategy, bratLabelText);
            } else {
                color = vspan.getColorHint();
            }
            aResponse.addEntity(new Entity(vspan.getVid(), vspan.getType(), offsets, bratLabelText, color, bratHoverText));
        }
        for (VArc varc : aVDoc.arcs(layer.getId())) {
            String bratLabelText;
            if (varc.getLabelHint() == null) {
                bratLabelText = TypeUtil.getUiLabelText(typeAdapter, varc.getFeatures());
            } else {
                bratLabelText = varc.getLabelHint();
            }
            String color;
            if (varc.getColorHint() == null) {
                color = getColor(varc, coloringStrategy, bratLabelText);
            } else {
                color = varc.getColorHint();
            }
            aResponse.addRelation(new Relation(varc.getVid(), varc.getType(), getArgument(varc.getSource(), varc.getTarget()), bratLabelText, color));
        }
    }
    List<Sentence> sentences = new ArrayList<>(select(aJCas, Sentence.class));
    for (VComment vcomment : aVDoc.comments()) {
        String type;
        switch(vcomment.getCommentType()) {
            case ERROR:
                type = AnnotationComment.ANNOTATION_ERROR;
                break;
            case INFO:
                type = AnnotationComment.ANNOTATOR_NOTES;
                break;
            case YIELD:
                type = "Yield";
                break;
            default:
                type = AnnotationComment.ANNOTATOR_NOTES;
                break;
        }
        AnnotationFS fs;
        if (!vcomment.getVid().isSynthetic() && ((fs = selectByAddr(aJCas, vcomment.getVid().getId())) instanceof Sentence)) {
            int index = sentences.indexOf(fs) + 1;
            aResponse.addComment(new SentenceComment(index, type, vcomment.getComment()));
        } else {
            aResponse.addComment(new AnnotationComment(vcomment.getVid(), type, vcomment.getComment()));
        }
    }
    // Render markers
    for (VMarker vmarker : aVDoc.getMarkers()) {
        if (vmarker instanceof VAnnotationMarker) {
            VAnnotationMarker marker = (VAnnotationMarker) vmarker;
            aResponse.addMarker(new AnnotationMarker(vmarker.getType(), marker.getVid()));
        } else if (vmarker instanceof VSentenceMarker) {
            VSentenceMarker marker = (VSentenceMarker) vmarker;
            aResponse.addMarker(new SentenceMarker(vmarker.getType(), marker.getIndex()));
        } else if (vmarker instanceof VTextMarker) {
            VTextMarker marker = (VTextMarker) vmarker;
            aResponse.addMarker(new TextMarker(marker.getType(), marker.getBegin(), marker.getEnd()));
        } else {
            LOG.warn("Unknown how to render marker: [" + vmarker + "]");
        }
    }
}
Also used : Entity(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Entity) VAnnotationMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VAnnotationMarker) VTextMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VTextMarker) HashMap(java.util.HashMap) AnnotationMarker(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.AnnotationMarker) VAnnotationMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VAnnotationMarker) ArrayList(java.util.ArrayList) ColoringStrategy(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) Offsets(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Offsets) TextMarker(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.TextMarker) VTextMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VTextMarker) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) VSentenceMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSentenceMarker) SentenceMarker(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.SentenceMarker) Relation(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Relation) Queue(java.util.Queue) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) VSpan(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSpan) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) AnnotationComment(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.AnnotationComment) VSentenceMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSentenceMarker) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) SentenceComment(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.SentenceComment) VMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VMarker)

Aggregations

Offsets (de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Offsets)4 Entity (de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Entity)2 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)2 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)1 ColoringStrategy (de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy)1 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)1 Selection (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection)1 VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)1 VAnnotationMarker (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VAnnotationMarker)1 VArc (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc)1 VComment (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment)1 VMarker (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VMarker)1 VSentenceMarker (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSentenceMarker)1 VSpan (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSpan)1 VTextMarker (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VTextMarker)1 SpanAnnotationResponse (de.tudarmstadt.ukp.clarin.webanno.brat.message.SpanAnnotationResponse)1 AnnotationComment (de.tudarmstadt.ukp.clarin.webanno.brat.render.model.AnnotationComment)1 AnnotationMarker (de.tudarmstadt.ukp.clarin.webanno.brat.render.model.AnnotationMarker)1 OffsetsList (de.tudarmstadt.ukp.clarin.webanno.brat.render.model.OffsetsList)1 Relation (de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Relation)1