use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.coloring.ColoringRulesTrait 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 + "]");
}
}
}
Aggregations