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

The following examples show how to use org.apache.uima.cas.text.AnnotationFS#equals() . 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: RelationOverlapBehavior.java    From webanno with Apache License 2.0 6 votes vote down vote up
private void overlappingOrStackingRelations(Collection<AnnotationFS> aRelations,
        Feature sourceFeature, Feature targetFeature, Collection<AnnotationFS> aStacking,
        Collection<AnnotationFS> aOverlapping)
{
    for (AnnotationFS rel1 : aRelations) {
        for (AnnotationFS rel2 : aRelations) {
            if (rel1.equals(rel2)) {
                continue;
            }
            
            if (stacking(rel1, rel2, sourceFeature, targetFeature)) {
                aStacking.add(rel1);
                aStacking.add(rel2);
            }
            else if (overlapping(rel1, rel2, sourceFeature, targetFeature)) {
                aOverlapping.add(rel1);
                aOverlapping.add(rel2);
            }
        }
    }
}
 
Example 2
Source File: RelationOverlapBehavior.java    From webanno with Apache License 2.0 6 votes vote down vote up
private Set<AnnotationFS> overlappingNonStackingRelations(Collection<AnnotationFS> aRelations,
        Feature sourceFeature, Feature targetFeature)
{
    Set<AnnotationFS> overlapping = new HashSet<>();
    for (AnnotationFS rel1 : aRelations) {
        for (AnnotationFS rel2 : aRelations) {
            if (rel1.equals(rel2)) {
                continue;
            }
             
            if (
                    overlapping(rel1, rel2, sourceFeature, targetFeature) && 
                    !stacking(rel1, rel2, sourceFeature, targetFeature)
            ) {
                overlapping.add(rel1);
                overlapping.add(rel2);
            }
        }
    }
    return overlapping;
}
 
Example 3
Source File: SpanOverlapBehavior.java    From webanno with Apache License 2.0 6 votes vote down vote up
private void overlappingOrStackingSpans(Collection<AnnotationFS> aSpans,
        Collection<AnnotationFS> aStacking, Collection<AnnotationFS> aOverlapping)
{
    for (AnnotationFS span1 : aSpans) {
        for (AnnotationFS span2 : aSpans) {
            if (span1.equals(span2)) {
                continue;
            }
            
            if (stacking(span1, span2)) {
                aStacking.add(span1);
                aStacking.add(span2);
            }
            else if (overlapping(span1, span2)) {
                aOverlapping.add(span1);
                aOverlapping.add(span2);
            }
        }
    }
}
 
Example 4
Source File: SpanOverlapBehavior.java    From webanno with Apache License 2.0 6 votes vote down vote up
private Set<AnnotationFS> overlappingNonStackingSpans(Collection<AnnotationFS> aSpans)
{
    Set<AnnotationFS> overlapping = new HashSet<>();
    for (AnnotationFS fs1 : aSpans) {
        for (AnnotationFS fs2 : aSpans) {
            if (fs1.equals(fs2)) {
                continue;
            }
            
            if (overlapping(fs1, fs2) && !stacking(fs1, fs2)) {
                overlapping.add(fs1);
                overlapping.add(fs2);
            }
        }
    }
    return overlapping;
}
 
Example 5
Source File: AutomationUtil.java    From webanno with Apache License 2.0 5 votes vote down vote up
public static void clearAnnotations(CAS aCas, AnnotationFeature aFeature)
    throws IOException
{
    // Check if annotation layer is attached to another layer
    String attachTypeName = aFeature.getLayer().getAttachType() == null ? null : aFeature
            .getLayer().getAttachType().getName();
    Type attachType = null;
    Feature attachFeature = null;
    if (attachTypeName != null) {
        attachType = CasUtil.getType(aCas, attachTypeName);
        attachFeature = attachType
                .getFeatureByBaseName(aFeature.getLayer().getAttachFeature().getName());
    }
    
    List<AnnotationFS> annotationsToRemove = new ArrayList<>();
    Type type = CasUtil.getType(aCas, aFeature.getLayer().getName());
    annotationsToRemove.addAll(select(aCas, type));
    
    for (AnnotationFS annotation : annotationsToRemove) {
        if (attachFeature != null) {
            // Unattach the annotation to be removed
            for (AnnotationFS attach : selectCovered(attachType, annotation)) {
                FeatureStructure existing = attach.getFeatureValue(attachFeature);
                if (annotation.equals(existing)) {
                    attach.setFeatureValue(attachFeature, null);
                }
            }
        }
        aCas.removeFsFromIndexes(annotation);
    }
}
 
Example 6
Source File: FeatureAttachedSpanAnnotationsTrulyAttachedCheck.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Override
public boolean check(Project aProject, CAS aCas, List<LogMessage> aMessages)
{
    boolean ok = true;
    int count = 0;
    for (AnnotationLayer layer : annotationService.listAnnotationLayer(aProject)) {
        if (!(SPAN_TYPE.equals(layer.getType()) && layer.getAttachFeature() != null)) {
            continue;
        }

        Type layerType;
        Type attachType;

        try {
            layerType = getAnnotationType(aCas, layer.getName());
            attachType = getAnnotationType(aCas, layer.getAttachType().getName());
        }
        catch (IllegalArgumentException e) {
            // This happens if the types do not (yet) exist in the CAS because the types are
            // new and the CAS has not been upgraded yet. In this case, we can just ignore the
            // check
            continue;
        }

        for (AnnotationFS anno : select(aCas, layerType)) {
            for (AnnotationFS attach : selectCovered(attachType, anno)) {
                AnnotationFS candidate = getFeature(attach, layer.getAttachFeature().getName(),
                        AnnotationFS.class);
                if (!anno.equals(candidate)) {
                    if (count < 100) {
                        aMessages.add(new LogMessage(this, LogLevel.ERROR,
                                "Annotation should be attached to ["
                                        + layer.getAttachFeature().getName()
                                        + "] but is not.\nAnnotation: [" + anno
                                        + "]\nAttach annotation:[" + attach + "]"));
                    }
                    count ++;
                    ok = false;
                }
            }
        }
    }
    
    if (count >= 100) {
        aMessages.add(new LogMessage(this, LogLevel.ERROR,
                "In total [%d] annotations were not properly attached", count));
    }
    
    return ok;
}
 
