Skip to content

Commit

Permalink
#2696 - Document-level recommendations
Browse files Browse the repository at this point in the history
- Removed a bunch of unused methods
- Moved a bunch of methods only used once to the classes where they are used
  • Loading branch information
reckart committed Dec 26, 2023
1 parent bf47a1f commit eb66783
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static java.util.Arrays.asList;
import static org.apache.uima.fit.factory.AnalysisEngineFactory.createEngine;
import static org.apache.uima.fit.factory.CollectionReaderFactory.createReader;
import static org.apache.uima.fit.util.CasUtil.getType;
import static org.apache.uima.fit.util.CasUtil.select;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.contentOf;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -32,6 +34,7 @@

import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.collection.CollectionReader;
import org.apache.uima.fit.factory.JCasFactory;
import org.dkpro.core.io.tcf.TcfReader;
Expand Down Expand Up @@ -59,6 +62,7 @@
import de.tudarmstadt.ukp.clarin.webanno.model.Project;
import de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument;
import de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS;
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.feature.bool.BooleanFeatureSupport;
import de.tudarmstadt.ukp.inception.annotation.feature.link.LinkFeatureSupport;
Expand All @@ -76,7 +80,6 @@
import de.tudarmstadt.ukp.inception.schema.api.AnnotationSchemaService;
import de.tudarmstadt.ukp.inception.schema.service.FeatureSupportRegistryImpl;
import de.tudarmstadt.ukp.inception.support.json.JSONUtil;
import de.tudarmstadt.ukp.inception.support.uima.WebAnnoCasUtil;

@ExtendWith(MockitoExtension.class)
public class BratSerializerImplTest
Expand Down Expand Up @@ -180,7 +183,7 @@ public void thatSentenceOrientedStrategyRenderCorrectly() throws Exception
state.setAllAnnotationLayers(schemaService.listAnnotationLayer(project));
state.setPagingStrategy(new SentenceOrientedPagingStrategy());
state.getPreferences().setWindowSize(10);
state.setFirstVisibleUnit(WebAnnoCasUtil.getFirstSentence(cas));
state.setFirstVisibleUnit(getFirstSentence(cas));
state.setProject(project);
state.setDocument(sourceDocument, asList(sourceDocument));

Expand Down Expand Up @@ -219,7 +222,7 @@ public void thatLineOrientedStrategyRenderCorrectly() throws Exception
AnnotatorState state = new AnnotatorStateImpl(Mode.ANNOTATION);
state.setPagingStrategy(new LineOrientedPagingStrategy());
state.getPreferences().setWindowSize(10);
state.setFirstVisibleUnit(WebAnnoCasUtil.getFirstSentence(cas));
state.setFirstVisibleUnit(getFirstSentence(cas));
state.setProject(project);
state.setDocument(sourceDocument, asList(sourceDocument));

Expand Down Expand Up @@ -258,7 +261,7 @@ public void thatTokenWrappingStrategyRenderCorrectly() throws Exception
AnnotatorState state = new AnnotatorStateImpl(Mode.ANNOTATION);
state.setPagingStrategy(new TokenWrappingPagingStrategy(80));
state.getPreferences().setWindowSize(10);
state.setFirstVisibleUnit(WebAnnoCasUtil.getFirstSentence(cas));
state.setFirstVisibleUnit(getFirstSentence(cas));
state.setProject(project);
state.setDocument(sourceDocument, asList(sourceDocument));

Expand All @@ -281,4 +284,22 @@ public void thatTokenWrappingStrategyRenderCorrectly() throws Exception
assertThat(contentOf(new File("src/test/resources/longlines.json"), UTF_8))
.isEqualToNormalizingNewlines(contentOf(new File(jsonFilePath), UTF_8));
}

