@Override public SpanAdapter createAdapter(AnnotationLayer aLayer) { SpanAdapter adapter = new SpanAdapter(featureSupportRegistry, eventPublisher, aLayer, schemaService.listAnnotationFeature(aLayer), layerBehaviorsRegistry.getLayerBehaviors(this, SpanLayerBehavior.class)); return adapter; }
@Override public SpanAdapter createAdapter(AnnotationLayer aLayer) { SpanAdapter adapter = new SpanAdapter(featureSupportRegistry, eventPublisher, aLayer, schemaService.listAnnotationFeature(aLayer), layerBehaviorsRegistry.getLayerBehaviors(this, SpanLayerBehavior.class)); return adapter; }
@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 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 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, "")); }
@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"); }
@Test public void thatSpanCrossSentenceBehaviorOnRenderGeneratesErrors() { neLayer.setCrossSentence(false); jcas.setDocumentText(StringUtils.repeat("a", 20)); new Sentence(jcas, 0, 10).addToIndexes(); new Sentence(jcas, 10, 20).addToIndexes(); NamedEntity ne = new NamedEntity(jcas, 5, 15); ne.addToIndexes(); SpanAdapter adapter = new SpanAdapter(featureSupportRegistry, null, neLayer, asList(), asList(new SpanCrossSentenceBehavior())); SpanRenderer sut = new SpanRenderer(adapter, featureSupportRegistry, asList(new SpanCrossSentenceBehavior())); VDocument vdoc = new VDocument(); sut.render(jcas, asList(), vdoc, 0, jcas.getDocumentText().length()); assertThat(vdoc.comments()) .usingFieldByFieldElementComparator() .containsExactlyInAnyOrder(new VComment(ne, VCommentType.ERROR, "Crossing sentence bounardies is not permitted.")); }
@Test public void thatSpanStackingBehaviorOnRenderGeneratesErrors() { neLayer.setAllowStacking(false); jcas.setDocumentText(StringUtils.repeat("a", 10)); new Sentence(jcas, 0, 10).addToIndexes(); NamedEntity ne1 = new NamedEntity(jcas, 3, 8); ne1.addToIndexes(); NamedEntity ne2 = new NamedEntity(jcas, 3, 8); ne2.addToIndexes(); SpanAdapter adapter = new SpanAdapter(featureSupportRegistry, null, neLayer, asList(), asList(new SpanStackingBehavior())); SpanRenderer sut = new SpanRenderer(adapter, featureSupportRegistry, asList(new SpanStackingBehavior())); VDocument vdoc = new VDocument(); sut.render(jcas, asList(), vdoc, 0, jcas.getDocumentText().length()); assertThat(vdoc.comments()) .usingFieldByFieldElementComparator() .containsExactlyInAnyOrder( new VComment(ne1, VCommentType.ERROR, "Stacking is not permitted."), new VComment(ne2, VCommentType.ERROR, "Stacking is not permitted.")); } }
@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, "")); }