use of org.apache.uima.cas.FeatureStructure in project webanno by webanno.
the class AnnotationDetailEditorPanel method getAttachedRels.
public Set<AnnotationFS> getAttachedRels(AnnotationFS aFs, AnnotationLayer aLayer) {
CAS cas = aFs.getCAS();
Set<AnnotationFS> toBeDeleted = new HashSet<>();
for (AnnotationLayer relationLayer : annotationService.listAttachedRelationLayers(aLayer)) {
ArcAdapter relationAdapter = (ArcAdapter) annotationService.getAdapter(relationLayer);
Type relationType = CasUtil.getType(cas, relationLayer.getName());
Feature sourceFeature = relationType.getFeatureByBaseName(relationAdapter.getSourceFeatureName());
Feature targetFeature = relationType.getFeatureByBaseName(relationAdapter.getTargetFeatureName());
// This code is already prepared for the day that relations can go between
// different layers and may have different attach features for the source and
// target layers.
Feature relationSourceAttachFeature = null;
Feature relationTargetAttachFeature = null;
if (relationAdapter.getAttachFeatureName() != null) {
relationSourceAttachFeature = sourceFeature.getRange().getFeatureByBaseName(relationAdapter.getAttachFeatureName());
relationTargetAttachFeature = targetFeature.getRange().getFeatureByBaseName(relationAdapter.getAttachFeatureName());
}
for (AnnotationFS relationFS : CasUtil.select(cas, relationType)) {
// Here we get the annotations that the relation is pointing to in the UI
FeatureStructure sourceFS;
if (relationSourceAttachFeature != null) {
sourceFS = relationFS.getFeatureValue(sourceFeature).getFeatureValue(relationSourceAttachFeature);
} else {
sourceFS = relationFS.getFeatureValue(sourceFeature);
}
FeatureStructure targetFS;
if (relationTargetAttachFeature != null) {
targetFS = relationFS.getFeatureValue(targetFeature).getFeatureValue(relationTargetAttachFeature);
} else {
targetFS = relationFS.getFeatureValue(targetFeature);
}
if (isSame(sourceFS, aFs) || isSame(targetFS, aFs)) {
toBeDeleted.add(relationFS);
LOG.debug("Deleted relation [" + getAddr(relationFS) + "] from layer [" + relationLayer.getName() + "]");
}
}
}
return toBeDeleted;
}
use of org.apache.uima.cas.FeatureStructure in project webanno by webanno.
the class AnnotationDetailEditorPanel method populateTagsBasedOnRules.
/**
* Adds and sorts tags based on Constraints rules
*/
private void populateTagsBasedOnRules(JCas aJCas, FeatureState aModel) {
LOG.trace("populateTagsBasedOnRules(feature: " + aModel.feature.getUiName() + ")");
AnnotatorState state = getModelObject();
// Add values from rules
String restrictionFeaturePath;
switch(aModel.feature.getLinkMode()) {
case WITH_ROLE:
restrictionFeaturePath = aModel.feature.getName() + "." + aModel.feature.getLinkTypeRoleFeatureName();
break;
case NONE:
restrictionFeaturePath = aModel.feature.getName();
break;
default:
throw new IllegalArgumentException("Unsupported link mode [" + aModel.feature.getLinkMode() + "] on feature [" + aModel.feature.getName() + "]");
}
aModel.indicator.reset();
// Fetch possible values from the constraint rules
List<PossibleValue> possibleValues;
try {
FeatureStructure featureStructure = selectByAddr(aJCas, state.getSelection().getAnnotation().getId());
Evaluator evaluator = new ValuesGenerator();
// Only show indicator if this feature can be affected by Constraint rules!
aModel.indicator.setAffected(evaluator.isThisAffectedByConstraintRules(featureStructure, restrictionFeaturePath, state.getConstraints()));
possibleValues = evaluator.generatePossibleValues(featureStructure, restrictionFeaturePath, state.getConstraints());
LOG.debug("Possible values for [" + featureStructure.getType().getName() + "] [" + restrictionFeaturePath + "]: " + possibleValues);
} catch (Exception e) {
error("Unable to evaluate constraints: " + ExceptionUtils.getRootCauseMessage(e));
LOG.error("Unable to evaluate constraints: " + e.getMessage(), e);
possibleValues = new ArrayList<>();
}
// Fetch actual tagset
List<Tag> valuesFromTagset = annotationService.listTags(aModel.feature.getTagset());
// First add tags which are suggested by rules and exist in tagset
List<Tag> tagset = compareSortAndAdd(possibleValues, valuesFromTagset, aModel.indicator);
// Then add the remaining tags
for (Tag remainingTag : valuesFromTagset) {
if (!tagset.contains(remainingTag)) {
tagset.add(remainingTag);
}
}
// Record the possible values and the (re-ordered) tagset in the feature state
aModel.possibleValues = possibleValues;
aModel.tagset = tagset;
}
use of org.apache.uima.cas.FeatureStructure in project webanno by webanno.
the class WebannoTsv3Reader method addChainAnnotations.
/**
* The individual link annotations are stored in a {@link TreeMap} (chainAnnosPerTye) with chain
* number and link number references, sorted in an ascending order <br>
* Iterate over each chain number and link number references and construct the chain.
*/
private void addChainAnnotations(JCas aJCas) {
for (Type linkType : chainAnnosPerTyep.keySet()) {
for (int chainNo : chainAnnosPerTyep.get(linkType).keySet()) {
Type chainType = aJCas.getCas().getTypeSystem().getType(linkType.getName().substring(0, linkType.getName().length() - 4) + CHAIN);
Feature firstF = chainType.getFeatureByBaseName(FIRST);
Feature nextF = linkType.getFeatureByBaseName(NEXT);
FeatureStructure chain = aJCas.getCas().createFS(chainType);
aJCas.addFsToIndexes(chain);
AnnotationFS firstFs = chainAnnosPerTyep.get(linkType).get(chainNo).get(1);
AnnotationFS linkFs = firstFs;
chain.setFeatureValue(firstF, firstFs);
for (int i = 2; i <= chainAnnosPerTyep.get(linkType).get(chainNo).size(); i++) {
linkFs.setFeatureValue(nextF, chainAnnosPerTyep.get(linkType).get(chainNo).get(i));
linkFs = chainAnnosPerTyep.get(linkType).get(chainNo).get(i);
}
}
}
}
use of org.apache.uima.cas.FeatureStructure in project webanno by webanno.
the class WebannoTsv3Writer method setSpanAnnoPerFeature.
private void setSpanAnnoPerFeature(Map<AnnotationUnit, List<List<String>>> aAnnotationsPertype, Type aType, AnnotationFS aFs, AnnotationUnit aUnit, boolean aIsMultiToken, boolean aIsFirst) {
List<String> annoPerFeatures = new ArrayList<>();
featurePerLayer.putIfAbsent(aType.getName(), new LinkedHashSet<>());
int ref = getRefId(aType, aFs, aUnit);
if (ambigUnits.get(aType.getName()).get(getFirstUnit(aUnit)) != null && ambigUnits.get(aType.getName()).get(getFirstUnit(aUnit)).equals(false)) {
ref = 0;
}
if (ambigUnits.get(aType.getName()).get(getFirstUnit(aUnit)) == null && ambigUnits.get(aType.getName()).get(aUnit).equals(false)) {
ref = 0;
}
for (Feature feature : aType.getFeatures()) {
if (feature.toString().equals("uima.cas.AnnotationBase:sofa") || feature.toString().equals("uima.tcas.Annotation:begin") || feature.toString().equals("uima.tcas.Annotation:end") || feature.getShortName().equals(GOVERNOR) || feature.getShortName().equals(DEPENDENT) || feature.getShortName().equals(FIRST) || feature.getShortName().equals(NEXT)) {
continue;
}
// if slot feature
if (slotFeatures != null && slotFeatures.contains(feature.getName())) {
if (aFs.getFeatureValue(feature) != null) {
ArrayFS array = (ArrayFS) aFs.getFeatureValue(feature);
StringBuilder sbRole = new StringBuilder();
StringBuilder sbTarget = new StringBuilder();
for (FeatureStructure linkFS : array.toArray()) {
String role = linkFS.getStringValue(linkFS.getType().getFeatureByBaseName("role"));
AnnotationFS targetFs = (AnnotationFS) linkFS.getFeatureValue(linkFS.getType().getFeatureByBaseName("target"));
Type tType = targetFs.getType();
AnnotationUnit firstUnit = getFirstUnit(targetFs);
ref = getRefId(tType, targetFs, firstUnit);
// Check if the target is ambiguous or not
if (ambigUnits.get(tType.getName()).get(firstUnit).equals(false)) {
ref = 0;
}
if (role == null) {
role = "*";
} else {
// Escape special character
role = replaceEscapeChars(role);
}
if (sbRole.length() < 1) {
sbRole.append(role);
// record the actual target type column number if slot target is
// uima.tcas.Annotation
int targetTypeNumber = 0;
if (slotFeatureTypes.get(feature).getName().equals(CAS.TYPE_NAME_ANNOTATION)) {
targetTypeNumber = layerMaps.get(tType);
}
sbTarget.append(unitsLineNumber.get(firstUnit)).append(targetTypeNumber == 0 ? "" : "-" + targetTypeNumber).append(ref > 0 ? "[" + ref + "]" : "");
} else {
sbRole.append(";");
sbTarget.append(";");
sbRole.append(role);
int targetTypeNumber = 0;
if (slotFeatureTypes.get(feature).getName().equals(CAS.TYPE_NAME_ANNOTATION)) {
targetTypeNumber = layerMaps.get(tType);
}
sbTarget.append(unitsLineNumber.get(firstUnit)).append(targetTypeNumber == 0 ? "" : "-" + targetTypeNumber).append(ref > 0 ? "[" + ref + "]" : "");
}
}
annoPerFeatures.add(sbRole.toString().isEmpty() ? "_" : sbRole.toString());
annoPerFeatures.add(sbTarget.toString().isEmpty() ? "_" : sbTarget.toString());
} else {
// setting it to null
annoPerFeatures.add("_");
annoPerFeatures.add("_");
}
featurePerLayer.get(aType.getName()).add(ROLE + feature.getName() + "_" + slotLinkTypes.get(feature.getName()));
featurePerLayer.get(aType.getName()).add(slotFeatureTypes.get(feature).getName());
} else {
String annotation = aFs.getFeatureValueAsString(feature);
if (annotation == null) {
annotation = "*";
} else {
// Escape special character
annotation = replaceEscapeChars(annotation);
}
annotation = annotation + (ref > 0 ? "[" + ref + "]" : "");
// only add BIO markers to multiple annotations
setAnnoFeature(aIsMultiToken, aIsFirst, annoPerFeatures, annotation);
featurePerLayer.get(aType.getName()).add(feature.getShortName());
}
}
aAnnotationsPertype.putIfAbsent(aUnit, new ArrayList<>());
// If the layer do not have a feature at all, add dummy * as a place holder
if (annoPerFeatures.size() == 0) {
setAnnoFeature(aIsMultiToken, aIsFirst, annoPerFeatures, "*" + (ref > 0 ? "[" + ref + "]" : ""));
}
aAnnotationsPertype.get(aUnit).add(annoPerFeatures);
}
use of org.apache.uima.cas.FeatureStructure in project webanno by webanno.
the class Tsv3XCasDocumentBuilder method scanUnitForActiveColumns.
private static void scanUnitForActiveColumns(TsvUnit aUnit) {
for (TsvColumn col : aUnit.getDocument().getSchema().getColumns()) {
List<AnnotationFS> annotationsForColumn = aUnit.getAnnotationsForColumn(col);
if (!annotationsForColumn.isEmpty()) {
if (!PLACEHOLDER.equals(col.featureType)) {
aUnit.getDocument().activateColumn(col);
}
// actual annotation.
if (RELATION.equals(col.layerType) && RELATION_REF.equals(col.featureType)) {
AnnotationFS annotation = annotationsForColumn.get(0);
FeatureStructure target = FSUtil.getFeature(annotation, FEAT_REL_SOURCE, FeatureStructure.class);
if (target == null) {
throw new IllegalStateException("Relation does not have its source feature (" + FEAT_REL_SOURCE + ") set: " + annotation);
}
if (col.uimaType.getName().equals(Dependency.class.getName())) {
// COMPATIBILITY NOTE:
// WebAnnoTsv3Writer hard-changes the target type for DKPro Core
// Dependency annotations from Token to POS - the reason is not really
// clear. Probably because the Dependency relations in the WebAnno UI
// attach to POS (Token's are not visible as annotations in the UI).
col.setTargetTypeHint(aUnit.getDocument().getJCas().getTypeSystem().getType(POS.class.getName()));
} else {
col.setTargetTypeHint(target.getType());
}
}
}
}
}
Aggregations