Skip to content

Commit

Permalink
#2696 - Document-level recommendations
Browse files Browse the repository at this point in the history
- Switch unit tests away from deprecated methods
  • Loading branch information
reckart committed Dec 20, 2023
1 parent 05e96e2 commit 7460495
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
import de.tudarmstadt.ukp.inception.annotation.feature.string.StringFeatureSupport;
import de.tudarmstadt.ukp.inception.annotation.layer.span.SpanAdapter;
import de.tudarmstadt.ukp.inception.annotation.layer.span.SpanLayerSupport;
import de.tudarmstadt.ukp.inception.annotation.storage.CasStorageSession;
import de.tudarmstadt.ukp.inception.recommendation.api.RecommenderFactoryRegistry;
import de.tudarmstadt.ukp.inception.recommendation.api.model.LearningRecord;
Expand All @@ -85,7 +86,6 @@
import de.tudarmstadt.ukp.inception.schema.api.layer.LayerSupportRegistry;
import de.tudarmstadt.ukp.inception.schema.service.AnnotationSchemaServiceImpl;
import de.tudarmstadt.ukp.inception.schema.service.FeatureSupportRegistryImpl;
import de.tudarmstadt.ukp.inception.support.WebAnnoConst;

@ExtendWith(MockitoExtension.class)
@ContextConfiguration(classes = SpringConfig.class)
Expand Down Expand Up @@ -516,7 +516,7 @@ private AnnotationLayer createAnnotationLayer(String aType)
l.setEnabled(true);
l.setName(aType);
l.setReadonly(false);
l.setType(WebAnnoConst.SPAN_TYPE);
l.setType(SpanLayerSupport.TYPE);
l.setUiName(aType);
l.setAnchoringMode(false, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@
import de.tudarmstadt.ukp.clarin.webanno.security.model.User;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
import de.tudarmstadt.ukp.inception.annotation.layer.span.SpanLayerSupport;
import de.tudarmstadt.ukp.inception.recommendation.api.model.AnnotationSuggestion;
import de.tudarmstadt.ukp.inception.recommendation.api.model.LearningRecord;
import de.tudarmstadt.ukp.inception.recommendation.api.model.Offset;
import de.tudarmstadt.ukp.inception.recommendation.api.model.Predictions;
import de.tudarmstadt.ukp.inception.recommendation.api.model.Recommender;
import de.tudarmstadt.ukp.inception.recommendation.api.model.SpanSuggestion;
import de.tudarmstadt.ukp.inception.rendering.model.Range;
import de.tudarmstadt.ukp.inception.support.WebAnnoConst;

class RecommendationServiceImplTest
{
Expand Down Expand Up @@ -351,7 +351,7 @@ void testExtractSuggestionsWithSpanSuggestions() throws Exception
var layer = AnnotationLayer.builder() //
.withId(1l) //
.forUimaType(targetCas.getTypeSystem().getType(predType.getName())) //
.withType(WebAnnoConst.SPAN_TYPE) //
.withType(SpanLayerSupport.TYPE) //
.withAnchoringMode(TOKENS) //
.build();
var feature = AnnotationFeature.builder() //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@
import static org.apache.uima.cas.CAS.TYPE_NAME_STRING;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import org.apache.uima.cas.CAS;
import org.apache.uima.fit.factory.JCasFactory;
import org.apache.uima.jcas.JCas;
Expand All @@ -47,6 +44,7 @@
import de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token;
import de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency;
import de.tudarmstadt.ukp.inception.recommendation.api.LearningRecordService;
import de.tudarmstadt.ukp.inception.recommendation.api.model.AnnotationSuggestion;
import de.tudarmstadt.ukp.inception.schema.api.AnnotationSchemaService;

Expand All @@ -56,13 +54,14 @@ public class RelationSuggestionVisibilityCalculationTest
private static final String TEST_USER = "Testuser";

private @Mock AnnotationSchemaService annoService;
private @Mock LearningRecordService learningRecordService;

private Project project;
private SourceDocument doc;
private AnnotationLayer layer;
private AnnotationFeature feature;

private RecommendationServiceImpl sut;
private RelationRecommendationSupportImpl sut;

@BeforeEach
public void setUp() throws Exception
Expand All @@ -84,20 +83,20 @@ public void setUp() throws Exception
.add(new AnnotationFeature(Dependency._FeatName_DependencyType, TYPE_NAME_STRING));
when(annoService.listSupportedFeatures(layer)).thenReturn(featureList);

sut = spy(new RecommendationServiceImpl(null, null, null, null, null, annoService, null,
null, (EntityManager) null, null));
sut = new RelationRecommendationSupportImpl(null, learningRecordService, null, annoService);
}

@Test
public void testCalculateVisibilityNoRecordsAllHidden() throws Exception
{
doReturn(new ArrayList<>()).when(sut).listLearningRecords(TEST_USER, TEST_USER, layer);
doReturn(new ArrayList<>()).when(learningRecordService).listLearningRecords(TEST_USER,
TEST_USER, layer);

var cas = getTestCas();
var suggestions = makeRelationSuggestionGroup(doc, feature,
new int[][] { { 1, 0, 3, 13, 20 } });
sut.calculateRelationSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer,
suggestions, 0, 25);
sut.calculateSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
25);

