Search in sources :

Example 1 with ColoringRules

use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringRules in project webanno by webanno.

the class BratRenderer method render.

/**
 * Convert the visual representation to the brat representation.
 *
 * @param aResponse
 *            the response.
 * @param aState
 *            the annotator model.
 * @param aCas
 *            the CAS.
 */
public void render(GetDocumentResponse aResponse, AnnotatorState aState, VDocument aVDoc, CAS aCas, ColoringStrategy aColoringStrategy) {
    aResponse.setRtlMode(ScriptDirection.RTL.equals(aState.getScriptDirection()));
    aResponse.setFontZoom(aState.getPreferences().getFontZoom());
    // Render invisible baseline annotations (sentence, tokens)
    renderText(aCas, aResponse, aState);
    // The rows need to be rendered first because we use the row boundaries to split
    // cross-row spans into multiple ranges
    renderUnitsAsRows(aResponse, aState);
    renderTokens(aCas, aResponse, aState);
    Map<AnnotationFS, Integer> sentenceIndexes = null;
    // Render visible (custom) layers
    Map<String[], Queue<String>> colorQueues = new HashMap<>();
    for (AnnotationLayer layer : aState.getAllAnnotationLayers()) {
        ColoringStrategy coloringStrategy = aColoringStrategy != null ? aColoringStrategy : coloringService.getStrategy(layer, aState.getPreferences(), colorQueues);
        // of visible layers.
        if (!aVDoc.getAnnotationLayers().contains(layer)) {
            continue;
        }
        TypeAdapter typeAdapter = schemaService.getAdapter(layer);
        ColoringRules coloringRules = typeAdapter.getTraits(ColoringRulesTrait.class).map(ColoringRulesTrait::getColoringRules).orElse(null);
        for (VSpan vspan : aVDoc.spans(layer.getId())) {
            List<Offsets> offsets = vspan.getRanges().stream().flatMap(range -> split(aResponse.getSentenceOffsets(), aCas.getDocumentText().substring(aState.getWindowBeginOffset(), aState.getWindowEndOffset()), range.getBegin(), range.getEnd()).stream()).collect(toList());
            String labelText = getUiLabelText(typeAdapter, vspan);
            String hoverText = getUiHoverText(typeAdapter, vspan.getHoverFeatures());
            String color = coloringStrategy.getColor(vspan, labelText, coloringRules);
            if (DEBUG) {
                hoverText = vspan.getOffsets() + "\n" + hoverText;
            }
            aResponse.addEntity(new Entity(vspan.getVid(), vspan.getType(), offsets, labelText, color, hoverText));
            vspan.getLazyDetails().stream().map(d -> new Normalization(vspan.getVid(), d.getFeature(), d.getQuery())).forEach(aResponse::addNormalization);
        }
        for (VArc varc : aVDoc.arcs(layer.getId())) {
            String bratLabelText = getUiLabelText(typeAdapter, varc);
            String color = coloringStrategy.getColor(varc, bratLabelText, coloringRules);
            aResponse.addRelation(new Relation(varc.getVid(), varc.getType(), getArgument(varc.getSource(), varc.getTarget()), bratLabelText, color));
            varc.getLazyDetails().stream().map(d -> new Normalization(varc.getVid(), d.getFeature(), d.getQuery())).forEach(aResponse::addNormalization);
        }
    }
    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 = selectAnnotationByAddr(aCas, vcomment.getVid().getId())) != null && fs.getType().getName().equals(Sentence.class.getName()))) {
            // Lazily fetching the sentences because we only need them for the comments
            if (sentenceIndexes == null) {
                sentenceIndexes = new HashMap<>();
                int i = 1;
                for (AnnotationFS s : select(aCas, getType(aCas, Sentence.class))) {
                    sentenceIndexes.put(s, i);
                    i++;
                }
            }
            int index = sentenceIndexes.get(fs);
            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 : TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) AnnotationMarker(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.AnnotationMarker) Argument(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Argument) LoggerFactory(org.slf4j.LoggerFactory) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) LinkMode(de.tudarmstadt.ukp.clarin.webanno.model.LinkMode) VAnnotationMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VAnnotationMarker) Type(org.apache.uima.cas.Type) SentenceComment(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.SentenceComment) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency) EntityType(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.EntityType) TypeUtil.getUiLabelText(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil.getUiLabelText) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) ColoringRules(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringRules) ColoringService(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringService) Relation(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Relation) WebAnnoCasUtil.selectAnnotationByAddr(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectAnnotationByAddr) VMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VMarker) TextMarker(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.TextMarker) POS(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS) Collections.emptyList(java.util.Collections.emptyList) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) Offsets(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Offsets) AnnotationComment(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.AnnotationComment) Set(java.util.Set) TypeUtil.getUiHoverText(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil.getUiHoverText) VSpan(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSpan) List(java.util.List) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) TypeUtil(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil) Comment(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Comment) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) BratAnnotationEditor(de.tudarmstadt.ukp.clarin.webanno.brat.annotation.BratAnnotationEditor) Queue(java.util.Queue) VSentenceMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSentenceMarker) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) SentenceMarker(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.SentenceMarker) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) CAS(org.apache.uima.cas.CAS) HashMap(java.util.HashMap) ColoringStrategy(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy) Entity(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Entity) ArrayList(java.util.ArrayList) ScriptDirection(de.tudarmstadt.ukp.clarin.webanno.model.ScriptDirection) AnnotationSchemaService(de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) GetDocumentResponse(de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse) Unit(de.tudarmstadt.ukp.clarin.webanno.api.annotation.paging.Unit) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) CasUtil.select(org.apache.uima.fit.util.CasUtil.select) VComment(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment) ColoringRulesTrait(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringRulesTrait) Normalization(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Normalization) CasUtil(org.apache.uima.fit.util.CasUtil) CasUtil.selectCovered(org.apache.uima.fit.util.CasUtil.selectCovered) RelationType(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.RelationType) Collectors.toList(java.util.stream.Collectors.toList) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) CHAIN_TYPE(de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CHAIN_TYPE) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) VTextMarker(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VTextMarker) Comparator(java.util.Comparator) 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) 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) Normalization(de.tudarmstadt.ukp.clarin.webanno.brat.render.model.Normalization) ColoringRules(de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringRules) 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

AnnotationSchemaService (de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService)1 CHAIN_TYPE (de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.CHAIN_TYPE)1 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)1 ColoringRules (de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringRules)1 ColoringRulesTrait (de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringRulesTrait)1 ColoringService (de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringService)1 ColoringStrategy (de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringStrategy)1 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)1 VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)1 Unit (de.tudarmstadt.ukp.clarin.webanno.api.annotation.paging.Unit)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 VDocument (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument)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 TypeUtil (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil)1 TypeUtil.getUiHoverText (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil.getUiHoverText)1