@Test public void thatSpanAnchoringAndStackingBehaviorsWorkInConcert() throws AnnotationException { TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test ."); SpanAdapter sut = new SpanAdapter(featureSupportRegistry, null, neLayer, asList(), behaviors); // First time should work - we annotate the whole word "This" sut.add(document, username, jcas, 0, 4); // Second time not - here we annotate "T" but it should be expanded to "This" assertThatExceptionOfType(AnnotationException.class) .isThrownBy(() -> sut.add(document, username, jcas, 0, 1)) .withMessageContaining("stacking is not enabled"); } }
@Test public void thatSpanStackingBehaviorOnCreateThrowsException() throws AnnotationException { neLayer.setAllowStacking(false); TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test ."); SpanAdapter sut = new SpanAdapter(featureSupportRegistry, null, neLayer, asList(), behaviors); // First time should work sut.add(document, username, jcas, 0, 1); // Second time not assertThatExceptionOfType(AnnotationException.class) .isThrownBy(() -> sut.add(document, username, jcas, 0, 1)) .withMessageContaining("stacking is not enabled"); }
if (selectCovered(jCas, Token.class, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length()).size() > 0) { int addr = getAddr(adapter.add(aState.getDocument(), aState.getUser().getUsername(), jCas, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length() - 1));
if (selectCovered(jCas, Token.class, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length()).size() > 0) { int addr = getAddr(adapter.add(aState.getDocument(), aState.getUser().getUsername(), jCas, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length() - 1));
@Test public void thatSpanStackingBehaviorOnValidateGeneratesErrors() throws AnnotationException { TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test ."); SpanAdapter sut = new SpanAdapter(featureSupportRegistry, null, neLayer, asList(), behaviors); // Add two annotations neLayer.setAllowStacking(true); sut.add(document, username, jcas, 0, 1); sut.add(document, username, jcas, 0, 1); //Validation fails neLayer.setAllowStacking(false); assertThat(sut.validate(jcas)) .extracting(Pair::getLeft) .usingElementComparatorIgnoringFields("source", "message") .containsExactly(LogMessage.error(null, "")); }
@Override public int upsertFeature(AnnotationSchemaService annotationService, SourceDocument aDocument, String aUsername, JCas aJCas, AnnotationLayer layer, AnnotationFeature aFeature, String aValue, int aBegin, int aEnd) throws AnnotationException { // The feature of the predicted label SpanAdapter adapter = (SpanAdapter) annotationService.getAdapter(layer); // Check if there is already an annotation of the target type at the given location Type type = CasUtil.getType(aJCas.getCas(), adapter.getAnnotationTypeName()); AnnotationFS annoFS = selectSingleFsAt(aJCas, type, aBegin, aEnd); int address; if (annoFS != null) { // ... if yes, then we update the feature on the existing annotation address = getAddr(annoFS); } else { // ... if not, then we create a new annotation - this also takes care of attaching to // an annotation if necessary address = getAddr(adapter.add(aDocument, aUsername, aJCas, aBegin, aEnd)); } // Update the feature value adapter.setFeatureValue(aDocument, aUsername, aJCas, address, aFeature, aValue); return address; }
private void createNewSpanAnnotation(AjaxRequestTarget aTarget, SpanAdapter aAdapter, JCas aJCas) throws IOException, AnnotationException { handleForwardAnnotation(aTarget, aAdapter, aJCas); AnnotatorState state = getModelObject(); Selection selection = state.getSelection(); AnnotationFS annoFs = aAdapter.add(state.getDocument(), state.getUser().getUsername(), aJCas, selection.getBegin(), selection.getEnd()); selection.selectSpan(new VID(annoFs), aJCas, annoFs.getBegin(), annoFs.getEnd()); }
@Test public void thatSpanCrossSentenceBehaviorOnCreateThrowsException() { neLayer.setCrossSentence(false); TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test .\nThis is sentence two ."); SpanAdapter sut = new SpanAdapter(featureSupportRegistry, null, neLayer, asList(), behaviors); assertThatExceptionOfType(MultipleSentenceCoveredException.class) .isThrownBy(() -> sut.add(document, username, jcas, 0, jcas.getDocumentText().length())) .withMessageContaining("covers multiple sentences"); }
@Test public void thatSpanCrossSentenceBehaviorOnValidateReturnsErrorMessage() throws AnnotationException { TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test .\nThis is sentence two ."); SpanAdapter sut = new SpanAdapter(featureSupportRegistry, null, neLayer, asList(), behaviors); // Add two annotations neLayer.setCrossSentence(true); sut.add(document, username, jcas, 0, jcas.getDocumentText().length()); //Validation fails neLayer.setCrossSentence(false); assertThat(sut.validate(jcas)) .extracting(Pair::getLeft) .usingElementComparatorIgnoringFields("source", "message") .containsExactly(LogMessage.error(null, "")); }
.getLayer(state.getArmedFeature().getType(), state.getProject())); id = getAddr(adapter.add(state.getDocument(), state.getUser().getUsername(), aJCas, aBegin, aEnd));
/** * Copy this same annotation from the user annotation to the mergeview */ private static void copySpanAnnotation(AnnotatorState aState, AnnotationSchemaService aAnnotationService, AnnotationLayer aAnnotationLayer, AnnotationFS aOldFs, JCas aJCas) throws AnnotationException { SpanAdapter adapter = (SpanAdapter) aAnnotationService.getAdapter(aAnnotationLayer); // Create the annotation - this also takes care of attaching to an annotation if necessary int id = getAddr(adapter.add(aState.getDocument(), aState.getUser().getUsername(), aJCas, aOldFs.getBegin(), aOldFs.getEnd())); List<AnnotationFeature> features = aAnnotationService .listAnnotationFeature(adapter.getLayer()); // Copy the features for (AnnotationFeature feature : features) { Type oldType = adapter.getAnnotationType(aOldFs.getCAS()); Feature oldFeature = oldType.getFeatureByBaseName(feature.getName()); if (isLinkOrBasicFeatures(aOldFs, oldFeature)) { continue; } Object value = adapter.getFeatureValue(feature, aOldFs); adapter.setFeatureValue(aState.getDocument(), aState.getUser().getUsername(), aJCas, id, feature, value); } }
/** * Copy this same annotation from the user annotation to the mergeview */ private static void copySpanAnnotation(AnnotatorState aState, AnnotationSchemaService aAnnotationService, AnnotationLayer aAnnotationLayer, AnnotationFS aOldFs, JCas aJCas) throws AnnotationException { SpanAdapter adapter = (SpanAdapter) aAnnotationService.getAdapter(aAnnotationLayer); // Create the annotation - this also takes care of attaching to an annotation if necessary int id = getAddr(adapter.add(aState.getDocument(), aState.getUser().getUsername(), aJCas, aOldFs.getBegin(), aOldFs.getEnd())); List<AnnotationFeature> features = aAnnotationService .listAnnotationFeature(adapter.getLayer()); // Copy the features for (AnnotationFeature feature : features) { Type oldType = adapter.getAnnotationType(aOldFs.getCAS()); Feature oldFeature = oldType.getFeatureByBaseName(feature.getName()); if (isLinkOrBasicFeatures(aOldFs, oldFeature)) { continue; } Object value = adapter.getFeatureValue(feature, aOldFs); adapter.setFeatureValue(aState.getDocument(), aState.getUser().getUsername(), aJCas, id, feature, value); } }