Java Code Examples for org.apache.uima.cas.text.AnnotationFS#getType()

The following examples show how to use org.apache.uima.cas.text.AnnotationFS#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Tsv3XDeserializer.java    From webanno with Apache License 2.0 6 votes vote down vote up
private void setPrimitiveValue(TsvColumn aCol, AnnotationFS aAnnotation, String aValue)
{
    // Unescape value - this needs to be done after extracting the disambiguation ID and
    // after determining whether the values is a null value.
    if (!NULL_VALUE.equals(aValue)) {
        String value = Escaping.unescapeValue(aValue);
        Feature feat = aAnnotation.getType()
                .getFeatureByBaseName(aCol.uimaFeature.getShortName());
        
        if (feat == null) {
            throw new IllegalArgumentException(
                    "CAS type [" + aAnnotation.getType() + "] does not have a feature called ["
                            + aCol.uimaFeature.getShortName() + "]");
        }
        
        aAnnotation.setFeatureValueFromString(feat, value);
    }
}
 
Example 2
Source File: RelationAttachmentBehavior.java    From webanno with Apache License 2.0 6 votes vote down vote up
public static FeatureStructure[] resolve(RelationAdapter aAdapter, AnnotationFS aRelation)
{
    Type type = aRelation.getType();
    Feature targetFeature = type.getFeatureByBaseName(aAdapter.getTargetFeatureName());
    Feature sourceFeature = type.getFeatureByBaseName(aAdapter.getSourceFeatureName());
    
    FeatureStructure targetFs;
    FeatureStructure sourceFs;
    
    if (aAdapter.getAttachFeatureName() != null) {
        Type spanType = getType(aRelation.getCAS(), aAdapter.getAttachTypeName());
        Feature arcSpanFeature = spanType.getFeatureByBaseName(aAdapter.getAttachFeatureName());
        targetFs = aRelation.getFeatureValue(targetFeature).getFeatureValue(arcSpanFeature);
        sourceFs = aRelation.getFeatureValue(sourceFeature).getFeatureValue(arcSpanFeature);
    }
    else {
        targetFs = aRelation.getFeatureValue(targetFeature);
        sourceFs = aRelation.getFeatureValue(sourceFeature);
    }
    
    return new FeatureStructure[] { sourceFs, targetFs };
}
 
Example 3
Source File: FeatureStructureBrowserViewPage.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
protected void addedAnnotation(Collection<AnnotationFS> annotations) {

  final List<ModelFeatureStructure> featureStructureList = new LinkedList<>();

  for (AnnotationFS annotation : annotations) {
    if (annotation.getType() == mCurrentType) {
      featureStructureList.add(new ModelFeatureStructure(mDocument, annotation));
    }
  }

  Display.getDefault().syncExec(new Runnable() {
    @Override
    public void run() {
      mFSList.add(featureStructureList.toArray());
    }
  });
}
 
Example 4
Source File: FeatureStructureBrowserViewPage.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
protected void removedAnnotation(Collection<AnnotationFS> annotations) {

  final List<ModelFeatureStructure> featureStructureList = new LinkedList<>();

  for (AnnotationFS annotation : annotations) {
    if (annotation.getType() == mCurrentType) {
      featureStructureList.add(new ModelFeatureStructure(mDocument, annotation));
    }
  }

  Display.getDefault().syncExec(new Runnable() {
    @Override
    public void run() {
      mFSList.remove(featureStructureList.toArray());
    }
  });
}
 
Example 5
Source File: DebugFSLogicalStructure.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public static Object getDebugLogicalStructure_Features(AnnotationFS fs) {
  boolean isJCasClass = false;
  Type fsType = fs.getType();
  String typeName = fsType.getName();

  if (fs.getClass().getName().equals(typeName)) { // true for JCas cover classes
    isJCasClass = true;
  }

  DebugNameValuePair[] result = new DebugNameValuePair[3 + (isJCasClass ? 0 : 1)]; // slot for
  // type name
  // if not JCas
  int i = 0;
  if (!isJCasClass) {
    result[i++] = new DebugNameValuePair("CasType", typeName);
  }
  fillFeatures(result, i, fs, fsType.getFeatures());
  return result;
}
 