/**
* Get the internal address of the first sentence annotation from CAS. This will be used as a
* reference for moving forward/backward sentences positions
*
* @param aCas
* The CAS object assumed to contains some sentence annotations
* @return the sentence number or -1 if aCas don't have sentence annotation
*/
private static AnnotationFS getFirstSentence(CAS aCas)
{
AnnotationFS firstSentence = null;
for (AnnotationFS s : select(aCas, getType(aCas, Sentence.class))) {
firstSentence = s;
break;
}
return firstSentence;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_QUERY;
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_QUERY_BEST_MATCH_TERM_NC;
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_QUERY_NC;
import static de.tudarmstadt.ukp.inception.support.uima.WebAnnoCasUtil.selectSentenceCovering;
import static de.tudarmstadt.ukp.inception.support.uima.WebAnnoCasUtil.selectTokensCovered;
import static java.lang.System.currentTimeMillis;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.toCollection;
import static org.apache.uima.fit.util.CasUtil.getType;
import static org.apache.uima.fit.util.CasUtil.select;

import java.io.File;
import java.net.URISyntaxException;
Expand All @@ -47,6 +47,7 @@
import org.apache.commons.lang3.Validate;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.fit.util.CasUtil;
import org.eclipse.rdf4j.common.net.ParsedIRI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -58,6 +59,8 @@
import org.springframework.core.annotation.AnnotationAwareOrderComparator;

import de.tudarmstadt.ukp.clarin.webanno.model.Project;
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.conceptlinking.config.EntityLinkingProperties;
import de.tudarmstadt.ukp.inception.conceptlinking.config.EntityLinkingPropertiesImpl;
import de.tudarmstadt.ukp.inception.conceptlinking.config.EntityLinkingServiceAutoConfiguration;
Expand Down Expand Up @@ -513,4 +516,31 @@ public List<KBHandle> searchItems(KnowledgeBase aKB, String aQuery)
{
return disambiguate(aKB, null, ConceptFeatureValueType.ANY_OBJECT, aQuery, null, 0, null);
}

/**
* Get the sentence based on the annotation begin offset
*
* @param aCas
* the CAS.
* @param aBegin
* the begin offset.
* @return the sentence.
*/
private static AnnotationFS selectSentenceCovering(CAS aCas, int aBegin)
{
AnnotationFS currentSentence = null;
for (AnnotationFS sentence : select(aCas, getType(aCas, Sentence.class))) {
if (sentence.getBegin() <= aBegin && sentence.getEnd() > aBegin) {
currentSentence = sentence;
break;
}
}
return currentSentence;
}

private static Collection<AnnotationFS> selectTokensCovered(AnnotationFS aCover)
{
return CasUtil.selectCovered(aCover.getCAS(), getType(aCover.getCAS(), Token.class),
aCover);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
import static org.apache.uima.cas.impl.Serialization.deserializeCASComplete;
import static org.apache.uima.cas.impl.Serialization.serializeCASComplete;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.FeatureStructure;
import org.apache.uima.cas.impl.CASImpl;
import org.apache.uima.resource.ResourceInitializationException;

Expand Down Expand Up @@ -93,8 +96,15 @@ public static CASImpl makeDummyCas()

public static Map<String, Long> countFeatureStructures(CASImpl casImpl)
{
return WebAnnoCasUtil.findAllFeatureStructures(casImpl).stream() //
return findAllFeatureStructures(casImpl).stream() //
.map(fs -> fs.getType().getName()) //
.collect(groupingBy(identity(), counting()));
}

public static Set<FeatureStructure> findAllFeatureStructures(CAS aCas)
{
Set<FeatureStructure> allFSes = new LinkedHashSet<>();
((CASImpl) aCas).walkReachablePlusFSsSorted(allFSes::add, null, null, null);
return allFSes;
}
}
9 changes: 4 additions & 5 deletions inception/inception-pdf-editor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
<groupId>org.dkpro.core</groupId>
<artifactId>dkpro-core-io-pdf-asl</artifactId>
</dependency>
<dependency>
<groupId>org.dkpro.core</groupId>
<artifactId>dkpro-core-api-segmentation-asl</artifactId>
</dependency>
<dependency>
<groupId>org.dkpro.core</groupId>
<artifactId>dkpro-core-api-resources-asl</artifactId>
Expand Down Expand Up @@ -168,11 +172,6 @@
<artifactId>dkpro-core-api-lexmorph-asl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dkpro.core</groupId>
<artifactId>dkpro-core-api-segmentation-asl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.tudarmstadt.ukp.inception.app</groupId>
<artifactId>inception-schema</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import static de.tudarmstadt.ukp.inception.pdfeditor.pdfanno.render.PdfAnnoSerializer.convertToDocumentOffset;
import static de.tudarmstadt.ukp.inception.pdfeditor.pdfanno.render.PdfAnnoSerializer.convertToDocumentOffsets;
import static de.tudarmstadt.ukp.inception.rendering.vmodel.VID.NONE_ID;
import static de.tudarmstadt.ukp.inception.support.uima.WebAnnoCasUtil.selectSentenceAt;
import static de.tudarmstadt.ukp.inception.support.wicket.ServletContextUtils.referenceToUrl;
import static java.lang.String.join;
import static java.util.Arrays.asList;
import static org.apache.uima.fit.util.CasUtil.getType;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -37,6 +37,7 @@
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.uima.cas.CAS;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.uima.fit.util.CasUtil;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.feedback.IFeedback;
Expand All @@ -48,6 +49,7 @@
import org.slf4j.LoggerFactory;

import de.tudarmstadt.ukp.clarin.webanno.api.casstorage.CasProvider;
import de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence;
import de.tudarmstadt.ukp.inception.documents.api.DocumentService;
import de.tudarmstadt.ukp.inception.editor.AnnotationEditorExtensionRegistry;
import de.tudarmstadt.ukp.inception.editor.action.AnnotationActionHandler;
Expand Down Expand Up @@ -314,4 +316,20 @@ private String getAnnotationsJS(PdfAnnoModel aPdfAnnoModel)
"'colorMap': {},", //
"'annotations':[annoFile]}, true);");
}

