use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment in project webanno by webanno.
the class RelationRenderer method render.
@Override
public void render(final JCas aJcas, List<AnnotationFeature> aFeatures, VDocument aResponse, AnnotatorState aBratAnnotatorModel) {
List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
ArcAdapter typeAdapter = getTypeAdapter();
Type type = getType(aJcas.getCas(), typeAdapter.getAnnotationTypeName());
int windowBegin = aBratAnnotatorModel.getWindowBeginOffset();
int windowEnd = aBratAnnotatorModel.getWindowEndOffset();
Feature dependentFeature = type.getFeatureByBaseName(typeAdapter.getTargetFeatureName());
Feature governorFeature = type.getFeatureByBaseName(typeAdapter.getSourceFeatureName());
Type spanType = getType(aJcas.getCas(), typeAdapter.getAttachTypeName());
Feature arcSpanFeature = spanType.getFeatureByBaseName(typeAdapter.getAttachFeatureName());
FeatureStructure dependentFs;
FeatureStructure governorFs;
Map<Integer, Set<Integer>> relationLinks = getRelationLinks(aJcas, windowBegin, windowEnd, type, dependentFeature, governorFeature, arcSpanFeature);
// if this is a governor for more than one dependent, avoid duplicate yield
List<Integer> yieldDeps = new ArrayList<>();
for (AnnotationFS fs : selectCovered(aJcas.getCas(), type, windowBegin, windowEnd)) {
if (typeAdapter.getAttachFeatureName() != null) {
dependentFs = fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
governorFs = fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
} else {
dependentFs = fs.getFeatureValue(dependentFeature);
governorFs = fs.getFeatureValue(governorFeature);
}
String bratTypeName = TypeUtil.getUiTypeName(typeAdapter);
Map<String, String> features = getFeatures(typeAdapter, fs, visibleFeatures);
if (dependentFs == null || governorFs == null) {
RequestCycle requestCycle = RequestCycle.get();
IPageRequestHandler handler = PageRequestHandlerTracker.getLastHandler(requestCycle);
Page page = (Page) handler.getPage();
StringBuilder message = new StringBuilder();
message.append("Relation [" + typeAdapter.getLayer().getName() + "] with id [" + getAddr(fs) + "] has loose ends - cannot render.");
if (typeAdapter.getAttachFeatureName() != null) {
message.append("\nRelation [" + typeAdapter.getLayer().getName() + "] attached to feature [" + typeAdapter.getAttachFeatureName() + "].");
}
message.append("\nDependent: " + dependentFs);
message.append("\nGovernor: " + governorFs);
page.warn(message.toString());
continue;
}
aResponse.add(new VArc(typeAdapter.getLayer(), fs, bratTypeName, governorFs, dependentFs, features));
// Render errors if required features are missing
renderRequiredFeatureErrors(visibleFeatures, fs, aResponse);
if (relationLinks.keySet().contains(getAddr(governorFs)) && !yieldDeps.contains(getAddr(governorFs))) {
yieldDeps.add(getAddr(governorFs));
// sort the annotations (begin, end)
List<Integer> sortedDepFs = new ArrayList<>(relationLinks.get(getAddr(governorFs)));
sortedDepFs.sort(comparingInt(arg0 -> selectByAddr(aJcas, arg0).getBegin()));
String cm = getYieldMessage(aJcas, sortedDepFs);
aResponse.add(new VComment(governorFs, VCommentType.YIELD, cm));
}
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VComment 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