use of org.apache.uima.cas.Feature in project webanno by webanno.
the class CopyAnnotationTest method copyLinkToExistingButDiffLinkTest.
@Test
public void copyLinkToExistingButDiffLinkTest() throws Exception {
JCas mergeCAs = JCasFactory.createJCas(DiffUtils.createMultiLinkWithRoleTestTypeSytem("f1"));
Type type = mergeCAs.getTypeSystem().getType(DiffUtils.HOST_TYPE);
Feature feature = type.getFeatureByBaseName("f1");
AnnotationFS mergeFs = DiffUtils.makeLinkHostMultiSPanFeatureFS(mergeCAs, 0, 0, feature, "A", DiffUtils.makeLinkFS(mergeCAs, "slot1", 0, 0));
FeatureStructure copyFS = DiffUtils.makeLinkFS(mergeCAs, "slot2", 0, 0);
List<FeatureStructure> linkFs = new ArrayList<>();
linkFs.add(copyFS);
WebAnnoCasUtil.setLinkFeatureValue(mergeFs, type.getFeatureByBaseName("links"), linkFs);
JCas jcasA = JCasFactory.createJCas(DiffUtils.createMultiLinkWithRoleTestTypeSytem("f1"));
DiffUtils.makeLinkHostMultiSPanFeatureFS(jcasA, 0, 0, feature, "A", DiffUtils.makeLinkFS(jcasA, "slot1", 0, 0));
Map<String, List<JCas>> casByUser = new LinkedHashMap<>();
casByUser.put("user1", asList(mergeCAs));
casByUser.put("user2", asList(jcasA));
List<String> entryTypes = asList(DiffUtils.HOST_TYPE);
CasDiff2.SpanDiffAdapter adapter = new CasDiff2.SpanDiffAdapter(DiffUtils.HOST_TYPE);
adapter.addLinkFeature("links", "role", "target");
List<? extends CasDiff2.DiffAdapter> diffAdapters = asList(adapter);
CasDiff2.DiffResult diff = CasDiff2.doDiff(entryTypes, diffAdapters, CasDiff2.LinkCompareBehavior.LINK_TARGET_AS_LABEL, casByUser);
assertEquals(0, diff.getDifferingConfigurationSets().size());
assertEquals(2, diff.getIncompleteConfigurationSets().size());
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class CopyAnnotationTest method simpleCopyToDiffExistingAnnoWithStackingTest.
@Test
public void simpleCopyToDiffExistingAnnoWithStackingTest() throws Exception {
AnnotatorState state = new AnnotatorStateImpl(Mode.CURATION);
state.setUser(new User());
posLayer.setAllowStacking(true);
JCas jcas = JCasFactory.createJCas();
Type type = jcas.getTypeSystem().getType(POS.class.getTypeName());
AnnotationFS clickedFs = createPOSAnno(jcas, type, "NN", 0, 0);
JCas mergeCAs = JCasFactory.createJCas();
createTokenAnno(mergeCAs, 0, 0);
AnnotationFS existingFs = mergeCAs.getCas().createAnnotation(type, 0, 0);
Feature posValue = type.getFeatureByBaseName("PosValue");
existingFs.setStringValue(posValue, "NE");
mergeCAs.addFsToIndexes(existingFs);
MergeCas.addSpanAnnotation(state, annotationSchemaService, posLayer, mergeCAs, clickedFs, true);
assertEquals(2, CasUtil.selectCovered(mergeCAs.getCas(), type, 0, 0).size());
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class CopyAnnotationTest method copySpanWithSlotWithStackingTest.
@Test
public void copySpanWithSlotWithStackingTest() throws Exception {
AnnotatorState state = new AnnotatorStateImpl(Mode.CURATION);
state.setUser(new User());
slotLayer.setAllowStacking(true);
JCas jcasA = JCasFactory.createJCas(DiffUtils.createMultiLinkWithRoleTestTypeSytem("f1"));
Type type = jcasA.getTypeSystem().getType(DiffUtils.HOST_TYPE);
Feature feature = type.getFeatureByBaseName("f1");
AnnotationFS clickedFs = DiffUtils.makeLinkHostMultiSPanFeatureFS(jcasA, 0, 0, feature, "A", DiffUtils.makeLinkFS(jcasA, "slot1", 0, 0));
JCas mergeCAs = JCasFactory.createJCas(DiffUtils.createMultiLinkWithRoleTestTypeSytem("f1"));
DiffUtils.makeLinkHostMultiSPanFeatureFS(mergeCAs, 0, 0, feature, "C", DiffUtils.makeLinkFS(mergeCAs, "slot1", 0, 0));
MergeCas.addSpanAnnotation(state, annotationSchemaService, slotLayer, mergeCAs, clickedFs, true);
assertEquals(2, CasUtil.selectCovered(mergeCAs.getCas(), type, 0, 0).size());
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class CopyAnnotationTest method simpleCopyRelationToStackedTargetsTest.
@Test
public void simpleCopyRelationToStackedTargetsTest() throws Exception {
JCas jcas = JCasFactory.createJCas();
Type type = jcas.getTypeSystem().getType(Dependency.class.getTypeName());
Type posType = jcas.getTypeSystem().getType(POS.class.getTypeName());
AnnotationFS originClickedToken = createTokenAnno(jcas, 0, 0);
AnnotationFS targetClickedToken = createTokenAnno(jcas, 1, 1);
AnnotationFS originClicked = createPOSAnno(jcas, posType, "NN", 0, 0);
AnnotationFS targetClicked = createPOSAnno(jcas, posType, "NN", 1, 1);
jcas.addFsToIndexes(originClicked);
jcas.addFsToIndexes(targetClicked);
originClickedToken.setFeatureValue(originClickedToken.getType().getFeatureByBaseName("pos"), originClicked);
targetClickedToken.setFeatureValue(targetClickedToken.getType().getFeatureByBaseName("pos"), targetClicked);
Feature sourceFeature = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE);
Feature targetFeature = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET);
AnnotationFS clickedFs = jcas.getCas().createAnnotation(type, 0, 1);
clickedFs.setFeatureValue(sourceFeature, originClickedToken);
clickedFs.setFeatureValue(targetFeature, targetClickedToken);
jcas.addFsToIndexes(clickedFs);
JCas mergeCAs = JCasFactory.createJCas();
AnnotationFS origin = createPOSAnno(mergeCAs, posType, "NN", 0, 0);
AnnotationFS target = createPOSAnno(mergeCAs, posType, "NN", 1, 1);
mergeCAs.addFsToIndexes(origin);
mergeCAs.addFsToIndexes(target);
AnnotationFS originToken = createTokenAnno(mergeCAs, 0, 0);
AnnotationFS targetToken = createTokenAnno(mergeCAs, 1, 1);
originToken.setFeatureValue(originToken.getType().getFeatureByBaseName("pos"), origin);
targetToken.setFeatureValue(targetToken.getType().getFeatureByBaseName("pos"), target);
mergeCAs.addFsToIndexes(originToken);
mergeCAs.addFsToIndexes(targetToken);
AnnotationFS origin2 = createPOSAnno(mergeCAs, posType, "NN", 0, 0);
AnnotationFS target2 = createPOSAnno(mergeCAs, posType, "NN", 1, 1);
mergeCAs.addFsToIndexes(origin2);
mergeCAs.addFsToIndexes(target2);
AnnotationFS originToken2 = createTokenAnno(mergeCAs, 0, 0);
AnnotationFS targetToken2 = createTokenAnno(mergeCAs, 1, 1);
originToken2.setFeatureValue(originToken.getType().getFeatureByBaseName("pos"), origin2);
targetToken2.setFeatureValue(targetToken.getType().getFeatureByBaseName("pos"), target2);
mergeCAs.addFsToIndexes(originToken2);
mergeCAs.addFsToIndexes(targetToken2);
exception.expect(AnnotationException.class);
MergeCas.addRelationArcAnnotation(mergeCAs, clickedFs, true, false, originToken, targetToken);
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class AutomationUtil method automate.
/**
* Add new annotation to the CAS using the MIRA prediction. This is different from the add
* methods in the {@link TypeAdapter}s in such a way that the begin and end offsets are always
* exact so that no need to re-compute
*
* @param aJcas
* the JCas.
* @param aFeature
* the feature.
* @param aLabelValues
* the values.
* @throws AnnotationException
* if the annotations could not be created/updated.
* @throws IOException
* if an I/O error occurs.
*/
public static void automate(JCas aJcas, AnnotationFeature aFeature, List<String> aLabelValues) throws AnnotationException, IOException {
String typeName = aFeature.getLayer().getName();
String attachTypeName = aFeature.getLayer().getAttachType() == null ? null : aFeature.getLayer().getAttachType().getName();
Type type = CasUtil.getType(aJcas.getCas(), typeName);
Feature feature = type.getFeatureByBaseName(aFeature.getName());
int i = 0;
String prevNe = "O";
int begin = 0;
int end = 0;
// remove existing annotations of this type, after all it is an
// automation, no care
clearAnnotations(aJcas, type);
if (!aFeature.getLayer().isLockToTokenOffset() || aFeature.getLayer().isMultipleTokens()) {
for (Token token : select(aJcas, Token.class)) {
String value = aLabelValues.get(i);
AnnotationFS newAnnotation;
if (value.equals("O") && prevNe.equals("O")) {
i++;
continue;
} else if (value.equals("O") && !prevNe.equals("O")) {
newAnnotation = aJcas.getCas().createAnnotation(type, begin, end);
newAnnotation.setFeatureValueFromString(feature, prevNe.replace("B-", ""));
prevNe = "O";
aJcas.getCas().addFsToIndexes(newAnnotation);
} else if (!value.equals("O") && prevNe.equals("O")) {
begin = token.getBegin();
end = token.getEnd();
prevNe = value;
} else if (!value.equals("O") && !prevNe.equals("O")) {
if (value.replace("B-", "").replace("I-", "").equals(prevNe.replace("B-", "").replace("I-", "")) && value.startsWith("B-")) {
newAnnotation = aJcas.getCas().createAnnotation(type, begin, end);
newAnnotation.setFeatureValueFromString(feature, prevNe.replace("B-", "").replace("I-", ""));
prevNe = value;
begin = token.getBegin();
end = token.getEnd();
aJcas.getCas().addFsToIndexes(newAnnotation);
} else if (value.replace("B-", "").replace("I-", "").equals(prevNe.replace("B-", "").replace("I-", ""))) {
i++;
end = token.getEnd();
continue;
} else {
newAnnotation = aJcas.getCas().createAnnotation(type, begin, end);
newAnnotation.setFeatureValueFromString(feature, prevNe.replace("B-", "").replace("I-", ""));
prevNe = value;
begin = token.getBegin();
end = token.getEnd();
aJcas.getCas().addFsToIndexes(newAnnotation);
}
}
i++;
}
} else {
// check if annotation is on an AttachType
Feature attachFeature = null;
Type attachType;
if (attachTypeName != null) {
attachType = CasUtil.getType(aJcas.getCas(), attachTypeName);
attachFeature = attachType.getFeatureByBaseName(attachTypeName);
}
for (Token token : select(aJcas, Token.class)) {
AnnotationFS newAnnotation = aJcas.getCas().createAnnotation(type, token.getBegin(), token.getEnd());
newAnnotation.setFeatureValueFromString(feature, aLabelValues.get(i));
i++;
if (attachFeature != null) {
token.setFeatureValue(attachFeature, newAnnotation);
}
aJcas.getCas().addFsToIndexes(newAnnotation);
}
}
}
Aggregations