assertThat(getVisibleSuggestions(suggestions)) //
.as("No suggestions are visible as they overlap with annotations") //
Expand All @@ -113,13 +112,14 @@ public void testCalculateVisibilityNoRecordsAllHidden() throws Exception
@Test
public void thatVisibilityIsRestoredWhenOverlappingAnnotationIsRemoved() throws Exception
{
doReturn(new ArrayList<>()).when(sut).listLearningRecords(TEST_USER, TEST_USER, layer);
doReturn(new ArrayList<>()).when(learningRecordService).listLearningRecords(TEST_USER,
TEST_USER, layer);

var cas = getTestCas();
var suggestions = makeRelationSuggestionGroup(doc, feature,
new int[][] { { 1, 0, 3, 13, 20 } });
sut.calculateRelationSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer,
suggestions, 0, 25);
sut.calculateSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
25);

assertThat(getVisibleSuggestions(suggestions)) //
.as("No suggestions are visible as they overlap with annotations") //
Expand All @@ -130,8 +130,8 @@ public void thatVisibilityIsRestoredWhenOverlappingAnnotationIsRemoved() throws

cas.select(Dependency.class).forEach(Dependency::removeFromIndexes);

sut.calculateRelationSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer,
suggestions, 0, 25);
sut.calculateSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
25);

assertThat(getInvisibleSuggestions(suggestions)) //
.as("No suggestions are hidden as they no longer overlap with annotations") //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@
import static org.apache.uima.cas.CAS.TYPE_NAME_STRING;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import org.apache.uima.cas.CAS;
import org.apache.uima.fit.factory.JCasFactory;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -47,6 +44,7 @@
import de.tudarmstadt.ukp.clarin.webanno.model.Project;
import de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument;
import de.tudarmstadt.ukp.dkpro.core.api.ner.type.NamedEntity;
import de.tudarmstadt.ukp.inception.recommendation.api.LearningRecordService;
import de.tudarmstadt.ukp.inception.recommendation.api.model.AnnotationSuggestion;
import de.tudarmstadt.ukp.inception.recommendation.api.model.LearningRecord;
import de.tudarmstadt.ukp.inception.recommendation.api.model.LearningRecordUserAction;
Expand All @@ -61,13 +59,14 @@ public class SpanSuggestionVisibilityCalculationTest
private static final String TEST_USER = "Testuser";

private @Mock AnnotationSchemaService annoService;
private @Mock LearningRecordService learningRecordService;

private Project project;
private SourceDocument doc;
private AnnotationLayer layer;
private AnnotationFeature feature;

private RecommendationServiceImpl sut;
private SpanRecommendationSupportImpl sut;

@BeforeEach
public void setUp() throws Exception
Expand All @@ -88,19 +87,19 @@ public void setUp() throws Exception
featureList.add(new AnnotationFeature(NamedEntity._FeatName_value, TYPE_NAME_STRING));
when(annoService.listSupportedFeatures(layer)).thenReturn(featureList);

sut = spy(new RecommendationServiceImpl(null, null, null, null, null, annoService, null,
(EntityManager) null));
sut = new SpanRecommendationSupportImpl(null, learningRecordService, null, annoService);
}

@Test
public void testCalculateVisibilityNoRecordsAllHidden() throws Exception
{
doReturn(new ArrayList<>()).when(sut).listLearningRecords(TEST_USER, TEST_USER, layer);
doReturn(new ArrayList<>()).when(learningRecordService).listLearningRecords(TEST_USER,
TEST_USER, layer);

var cas = getTestCas();
var suggestions = makeSpanSuggestionGroup(doc, feature,
new int[][] { { 1, 0, 3 }, { 2, 13, 20 } });
sut.calculateSpanSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
sut.calculateSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
25);

var invisibleSuggestions = getInvisibleSuggestions(suggestions);
Expand All @@ -122,11 +121,12 @@ public void testCalculateVisibilityNoRecordsAllHidden() throws Exception
@Test
public void testCalculateVisibilityNoRecordsNotHidden() throws Exception
{
doReturn(new ArrayList<>()).when(sut).listLearningRecords(TEST_USER, TEST_USER, layer);
doReturn(new ArrayList<>()).when(learningRecordService).listLearningRecords(TEST_USER,
TEST_USER, layer);

var cas = getTestCas();
var suggestions = makeSpanSuggestionGroup(doc, feature, new int[][] { { 1, 5, 10 } });
sut.calculateSpanSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
sut.calculateSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
25);

