@Test public void thatRelationAttachmentBehaviorOnCreateWorks() throws Exception { TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test ."); for (Token t : select(jcas, Token.class)) { POS pos = new POS(jcas, t.getBegin(), t.getEnd()); t.setPos(pos); pos.addToIndexes(); } RelationAdapter sut = new RelationAdapter(featureSupportRegistry, null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE, asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors); List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class)); List<Token> tokens = new ArrayList<>(select(jcas, Token.class)); POS source = posAnnotations.get(0); POS target = posAnnotations.get(1); AnnotationFS dep = sut.add(document, username, source, target, jcas, 0, jcas.getDocumentText().length()); assertThat(FSUtil.getFeature(dep, FEAT_REL_SOURCE, Token.class)).isEqualTo(tokens.get(0)); assertThat(FSUtil.getFeature(dep, FEAT_REL_TARGET, Token.class)).isEqualTo(tokens.get(1)); }
@Test public void thatRelationStackingBehaviorOnCreateDoesNotThrowException() throws Exception { TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test .\nThis is sentence two ."); for (Token t : select(jcas, Token.class)) { POS pos = new POS(jcas, t.getBegin(), t.getEnd()); t.setPos(pos); pos.addToIndexes(); } RelationAdapter sut = new RelationAdapter(featureSupportRegistry, null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE, asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors); List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class)); List<Token> tokens = new ArrayList<>(select(jcas, Token.class)); POS source = posAnnotations.get(0); POS target = posAnnotations.get(1); depLayer.setAllowStacking(true); AnnotationFS dep1 = sut.add(document, username, source, target, jcas, 0, jcas.getDocumentText().length()); AnnotationFS dep2 = sut.add(document, username, source, target, jcas, 0, jcas.getDocumentText().length()); assertThat(FSUtil.getFeature(dep1, FEAT_REL_SOURCE, Token.class)).isEqualTo(tokens.get(0)); assertThat(FSUtil.getFeature(dep1, FEAT_REL_TARGET, Token.class)).isEqualTo(tokens.get(1)); assertThat(FSUtil.getFeature(dep2, FEAT_REL_SOURCE, Token.class)).isEqualTo(tokens.get(0)); assertThat(FSUtil.getFeature(dep2, FEAT_REL_TARGET, Token.class)).isEqualTo(tokens.get(1)); }
private void createNewRelationAnnotation(RelationAdapter aAdapter, JCas aJCas) throws AnnotationException { LOG.trace("createNewRelationAnnotation()"); AnnotatorState state = getModelObject(); Selection selection = state.getSelection(); AnnotationFS originFs = selectByAddr(aJCas, selection.getOrigin()); AnnotationFS targetFs = selectByAddr(aJCas, selection.getTarget()); // Creating a relation AnnotationFS arc = aAdapter.add(state.getDocument(), state.getUser().getUsername(), originFs, targetFs, aJCas, state.getWindowBeginOffset(), state.getWindowEndOffset()); selection.selectArc(new VID(arc), originFs, targetFs); }
@Test public void thatRelationStackingBehaviorOnCreateThrowsException() throws Exception { depLayer.setAllowStacking(false); TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test .\nThis is sentence two ."); for (Token t : select(jcas, Token.class)) { POS pos = new POS(jcas, t.getBegin(), t.getEnd()); t.setPos(pos); pos.addToIndexes(); } RelationAdapter sut = new RelationAdapter(featureSupportRegistry, null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE, asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors); List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class)); POS source = posAnnotations.get(0); POS target = posAnnotations.get(1); // First annotation should work sut.add(document, username, source, target, jcas, 0, jcas.getDocumentText().length()); // Second one at the same location should cause an error assertThatExceptionOfType(AnnotationException.class) .isThrownBy(() -> sut.add(document, username, source, target, jcas, 0, jcas.getDocumentText().length())) .withMessageContaining("stacking is not enabled"); }
@Test public void thatRelationCrossSentenceBehaviorOnCreateThrowsException() throws Exception { depLayer.setCrossSentence(false); TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test .\nThis is sentence two ."); for (Token t : select(jcas, Token.class)) { POS pos = new POS(jcas, t.getBegin(), t.getEnd()); t.setPos(pos); pos.addToIndexes(); } RelationAdapter sut = new RelationAdapter(featureSupportRegistry, null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE, asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors); List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class)); POS source = posAnnotations.get(0); POS target = posAnnotations.get(posAnnotations.size() - 1); assertThatExceptionOfType(MultipleSentenceCoveredException.class) .isThrownBy(() -> sut.add(document, username, source, target, jcas, 0, jcas.getDocumentText().length())) .withMessageContaining("multiple sentences"); }
@Test public void thatRelationCrossSentenceBehaviorOnValidateGeneratesErrors() throws Exception { TokenBuilder<Token, Sentence> builder = new TokenBuilder<>(Token.class, Sentence.class); builder.buildTokens(jcas, "This is a test .\nThis is sentence two ."); for (Token t : select(jcas, Token.class)) { POS pos = new POS(jcas, t.getBegin(), t.getEnd()); t.setPos(pos); pos.addToIndexes(); } RelationAdapter sut = new RelationAdapter(featureSupportRegistry, null, depLayer, FEAT_REL_TARGET, FEAT_REL_SOURCE, asList(dependencyLayerGovernor, dependencyLayerDependent), behaviors); List<POS> posAnnotations = new ArrayList<>(select(jcas, POS.class)); POS source = posAnnotations.get(0); POS target = posAnnotations.get(posAnnotations.size() - 1); depLayer.setCrossSentence(true); sut.add(document, username, source, target, jcas, 0, jcas.getDocumentText().length()); depLayer.setCrossSentence(false); assertThat(sut.validate(jcas)) .extracting(Pair::getLeft) .usingElementComparatorIgnoringFields("source", "message") .containsExactly(LogMessage.error(null, "")); }
AnnotationFS dep = adapter.add(document, username, source, target, jcas, 0, jcas.getDocumentText().length());
if (dCoveredText.equals(fs.getCoveredText())) { if (g != null && isSamAnno(attachSpanType, fs, aDepFS)) { AnnotationFS arc = adapter.add(aState.getDocument(), aState.getUser().getUsername(), g, fs, jCas, aStart, aEnd); adapter.setFeatureValue(aState.getDocument(), aState.getUser().getUsername(), AnnotationFS arc = adapter.add(aState.getDocument(), aState.getUser().getUsername(), fs, d, jCas, aStart, aEnd); adapter.setFeatureValue(aState.getDocument(), aState.getUser().getUsername(),
if (dCoveredText.equals(fs.getCoveredText())) { if (g != null && isSamAnno(attachSpanType, fs, aDepFS)) { AnnotationFS arc = adapter.add(aState.getDocument(), aState.getUser().getUsername(), g, fs, jCas, aStart, aEnd); adapter.setFeatureValue(aState.getDocument(), aState.getUser().getUsername(), AnnotationFS arc = adapter.add(aState.getDocument(), aState.getUser().getUsername(), fs, d, jCas, aStart, aEnd); adapter.setFeatureValue(aState.getDocument(), aState.getUser().getUsername(),
if (adapter instanceof RelationAdapter) { AnnotationFS arc = ((RelationAdapter) adapter).add(state.getDocument(), state.getUser().getUsername(), targetFs, originFs, jCas, state.getWindowBeginOffset(), state.getWindowEndOffset());