use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter in project webanno by webanno.
the class AnnotationDetailEditorPanel method createNewChainElement.
private void createNewChainElement(ChainAdapter aAdapter, JCas aJCas) throws AnnotationException {
LOG.trace("createNewChainElement()");
AnnotatorState state = getModelObject();
Selection selection = state.getSelection();
List<FeatureState> featureStates = state.getFeatureStates();
for (FeatureState featureState : featureStates) {
Serializable spanValue = aAdapter.getSpan(aJCas, selection.getBegin(), selection.getEnd(), featureState.feature, null);
if (spanValue != null) {
// allow modification for forward annotation
if (state.isForwardAnnotation()) {
featureState.value = spanValue;
featureStates.get(0).value = spanValue;
String selectedTag = annotationFeatureForm.getBindTags().entrySet().stream().filter(e -> e.getValue().equals(spanValue)).map(Map.Entry::getKey).findFirst().orElse(null);
annotationFeatureForm.setSelectedTag(selectedTag);
}
}
}
selection.setAnnotation(new VID(aAdapter.addSpan(aJCas, selection.getBegin(), selection.getEnd())));
selection.setText(aJCas.getDocumentText().substring(selection.getBegin(), selection.getEnd()));
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter in project webanno by webanno.
the class ChainRenderer method render.
@Override
public void render(JCas aJcas, List<AnnotationFeature> aFeatures, VDocument aResponse, AnnotatorState aState) {
List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
// Find the features for the arc and span labels - it is possible that we do not find a
// feature for arc/span labels because they may have been disabled.
AnnotationFeature spanLabelFeature = null;
AnnotationFeature arcLabelFeature = null;
for (AnnotationFeature f : visibleFeatures) {
if (WebAnnoConst.COREFERENCE_TYPE_FEATURE.equals(f.getName())) {
spanLabelFeature = f;
}
if (WebAnnoConst.COREFERENCE_RELATION_FEATURE.equals(f.getName())) {
arcLabelFeature = f;
}
}
// At this point arc and span feature labels must have been found! If not, the later code
// will crash.
ChainAdapter typeAdapter = getTypeAdapter();
Type chainType = typeAdapter.getAnnotationType(aJcas.getCas());
Feature chainFirst = chainType.getFeatureByBaseName(typeAdapter.getChainFirstFeatureName());
int colorIndex = 0;
// Iterate over the chains
for (FeatureStructure chainFs : selectFS(aJcas.getCas(), chainType)) {
AnnotationFS linkFs = (AnnotationFS) chainFs.getFeatureValue(chainFirst);
AnnotationFS prevLinkFs = null;
// Iterate over the links of the chain
while (linkFs != null) {
Feature linkNext = linkFs.getType().getFeatureByBaseName(typeAdapter.getLinkNextFeatureName());
AnnotationFS nextLinkFs = (AnnotationFS) linkFs.getFeatureValue(linkNext);
// Is link after window? If yes, we can skip the rest of the chain
if (linkFs.getBegin() >= aState.getWindowEndOffset()) {
// Go to next chain
break;
}
// end within the window
if (!(linkFs.getBegin() >= aState.getWindowBeginOffset()) && (linkFs.getEnd() <= aState.getWindowEndOffset())) {
// prevLinkFs remains null until we enter the window
linkFs = nextLinkFs;
// Go to next link
continue;
}
String bratTypeName = TypeUtil.getUiTypeName(typeAdapter);
// Render span
{
String bratLabelText = TypeUtil.getUiLabelText(typeAdapter, linkFs, (spanLabelFeature != null) ? asList(spanLabelFeature) : emptyList());
String bratHoverText = TypeUtil.getUiHoverText(typeAdapter, linkFs, (spanLabelFeature != null) ? asList(spanLabelFeature) : emptyList());
VRange offsets = new VRange(linkFs.getBegin() - aState.getWindowBeginOffset(), linkFs.getEnd() - aState.getWindowBeginOffset());
aResponse.add(new VSpan(typeAdapter.getLayer(), linkFs, bratTypeName, offsets, colorIndex, singletonMap("label", bratLabelText), singletonMap("label", bratHoverText)));
}
// and last link are within the window ;)
if (prevLinkFs != null) {
String bratLabelText = null;
if (typeAdapter.isLinkedListBehavior() && arcLabelFeature != null) {
// Render arc label
bratLabelText = TypeUtil.getUiLabelText(typeAdapter, prevLinkFs, asList(arcLabelFeature));
} else {
// Render only chain type
bratLabelText = TypeUtil.getUiLabelText(typeAdapter, prevLinkFs, emptyList());
}
aResponse.add(new VArc(typeAdapter.getLayer(), new VID(prevLinkFs, 1, VID.NONE, VID.NONE), bratTypeName, prevLinkFs, linkFs, colorIndex, singletonMap("label", bratLabelText)));
}
// Render errors if required features are missing
renderRequiredFeatureErrors(visibleFeatures, linkFs, aResponse);
// if (BratAjaxCasUtil.isSame(linkFs, nextLinkFs)) {
// log.error("Loop in CAS detected, aborting rendering of chains");
// break;
// }
prevLinkFs = linkFs;
linkFs = nextLinkFs;
}
// The color index is updated even for chains that have no visible links in the current
// window because we would like the chain color to be independent of visibility. In
// particular the color of a chain should not change when switching pages/scrolling.
colorIndex++;
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ChainAdapter in project webanno by webanno.
the class AnnotationDetailEditorPanel method createNewAnnotation.
private void createNewAnnotation(AjaxRequestTarget aTarget, TypeAdapter aAdapter, JCas aJCas) throws AnnotationException, IOException {
AnnotatorState state = getModelObject();
if (state.getSelection().isArc()) {
if (aAdapter instanceof SpanAdapter) {
error("Layer [" + aAdapter.getLayer().getUiName() + "] does not support arc annotation.");
aTarget.addChildren(getPage(), IFeedback.class);
} else if (aAdapter instanceof ArcAdapter) {
createNewRelationAnnotation((ArcAdapter) aAdapter, aJCas);
} else if (aAdapter instanceof ChainAdapter) {
createNewChainLinkAnnotation((ChainAdapter) aAdapter, aJCas);
} else {
throw new IllegalStateException("I don't know how to use [" + aAdapter.getClass().getSimpleName() + "] in this situation.");
}
} else {
if (aAdapter instanceof SpanAdapter) {
createNewSpanAnnotation(aTarget, (SpanAdapter) aAdapter, aJCas);
} else if (aAdapter instanceof ChainAdapter) {
createNewChainElement((ChainAdapter) aAdapter, aJCas);
} else {
throw new IllegalStateException("I don't know how to use [" + aAdapter.getClass().getSimpleName() + "] in this situation.");
}
}
}
Aggregations