Example 6
Source File: RemoveDanglingRelationsRepair.java    From webanno with Apache License 2.0 5 votes vote down vote up
@Override
public void repair(Project aProject, CAS aCas, List<LogMessage> aMessages)
{
    Set<FeatureStructure> nonIndexed = getNonIndexedFSes(aCas);
    
    Set<FeatureStructure> toDelete = new LinkedHashSet<>();
    
    for (AnnotationFS fs : aCas.getAnnotationIndex()) {
        Type t = fs.getType();
        
        Feature sourceFeat = t.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE);
        Feature targetFeat = t.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET);
        
        // Is this a relation?
        if (!(sourceFeat != null && targetFeat != null)) {
            continue;
        }
        
        FeatureStructure source = fs.getFeatureValue(sourceFeat);
        FeatureStructure target = fs.getFeatureValue(targetFeat);
        
        // Does it point to deleted spans?
        if (nonIndexed.contains(source) || nonIndexed.contains(target)) {
            toDelete.add(fs);
        }
    }

    // Delete those relations that pointed to deleted spans
    if (!toDelete.isEmpty()) {
        toDelete.forEach(aCas::removeFsFromIndexes);
        aMessages.add(new LogMessage(this, LogLevel.INFO, "Removed [%d] dangling relations.",
                nonIndexed.size()));
    }
}
 
Example 7
Source File: CasMerge.java    From webanno with Apache License 2.0 5 votes vote down vote up
private static List<AnnotationFS> selectCandidateRelationsAt(CAS aTargetCas,
        AnnotationFS aSourceFs, AnnotationFS aSourceOriginFs, AnnotationFS aSourceTargetFs)
{
    Type type = aSourceFs.getType();
    Feature sourceFeat = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE);
    Feature targetFeat = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET);
    return selectCovered(aTargetCas, type, aSourceFs.getBegin(), aSourceFs.getEnd()).stream()
            .filter(fs -> fs.getFeatureValue(sourceFeat).equals(aSourceOriginFs)
                    && fs.getFeatureValue(targetFeat).equals(aSourceTargetFs))
            .collect(Collectors.toList());
}
 
Example 8
Source File: LowerRightAnnotationSideAction.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Lower right annotation side.
 *
 * @param document the document
 * @param annotation the annotation
 */
public static void lowerRightAnnotationSide(ICasDocument document, AnnotationFS annotation) {
  
  Type annotationType = annotation.getType();
  Feature endFeature = annotationType.getFeatureByBaseName("end");
  
  if (annotation.getBegin() < annotation.getEnd()) {
    annotation.setIntValue(endFeature, annotation.getEnd() - 1);
  }
  
  document.update(annotation);
}
 
Example 9
Source File: WideLeftAnnotationSideAction.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Widens the annotation and sends and sends an update notification
 * to the provided document.
 *
 * @param document the document
 * @param annotation the annotation
 */
public static void wideLeftAnnotationSide(ICasDocument document, AnnotationFS annotation) {
  Type annotationType = annotation.getType();
  Feature beginFeature = annotationType.getFeatureByBaseName("begin");

  if (annotation.getBegin() > 0) {
    annotation.setIntValue(beginFeature, annotation.getBegin() - 1);
  }

  document.update(annotation);
}
 
Example 10
Source File: WideRightAnnotationSideAction.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Wide right annotation side.
 *
 * @param document the document
 * @param annotation the annotation
 */
public static void wideRightAnnotationSide(ICasDocument document, AnnotationFS annotation) {
  Type annotationType = annotation.getType();
  Feature endFeature = annotationType.getFeatureByBaseName("end");
  
  if (annotation.getEnd() < document.getCAS().getDocumentText().length()) {
    annotation.setIntValue(endFeature, annotation.getEnd() + 1);
  }
  
  document.update(annotation);
}
 
Example 11
Source File: LowerLeftAnnotationSideAction.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Lower left annotation side.
 *
 * @param document the document
 * @param annotation the annotation
 */
public static void lowerLeftAnnotationSide(ICasDocument document, AnnotationFS annotation) {
  Type annotationType = annotation.getType();
  Feature beginFeature = annotationType.getFeatureByBaseName("begin");

  if (annotation.getBegin() < annotation.getEnd()) {
    annotation.setIntValue(beginFeature, annotation.getBegin() + 1);
  }

  document.update(annotation);
}
 
Example 12
Source File: CurationEditorExtension.java    From inception with Apache License 2.0 4 votes vote down vote up
/**
 * Save annotation identified by aVID from user CAS to given curator's CAS
 */