var invisibleSuggestions = getInvisibleSuggestions(suggestions);
Expand All @@ -149,11 +149,12 @@ public void testCalculateVisibilityRejected() throws Exception
rejectedRecord.setOffsetBegin(5);
rejectedRecord.setOffsetEnd(10);
records.add(rejectedRecord);
doReturn(records).when(sut).listLearningRecords(TEST_USER, TEST_USER, layer);
doReturn(records).when(learningRecordService).listLearningRecords(TEST_USER, TEST_USER,
layer);

var cas = getTestCas();
var suggestions = makeSpanSuggestionGroup(doc, feature, new int[][] { { 1, 5, 10 } });
sut.calculateSpanSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
sut.calculateSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
25);

var invisibleSuggestions = getInvisibleSuggestions(suggestions);
Expand All @@ -171,12 +172,13 @@ public void testCalculateVisibilityRejected() throws Exception
@Test
public void thatVisibilityIsRestoredWhenOverlappingAnnotationIsRemoved() throws Exception
{
doReturn(new ArrayList<>()).when(sut).listLearningRecords(TEST_USER, TEST_USER, layer);
doReturn(new ArrayList<>()).when(learningRecordService).listLearningRecords(TEST_USER,
TEST_USER, layer);

var cas = getTestCas();
var suggestions = makeSpanSuggestionGroup(doc, feature,
new int[][] { { 1, 0, 3 }, { 2, 13, 20 } });
sut.calculateSpanSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
sut.calculateSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
25);

assertThat(getVisibleSuggestions(suggestions)) //
Expand All @@ -188,7 +190,7 @@ public void thatVisibilityIsRestoredWhenOverlappingAnnotationIsRemoved() throws

cas.select(NamedEntity.class).forEach(NamedEntity::removeFromIndexes);

sut.calculateSpanSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
sut.calculateSuggestionVisibility(TEST_USER, doc, cas, TEST_USER, layer, suggestions, 0,
25);

assertThat(getInvisibleSuggestions(suggestions)) //
Expand All @@ -203,7 +205,8 @@ public void thatVisibilityIsRestoredWhenOverlappingAnnotationIsRemoved() throws
@Test
public void thatOverlappingSuggestionsAreNotHiddenWhenStackingIsEnabled() throws Exception
{
doReturn(new ArrayList<>()).when(sut).listLearningRecords(TEST_USER, TEST_USER, layer);
doReturn(new ArrayList<>()).when(learningRecordService).listLearningRecords(TEST_USER,
TEST_USER, layer);

layer.setOverlapMode(OverlapMode.ANY_OVERLAP);

Expand All @@ -227,7 +230,7 @@ public void thatOverlappingSuggestionsAreNotHiddenWhenStackingIsEnabled() throws
.build();
var suggestions = new SuggestionDocumentGroup<>(asList(suggestion1, suggestion2));

sut.calculateSpanSuggestionVisibility(TEST_USER, doc, cas.getCas(), TEST_USER, layer,
sut.calculateSuggestionVisibility(TEST_USER, doc, cas.getCas(), TEST_USER, layer,
suggestions, 0, 2);

assertThat(getInvisibleSuggestions(suggestions)) //
Expand All @@ -247,7 +250,7 @@ public void thatOverlappingSuggestionsAreNotHiddenWhenStackingIsEnabled() throws
.isEmpty();

ne1.setValue("blah");
sut.calculateSpanSuggestionVisibility(TEST_USER, doc, cas.getCas(), TEST_USER, layer,
sut.calculateSuggestionVisibility(TEST_USER, doc, cas.getCas(), TEST_USER, layer,
suggestions, 0, 2);

assertThat(getInvisibleSuggestions(suggestions)) //
Expand All @@ -257,7 +260,7 @@ public void thatOverlappingSuggestionsAreNotHiddenWhenStackingIsEnabled() throws
var ne2 = new NamedEntity(cas, 1, 2);
ne2.setValue("blah");
ne2.addToIndexes();
sut.calculateSpanSuggestionVisibility(TEST_USER, doc, cas.getCas(), TEST_USER, layer,
sut.calculateSuggestionVisibility(TEST_USER, doc, cas.getCas(), TEST_USER, layer,
suggestions, 0, 2);

assertThat(getInvisibleSuggestions(suggestions)) //
Expand Down

0 comments on commit 7460495

Please sign in to comment.