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();
}
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());
}
}
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);
}
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 + "]");
}
}
}
Aggregations