private void saveAnnotation(String aAction, AnnotationActionHandler aPanel,
        AnnotatorState aState, AjaxRequestTarget aTarget, CAS aTargetCas, VID aVID)
    throws IOException, AnnotationException
{
    AnnotationLayer layer = annotationService.getLayer(aVID.getLayerId());
    
    // get user CAS and annotation (to be merged into curator's)
    SourceDocument doc = aState.getDocument();
    String srcUser = ((CurationVID) aVID).getUsername();
    
    if (!documentService.existsAnnotationDocument(doc, srcUser)) {
        log.error(
              String.format("Source CAS of %s for curation not found", srcUser));
        return;
    }
    
    CAS srcCas = documentService.readAnnotationCas(doc, srcUser);
    AnnotationFS sourceAnnotation = selectAnnotationByAddr(srcCas, aVID.getId());
    
    // merge into curator's CAS depending on annotation type (span or arc)
    CasMerge casMerge = new CasMerge(annotationService);
    CasMergeOperationResult mergeResult;
    if (ACTION_SELECT_SPAN.equals(aAction.toString())) {
        mergeResult = casMerge.mergeSpanAnnotation(doc, srcUser, layer, aTargetCas,
                sourceAnnotation, layer.isAllowStacking());
        // open created/updates FS in annotation detail editorpanel
        aState.getSelection().selectSpan(new VID(mergeResult.getResultFSAddress()), aTargetCas,
                sourceAnnotation.getBegin(), sourceAnnotation.getEnd());
        
    }
    else if (ACTION_SELECT_ARC.equals(aAction.toString())) {
        // this is a slot arc
        if (aVID.isSlotSet()) {
            TypeAdapter adapter = annotationService.getAdapter(layer);
            AnnotationFeature feature = adapter.listFeatures().stream().sequential()
                    .skip(aVID.getAttribute()).findFirst().get();

            mergeResult = casMerge.mergeSlotFeature(doc, srcUser, layer, aTargetCas,
                    sourceAnnotation, feature.getName(), aVID.getSlot());
            // open created/updates FS in annotation detail editorpanel
            aState.getSelection().selectSpan(new VID(mergeResult.getResultFSAddress()),
                    aTargetCas, sourceAnnotation.getBegin(), sourceAnnotation.getEnd());
        }
        // normal relation annotation arc is clicked
        else {
            mergeResult = casMerge.mergeRelationAnnotation(doc, srcUser, layer, aTargetCas,
                    sourceAnnotation, layer.isAllowStacking());
            // open created/updates FS in annotation detail editorpanel 
            AnnotationFS mergedAnno = selectAnnotationByAddr(aTargetCas,
                    mergeResult.getResultFSAddress());
            Type depType = mergedAnno.getType();
            Feature originFeat = depType.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE);
            Feature targetFeat = depType.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET);
            AnnotationFS originFS = (AnnotationFS) mergedAnno.getFeatureValue(originFeat);
            AnnotationFS targetFS = (AnnotationFS) mergedAnno.getFeatureValue(targetFeat);
            aState.getSelection().selectArc(new VID(mergeResult.getResultFSAddress()), originFS,
                    targetFS);
        }
    }
    
    aPanel.actionSelect(aTarget);
    aPanel.actionCreateOrUpdate(aTarget, aTargetCas);  //should also update timestamps
}
 