/**
* Get the sentence for this CAS based on the begin and end offsets. This is basically used to
* transform sentence address in one CAS to other sentence address for different CAS
*
* @param aCas
* the CAS.
* @param aBegin
* the begin offset.
* @return the sentence.
*/
private static AnnotationFS selectSentenceAt(CAS aCas, int aBegin)
{
return CasUtil.select(aCas, getType(aCas, Sentence.class)).stream()
.filter(s -> s.getBegin() == aBegin).findFirst().orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static de.tudarmstadt.ukp.inception.recommendation.api.model.AnnotationSuggestion.FLAG_OVERLAP;
import static de.tudarmstadt.ukp.inception.support.WebAnnoConst.FEAT_REL_SOURCE;
import static de.tudarmstadt.ukp.inception.support.WebAnnoConst.FEAT_REL_TARGET;
import static java.util.stream.Collectors.toList;
import static org.apache.uima.cas.text.AnnotationPredicates.colocated;
import static org.apache.uima.fit.util.CasUtil.select;
import static org.apache.uima.fit.util.CasUtil.selectAt;
Expand Down Expand Up @@ -243,8 +242,8 @@ public <T extends AnnotationSuggestion> void calculateSuggestionVisibility(Strin
// Collect all suggestions of the given layer
var groupedSuggestions = aRecommendations.stream()
.filter(group -> group.getLayerId() == aLayer.getId()) //
.map(group -> (SuggestionGroup) group) //
.collect(toList());
.map(group -> (SuggestionGroup<RelationSuggestion>) group) //
.toList();

// Get previously rejected suggestions
var groupedRecordedAnnotations = new ArrayListValuedHashMap<Position, LearningRecord>();
Expand All @@ -267,7 +266,7 @@ public <T extends AnnotationSuggestion> void calculateSuggestionVisibility(Strin
return;
}

for (SuggestionGroup<RelationSuggestion> group : groupedSuggestions) {
for (var group : groupedSuggestions) {
if (!feature.getName().equals(group.getFeature())) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import static de.tudarmstadt.ukp.inception.recommendation.service.Fixtures.getVisibleSuggestions;
import static de.tudarmstadt.ukp.inception.recommendation.service.Fixtures.makeRelationSuggestionGroup;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
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.when;

import java.util.ArrayList;

import org.apache.uima.cas.CAS;
import org.apache.uima.fit.factory.JCasFactory;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -82,8 +81,8 @@ public void setUp() throws Exception
@Test
public void testCalculateVisibilityNoRecordsAllHidden() throws Exception
{
doReturn(new ArrayList<>()).when(learningRecordService).listLearningRecords(TEST_USER,
TEST_USER, layer);
doReturn(emptyList()).when(learningRecordService).listLearningRecords(TEST_USER, TEST_USER,
layer);

var cas = getTestCas();
var suggestions = makeRelationSuggestionGroup(doc, feature,
Expand All @@ -105,8 +104,8 @@ public void testCalculateVisibilityNoRecordsAllHidden() throws Exception
@Test
public void thatVisibilityIsRestoredWhenOverlappingAnnotationIsRemoved() throws Exception
{
doReturn(new ArrayList<>()).when(learningRecordService).listLearningRecords(TEST_USER,
TEST_USER, layer);
doReturn(emptyList()).when(learningRecordService).listLearningRecords(TEST_USER, TEST_USER,
layer);

var cas = getTestCas();
var suggestions = makeRelationSuggestionGroup(doc, feature,
Expand Down
Loading

0 comments on commit eb66783

Please sign in to comment.