Example 7
Source File: ReattachFeatureAttachedSpanAnnotationsRepair.java    From webanno with Apache License 2.0 4 votes vote down vote up
@Override
public void repair(Project aProject, CAS aCas, List<LogMessage> aMessages)
{
    for (AnnotationLayer layer : annotationService.listAnnotationLayer(aProject)) {
        if (!(SPAN_TYPE.equals(layer.getType())
                && layer.getAttachFeature() != null)) {
            continue;
        }

        Type attachType = getType(aCas, layer.getAttachType().getName());
        String attachFeature = layer.getAttachFeature().getName();

        int count = 0;
        int nonNullCount = 0;

        // Go over the layer that has an attach feature (e.g. Token) and make sure that it is
        // filled
        // anno   -> e.g. Lemma
        // attach -> e.g. Token
        // Here we iterate over the attached layer, e.g. Lemma
        for (AnnotationFS anno : select(aCas, getType(aCas, layer.getName()))) {
            // Here we fetch all annotations of the layer we attach to at the relevant position,
            // e.g. Token
            for (AnnotationFS attach : selectCovered(attachType, anno)) {
                AnnotationFS existing = getFeature(attach, attachFeature, AnnotationFS.class);
                
                if (existing == null) {
                    setFeature(attach, layer.getAttachFeature().getName(), anno);
                    count++;
                }
                else if (!anno.equals(existing)) {
                    nonNullCount++;
                }
            }
        }

        if (count > 0) {
            aMessages.add(LogMessage.info(this,
                    "Reattached [%d] unattached spans on layer [%s].", count, layer.getName()));
        }
        
        if (nonNullCount > 0) {
            aMessages.add(LogMessage.error(this,
                    "Could not attach [%d] annotations on layer [%s] because attach feature "
                            + "already non-null.",
                    nonNullCount, layer.getName()));
        }
    }
}
 
Example 8
Source File: BlueCasUtil.java    From bluima with Apache License 2.0 4 votes vote down vote up
/**
 * REM: modified from CasUtils..
 * 
 * Get a list of annotations constraint by a certain annotation. Iterates
 * over all annotations to find the covered annotations. Does not use
 * subiterators and does not respect type prioritites. Was adapted from
 * {@link Subiterator}. Uses the same approach except that type priorities
 * are ignored.
 * 
 * @param cas
 *            a CAS.
 * @param coveringAnnotation
 *            the covering annotation.
 * @see Subiterator
 */
public static List<Annotation> selectCovered(CAS cas,
        AnnotationFS coveringAnnotation) {
    final int begin = coveringAnnotation.getBegin();
    final int end = coveringAnnotation.getEnd();

    final List<Annotation> list = new ArrayList<Annotation>();
    final FSIterator<AnnotationFS> it = cas.getAnnotationIndex().iterator();

    // Try to seek the insertion point.
    it.moveTo(coveringAnnotation);

    // If the insertion point is beyond the index, move back to the last.
    if (!it.isValid()) {
        it.moveToLast();
        if (!it.isValid()) {
            return list;
        }
    }

    // Ignore type priorities by seeking to the first that has the same
    // begin
    boolean moved = false;
    while (it.isValid() && (it.get()).getBegin() >= begin) {
        it.moveToPrevious();
        moved = true;
    }

    // If we moved, then we are now on one starting before the requested
    // begin, so we have to
    // move one ahead.
    if (moved) {
        it.moveToNext();
    }

    // If we managed to move outside the index, start at first.
    if (!it.isValid()) {
        it.moveToFirst();
    }

    // Skip annotations whose start is before the start parameter.
    while (it.isValid() && (it.get()).getBegin() < begin) {
        it.moveToNext();
    }

    boolean strict = true;
    while (it.isValid()) {
        AnnotationFS a = it.get();
        if (!(a instanceof Annotation))
            continue;

        // If the start of the current annotation is past the end parameter,
        // we're done.
        if (a.getBegin() > end) {
            break;
        }
        it.moveToNext();
        if (strict && a.getEnd() > end) {
            continue;
        }

        checkArgument(a.getBegin() >= coveringAnnotation.getBegin(),
                "Illegal begin " + a.getBegin() + " in ["
                        + coveringAnnotation.getBegin() + ".."
                        + coveringAnnotation.getEnd() + "]");
        checkArgument(
                a.getEnd() <= coveringAnnotation.getEnd(),
                "Illegal end " + a.getEnd() + " in ["
                        + coveringAnnotation.getBegin() + ".."
                        + coveringAnnotation.getEnd() + "]");

        if (!a.equals(coveringAnnotation) && !BlueCasUtil.isDocAnnot(a)) {
            list.add((Annotation) a);
        }
    }
    return unmodifiableList(list);
}