Example 13
Source File: WebannoTsv3Writer.java    From webanno with Apache License 2.0 4 votes vote down vote up
private void setChainAnnotation(JCas aJCas)
{
    for (String l : chainLayers) {
        if (l.equals(Token.class.getName())) {
            continue;
        }

        Map<AnnotationUnit, List<List<String>>> annotationsPertype = null;
        Type type = getType(aJCas.getCas(), l + CHAIN);
        Feature chainFirst = type.getFeatureByBaseName(FIRST);
        int chainNo = 1;
        for (FeatureStructure chainFs : selectFS(aJCas.getCas(), type)) {
            AnnotationFS linkFs = (AnnotationFS) chainFs.getFeatureValue(chainFirst);
            AnnotationUnit unit = getUnit(linkFs.getBegin(), linkFs.getEnd(),
                    linkFs.getCoveredText());
            Type lType = linkFs.getType();

            // this is the layer with annotations
            l = lType.getName();
            if (annotationsPerPostion.get(l) == null) {
                annotationsPertype = new HashMap<>();

            }
            else {
                annotationsPertype = annotationsPerPostion.get(l);
            }
            Feature linkNext = linkFs.getType().getFeatureByBaseName(NEXT);
            int linkNo = 1;
            while (linkFs != null) {
                AnnotationFS nextLinkFs = (AnnotationFS) linkFs.getFeatureValue(linkNext);
                if (nextLinkFs != null) {
                    addChinFeatureAnno(annotationsPertype, lType, linkFs, unit, linkNo,
                            chainNo);
                }
                else {
                    addChinFeatureAnno(annotationsPertype, lType, linkFs, unit, linkNo,
                            chainNo);
                }
                linkFs = nextLinkFs;
                linkNo++;
                if (nextLinkFs != null) {
                    unit = getUnit(linkFs.getBegin(), linkFs.getEnd(), linkFs.getCoveredText());
                }
            }
            if (annotationsPertype.keySet().size() > 0) {
                annotationsPerPostion.put(l, annotationsPertype);
            }
            chainNo++;
        }
    }
}
 
Example 14
Source File: WebannoTsv3Writer.java    From webanno with Apache License 2.0 4 votes vote down vote up
private void setRelationAnnotation(JCas aJCas)
{
    for (String l : relationLayers) {
        if (l.equals(Token.class.getName())) {
            continue;
        }
        Map<AnnotationUnit, List<List<String>>> annotationsPertype;
        if (annotationsPerPostion.get(l) == null) {
            annotationsPertype = new HashMap<>();

        }
        else {
            annotationsPertype = annotationsPerPostion.get(l);
        }
        Type type = getType(aJCas.getCas(), l);
        Feature dependentFeature = null;
        Feature governorFeature = null;

        List<Feature> features = type.getFeatures();
        Collections.sort(features, (a, b) -> 
                StringUtils.compare(a.getShortName(), b.getShortName()));
        for (Feature feature : features) {
            if (feature.getShortName().equals(DEPENDENT)) {

                // check if the dependent is
                dependentFeature = feature;
            }
            if (feature.getShortName().equals(GOVERNOR)) {
                governorFeature = feature;
            }
        }
        for (AnnotationFS fs : CasUtil.select(aJCas.getCas(), type)) {
            AnnotationFS depFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
            AnnotationFS govFs = (AnnotationFS) fs.getFeatureValue(governorFeature);

            Type govType = govFs.getType();

            AnnotationUnit govUnit = getFirstUnit(
                    getUnit(govFs.getBegin(), govFs.getEnd(), govFs.getCoveredText()));
            if (ambigUnits.get(govType.getName()).get(govUnit) == null) {
                govUnit = getUnit(govFs.getBegin(), govFs.getEnd(), govFs.getCoveredText());
            }

            AnnotationUnit depUnit = getFirstUnit(
                    getUnit(depFs.getBegin(), depFs.getEnd(), depFs.getCoveredText()));
            if (ambigUnits.get(govType.getName()).get(depUnit) == null) {
                depUnit = getUnit(depFs.getBegin(), depFs.getEnd(), depFs.getCoveredText());
            }
            // Since de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency is over
            // Over POS anno which itself attached to Token, we need the POS type here

            if (type.getName().equals(Dependency.class.getName())) {
                govType = aJCas.getCas().getTypeSystem().getType(POS.class.getName());
            }

            int govRef = 0;
            int depRef = 0;

            // For that unit test case only, where annotations are on Tokens.
            // The WebAnno world do not ever process Token as an annotation
            if (!govType.getName().equals(Token.class.getName())
                    && ambigUnits.get(govType.getName()).get(govUnit).equals(true)) {
                govRef = annotaionRefPerType.get(govType).get(govFs);
            }

            if (!govType.getName().equals(Token.class.getName())
                    && ambigUnits.get(govType.getName()).get(depUnit).equals(true)) {
                depRef = annotaionRefPerType.get(govType).get(depFs);
            }

            setRelationAnnoPerFeature(annotationsPertype, type, fs, depUnit, govUnit, govRef,
                    depRef, govType);

        }
        if (annotationsPertype.keySet().size() > 0) {
            annotationsPerPostion.put(l, annotationsPertype);
        }
    }
}