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

The following examples show how to use org.apache.uima.cas.text.AnnotationFS#getCAS() . 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: AutomationUtil.java    From webanno with Apache License 2.0 6 votes vote down vote up
private static List<String> getAnnotation(TypeAdapter aAdapter, AnnotationFS aSentence,
        AnnotationFeature aFeature)
{
    CAS cas = aSentence.getCAS();
    
    Type type = getType(cas, aAdapter.getAnnotationTypeName());
    List<String> annotations = new ArrayList<>();

    for (AnnotationFS token : selectTokensCovered(aSentence)) {
        List<AnnotationFS> tokenLevelAnnotations = selectCovered(type, token);
        if (tokenLevelAnnotations.size() > 0) {
            AnnotationFS anno = tokenLevelAnnotations.get(0);
            Feature labelFeature = anno.getType().getFeatureByBaseName(aFeature.getName());
            annotations.add(anno.getFeatureValueAsString(labelFeature));
        }
        else {
            annotations.add(NILL);
        }
    }
    return annotations;
}
 
Example 2
Source File: AnnotationDetailEditorPanel.java    From webanno with Apache License 2.0 6 votes vote down vote up
private static Set<AnnotationFS> getAttachedSpans(AnnotationSchemaService aAS, AnnotationFS aFs,
        AnnotationLayer aLayer)
{
    CAS cas = aFs.getCAS();
    Set<AnnotationFS> attachedSpans = new HashSet<>();
    TypeAdapter adapter = aAS.getAdapter(aLayer);
    if (adapter instanceof SpanAdapter && aLayer.getAttachType() != null) {
        Type spanType = CasUtil.getType(cas, aLayer.getAttachType().getName());
        Feature attachFeature = spanType.getFeatureByBaseName(aLayer.getAttachFeature()
            .getName());
        final Type type = spanType;

        for (AnnotationFS attachedFs : selectAt(cas, type, aFs.getBegin(), aFs.getEnd())) {
            if (isSame(attachedFs.getFeatureValue(attachFeature), aFs)) {
                attachedSpans.add(attachedFs);
            }
        }
    }
    return attachedSpans;
}
 
Example 3
Source File: WebAnnoTsv3WriterTestBase.java    From webanno with Apache License 2.0 5 votes vote down vote up
private static void makeChainHead(Type aType, AnnotationFS first)
{
    CAS cas = first.getCAS();
    FeatureStructure h = cas.createFS(aType);
    FSUtil.setFeature(h, "first", first);
    cas.addFsToIndexes(h);
}
 
Example 4
Source File: AnnotationDetailEditorPanel.java    From webanno with Apache License 2.0 5 votes vote down vote up
private Set<FeatureStructure> getAttachedLinks(AnnotationFS aFs, AnnotationLayer aLayer)
{
    CAS cas = aFs.getCAS();
    Set<FeatureStructure> attachedLinks = new HashSet<>();
    TypeAdapter adapter = annotationService.getAdapter(aLayer);
    if (adapter instanceof SpanAdapter) {
        for (AnnotationFeature linkFeature : annotationService
                .listAttachedLinkFeatures(aLayer)) {
            if (MultiValueMode.ARRAY.equals(linkFeature.getMultiValueMode())
                    && LinkMode.WITH_ROLE.equals(linkFeature.getLinkMode())) {
                // Fetch slot hosts that could link to the current FS and check if any of
                // them actually links to the current FS
                Type linkHost = CasUtil.getType(cas, linkFeature.getLayer().getName());
                for (FeatureStructure linkFS : CasUtil.selectFS(cas, linkHost)) {
                    List<LinkWithRoleModel> links = adapter.getFeatureValue(linkFeature,
                            linkFS);
                    for (int li = 0; li < links.size(); li++) {
                        LinkWithRoleModel link = links.get(li);
                        AnnotationFS linkTarget = selectByAddr(cas, AnnotationFS.class,
                                link.targetAddr);
                        // If the current annotation fills a slot, then add the slot host to
                        // our list of attached links.
                        if (isSame(linkTarget, aFs)) {
                            attachedLinks.add(linkFS);
                        }
                    }
                }
            }
        }
    }
    return attachedLinks;
}
 
Example 5
Source File: AnnotationDetailEditorPanel.java    From webanno with Apache License 2.0 4 votes vote down vote up
private static Set<AnnotationFS> getAttachedRels(AnnotationSchemaService aAS, AnnotationFS aFs,
        AnnotationLayer aLayer)
{
    CAS cas = aFs.getCAS();
    Set<AnnotationFS> toBeDeleted = new HashSet<>();
    for (AnnotationLayer relationLayer : aAS.listAttachedRelationLayers(aLayer)) {
        RelationAdapter relationAdapter = (RelationAdapter) aAS.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;
}
 
Example 6
Source File: JCasUtil.java    From uima-uimafit with Apache License 2.0 3 votes vote down vote up
/**
 * Convenience method to get a sub-iterator for the specified type.
 * 
 * @param <T>
 *          the iteration type.
 * @param container
 *          the containing annotation.
 * @param type
 *          the type.
 * @param ambiguous
 *          If set to <code>false</code>, resulting iterator will be unambiguous.
 * @param strict
 *          Controls if annotations that overlap to the right are considered in or out.
 * @return A sub-iterator.
 * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a>
 */
@SuppressWarnings("unchecked")
public static <T extends AnnotationFS> Iterator<T> iterator(AnnotationFS container,
        Class<T> type, boolean ambiguous, boolean strict) {
  CAS cas = container.getCAS();
  return ((AnnotationIndex<T>) cas.getAnnotationIndex(CasUtil.getType(cas, type))).subiterator(
          container, ambiguous, strict);
}
 
Example 7
Source File: JCasUtil.java    From uima-uimafit with Apache License 2.0 3 votes vote down vote up
/**
 * Return an annotation preceding or following of a given reference annotation.
 * 
 * @param <T>
 *          the JCas type.
 * @param aType
 *          a type.
 * @param annotation
 *          anchor annotation
 * @param index
 *          relative position to access. A negative value selects a preceding annotation while a
 *          positive number selects a following annotation.
 * @return the addressed annotation.
 * @throws IndexOutOfBoundsException
 *           if the relative index points beyond the type index bounds.
 * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a>
 */
@SuppressWarnings("unchecked")
public static <T extends Annotation> T selectSingleRelative(Class<T> aType,
        AnnotationFS annotation, int index) {
  CAS cas = annotation.getCAS();
  Type t = CasUtil.getType(cas, aType);
  return (T) CasUtil.selectSingleRelative(cas, t, annotation, index);
}