use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter in project webanno by webanno.
the class AnnotationDetailEditorPanel method getAttachedSpans.
private Set<AnnotationFS> getAttachedSpans(AnnotationFS aFs, AnnotationLayer aLayer) {
CAS cas = aFs.getCAS();
Set<AnnotationFS> attachedSpans = new HashSet<>();
TypeAdapter adapter = annotationService.getAdapter(aLayer);
if (adapter instanceof SpanAdapter && aLayer.getAttachType() != null) {
Type spanType = CasUtil.getType(cas, aLayer.getAttachType().getName());
Feature attachFeature = spanType.getFeatureByBaseName(aLayer.getAttachFeature().getName());
for (AnnotationFS attachedFs : selectAt(cas, spanType, aFs.getBegin(), aFs.getEnd())) {
if (isSame(attachedFs.getFeatureValue(attachFeature), aFs)) {
attachedSpans.add(attachedFs);
}
}
}
return attachedSpans;
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter in project webanno by webanno.
the class AnnotationDetailEditorPanel method actionFillSlot.
@Override
public void actionFillSlot(AjaxRequestTarget aTarget, JCas aJCas, int aBegin, int aEnd, VID aVID) throws AnnotationException, IOException {
assert aJCas != null;
AnnotatorState state = getModelObject();
// If this method is called when no slot is armed, it must be a bug!
if (!state.isSlotArmed()) {
throw new IllegalStateException("No slot is armed.");
}
// Fill slot with new annotation (only works if a concrete type is set for the link feature!
int id;
if (aVID.isNotSet()) {
if (!CAS.TYPE_NAME_ANNOTATION.equals(state.getArmedFeature().getType())) {
SpanAdapter adapter = (SpanAdapter) annotationService.getAdapter(annotationService.getLayer(state.getArmedFeature().getType(), state.getProject()));
id = adapter.add(state, aJCas, aBegin, aEnd);
} else {
throw new AnnotationException("Unable to create annotation of type [" + CAS.TYPE_NAME_ANNOTATION + "]. Please click an annotation in stead of selecting new text.");
}
} else {
id = aVID.getId();
}
// Fill the annotation into the slow
try {
setSlot(aTarget, aJCas, id);
} catch (Exception e) {
handleException(this, aTarget, e);
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter in project webanno by webanno.
the class AnnotationDetailEditorPanel method createNewSpanAnnotation.
private void createNewSpanAnnotation(AjaxRequestTarget aTarget, SpanAdapter aAdapter, JCas aJCas) throws IOException, AnnotationException {
LOG.trace("createNewSpanAnnotation()");
AnnotatorState state = getModelObject();
Selection selection = state.getSelection();
List<FeatureState> featureStates = state.getFeatureStates();
if ((aAdapter.isAllowMultipleToken() || aAdapter.isLockToTokenOffsets()) && selection.getBegin() == selection.getEnd()) {
throw new AnnotationException("Cannot create zero-width annotation on layers that lock to token boundaries.");
}
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);
} else {
actionClear(aTarget);
throw new AnnotationException("Cannot create another annotation of layer [" + state.getSelectedAnnotationLayer().getUiName() + "] at this" + " location - stacking is not enabled for this layer.");
}
}
}
int annoId = aAdapter.add(state, aJCas, selection.getBegin(), selection.getEnd());
AnnotationFS annoFs = WebAnnoCasUtil.selectByAddr(aJCas, annoId);
selection.selectSpan(new VID(annoId), aJCas, annoFs.getBegin(), annoFs.getEnd());
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter in project webanno by webanno.
the class AutomationUtil method addOtherFeatureFromAnnotation.
/**
* If the training file or the test file already contain the "Other layer" annotations, get the
* UIMA annotation and add it as a feature - no need to train and predict for this "other layer"
*/
private static void addOtherFeatureFromAnnotation(AnnotationFeature aFeature, DocumentService aRepository, AutomationService aAutomationServic, AnnotationSchemaService aAnnotationService, UserDao aUserDao, List<List<String>> aPredictions, SourceDocument aSourceDocument) throws UIMAException, ClassNotFoundException, IOException {
AutomationTypeAdapter adapter = (AutomationTypeAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
List<String> annotations = new ArrayList<>();
// this is training - all training documents will be converted to a single training file
if (aSourceDocument == null) {
for (TrainingDocument trainingDocument : aAutomationServic.listTrainingDocuments(aFeature.getProject())) {
JCas jCas = aAutomationServic.readTrainingAnnotationCas(trainingDocument);
for (Sentence sentence : select(jCas, Sentence.class)) {
if (aFeature.getLayer().isMultipleTokens()) {
annotations.addAll(((SpanAdapter) adapter).getMultipleAnnotation(sentence, aFeature).values());
} else {
annotations.addAll(adapter.getAnnotation(sentence, aFeature));
}
}
}
aPredictions.add(annotations);
} else // This is SourceDocument to predict (in the suggestion pane)
{
User user = aUserDao.getCurrentUser();
AnnotationDocument annodoc = aRepository.createOrGetAnnotationDocument(aSourceDocument, user);
JCas jCas = aRepository.readAnnotationCas(annodoc);
for (Sentence sentence : select(jCas, Sentence.class)) {
if (aFeature.getLayer().isMultipleTokens()) {
annotations.addAll(((SpanAdapter) adapter).getMultipleAnnotation(sentence, aFeature).values());
} else {
annotations.addAll(adapter.getAnnotation(sentence, aFeature));
}
}
aPredictions.add(annotations);
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter in project webanno by webanno.
the class SpanRenderer method render.
@Override
public void render(JCas aJcas, List<AnnotationFeature> aFeatures, VDocument aResponse, AnnotatorState aBratAnnotatorModel) {
List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
SpanAdapter typeAdapter = getTypeAdapter();
Type type = getType(aJcas.getCas(), typeAdapter.getAnnotationTypeName());
int windowBegin = aBratAnnotatorModel.getWindowBeginOffset();
int windowEnd = aBratAnnotatorModel.getWindowEndOffset();
List<Sentence> visibleSentences = selectCovered(aJcas, Sentence.class, windowBegin, windowEnd);
for (AnnotationFS fs : selectCovered(aJcas.getCas(), type, windowBegin, windowEnd)) {
String bratTypeName = TypeUtil.getUiTypeName(typeAdapter);
Map<String, String> features = getFeatures(typeAdapter, fs, visibleFeatures);
Map<String, String> hoverFeatures = getHoverFeatures(typeAdapter, fs, aFeatures);
Sentence beginSent = null;
Sentence endSent = null;
// the visible window
for (Sentence sent : visibleSentences) {
if (beginSent == null) {
// offset of the current annotation.
if (sent.getBegin() <= fs.getBegin() && fs.getBegin() <= sent.getEnd()) {
beginSent = sent;
}
// second sentence.
if (fs.getBegin() == fs.getEnd()) {
endSent = sent;
}
}
if (endSent == null) {
if (sent.getBegin() <= fs.getEnd() && fs.getEnd() <= sent.getEnd()) {
endSent = sent;
}
}
if (beginSent != null && endSent != null) {
break;
}
}
if (beginSent == null || endSent == null) {
throw new IllegalStateException("Unable to determine sentences in which the annotation starts/ends: " + fs);
}
List<Sentence> sentences = selectCovered(aJcas, Sentence.class, beginSent.getBegin(), endSent.getEnd());
List<VRange> ranges = new ArrayList<>();
if (sentences.size() > 1) {
for (Sentence sentence : sentences) {
if (sentence.getBegin() <= fs.getBegin() && fs.getBegin() < sentence.getEnd()) {
ranges.add(new VRange(fs.getBegin() - windowBegin, sentence.getEnd() - windowBegin));
} else if (sentence.getBegin() <= fs.getEnd() && fs.getEnd() <= sentence.getEnd()) {
ranges.add(new VRange(sentence.getBegin() - windowBegin, fs.getEnd() - windowBegin));
} else {
ranges.add(new VRange(sentence.getBegin() - windowBegin, sentence.getEnd() - windowBegin));
}
}
aResponse.add(new VSpan(typeAdapter.getLayer(), fs, bratTypeName, ranges, features, hoverFeatures));
} else {
// FIXME It should be possible to remove this case and the if clause because
// the case that a FS is inside a single sentence is just a special case
aResponse.add(new VSpan(typeAdapter.getLayer(), fs, bratTypeName, new VRange(fs.getBegin() - windowBegin, fs.getEnd() - windowBegin), features, hoverFeatures));
}
// Render errors if required features are missing
renderRequiredFeatureErrors(visibleFeatures, fs, aResponse);
// Render slots
int fi = 0;
for (AnnotationFeature feat : typeAdapter.listFeatures()) {
if (MultiValueMode.ARRAY.equals(feat.getMultiValueMode()) && LinkMode.WITH_ROLE.equals(feat.getLinkMode())) {
List<LinkWithRoleModel> links = typeAdapter.getFeatureValue(feat, fs);
for (int li = 0; li < links.size(); li++) {
LinkWithRoleModel link = links.get(li);
FeatureStructure targetFS = selectByAddr(fs.getCAS(), link.targetAddr);
aResponse.add(new VArc(typeAdapter.getLayer(), new VID(fs, fi, li), bratTypeName, fs, targetFS, link.role, features));
}
}
fi++;
}
}
}
Aggregations