Search in sources :

Example 21 with Annotation

use of gate.Annotation in project gate-core by GateNLP.

the class AnnotationSetImpl method get.

// get(types)
/**
 * Select annotations by type and features
 *
 * This will return an annotation set containing just those annotations of a
 * particular type (i.e. with a particular name) and which have features with
 * specific names and values. (It will also return annotations that have
 * features besides those specified, but it will not return any annotations
 * that do not have all the specified feature-value pairs.)
 *
 * However, if constraints contains a feature whose value is equal to
 * gate.creole.ANNIEConstants.LOOKUP_CLASS_FEATURE_NAME (which is normally
 * "class"), then GATE will attempt to match that feature using an ontology
 * which it will try to retreive from a feature on the both the annotation and
 * in constraints. If these do not return identical ontologies, or if either
 * the annotation or constraints does not contain an ontology, then matching
 * will fail, and the annotation will not be added. In summary, this method
 * will not work normally for features with the name "class".
 *
 * @param type
 *          The name of the annotations to return.
 * @param constraints
 *          A feature map containing all of the feature value pairs that the
 *          annotation must have in order for them to be returned.
 * @return An annotation set containing only those annotations with the given
 *         name and which have the specified set of feature-value pairs.
 */
@Override
public AnnotationSet get(String type, FeatureMap constraints) {
    if (annotsByType == null)
        indexByType();
    AnnotationSet typeSet = get(type);
    if (typeSet == null)
        return null;
    Iterator<Annotation> iter = typeSet.iterator();
    List<Annotation> annotationsToAdd = new ArrayList<Annotation>();
    while (iter.hasNext()) {
        Annotation a = iter.next();
        // (a.getFeatures().entrySet().containsAll(constraints.entrySet()))
        if (a.getFeatures().subsumes(constraints))
            annotationsToAdd.add(a);
    }
    // while
    if (annotationsToAdd.isEmpty())
        return emptyAS();
    return new ImmutableAnnotationSetImpl(doc, annotationsToAdd);
}
Also used : ArrayList(java.util.ArrayList) AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation)

Example 22 with Annotation

use of gate.Annotation in project gate-core by GateNLP.

the class AnnotationSetImpl method add.

// add(Node, Node, String, FeatureMap)
/**
 * Add an existing annotation. Returns true when the set is modified.
 */
@Override
public boolean add(Annotation a) throws ClassCastException {
    Annotation oldValue = annotsById.put(a.getId(), a);
    if (oldValue != null) {
        if (annotsByType != null)
            removeFromTypeIndex(oldValue);
        if (annotsByStartNode != null)
            removeFromOffsetIndex(oldValue);
    }
    if (annotsByType != null)
        addToTypeIndex(a);
    if (annotsByStartNode != null)
        addToStartOffsetIndex(a);
    AnnotationSetEvent evt = new AnnotationSetEvent(this, AnnotationSetEvent.ANNOTATION_ADDED, doc, a);
    fireAnnotationAdded(evt);
    fireGateEvent(evt);
    return oldValue != a;
}
Also used : AnnotationSetEvent(gate.event.AnnotationSetEvent) Annotation(gate.Annotation)

Example 23 with Annotation

use of gate.Annotation in project gate-core by GateNLP.

the class AnnotationSetImpl method addAll.

// add(o)
/**
 * Adds multiple annotations to this set in one go. All the objects in the
 * provided collection should be of {@link gate.Annotation} type, otherwise a
 * ClassCastException will be thrown. The provided annotations will be used to
 * create new annotations using the appropriate add() methods from this set.
 * The new annotations will have different IDs from the old ones (which is
 * required in order to preserve the uniqueness of IDs inside an annotation
 * set).
 *
 * @param c
 *          a collection of annotations
 * @return <tt>true</tt> if the set has been modified as a result of this
 *         call.
 */
@Override
public boolean addAll(Collection<? extends Annotation> c) {
    Iterator<? extends Annotation> annIter = c.iterator();
    boolean changed = false;
    while (annIter.hasNext()) {
        Annotation a = annIter.next();
        try {
            add(a.getStartNode().getOffset(), a.getEndNode().getOffset(), a.getType(), a.getFeatures());
            changed = true;
        } catch (InvalidOffsetException ioe) {
            throw new IllegalArgumentException(ioe.toString());
        }
    }
    return changed;
}
Also used : InvalidOffsetException(gate.util.InvalidOffsetException) Annotation(gate.Annotation)

Example 24 with Annotation

use of gate.Annotation in project gate-core by GateNLP.

the class AnnotationSetImpl method get.

// get(type, constraints)
/**
 * Select annotations by type and feature names
 */
@Override
public AnnotationSet get(String type, Set<? extends Object> featureNames) {
    if (annotsByType == null)
        indexByType();
    AnnotationSet typeSet = null;
    if (type != null) {
        // if a type is provided, try finding annotations of this type
        typeSet = get(type);
        // if none exist, then return coz nothing left to do
        if (typeSet == null)
            return null;
    }
    List<Annotation> annotationsToAdd = new ArrayList<Annotation>();
    Iterator<Annotation> iter = null;
    if (type != null)
        iter = typeSet.iterator();
    else
        iter = annotsById.values().iterator();
    while (iter.hasNext()) {
        Annotation a = iter.next();
        // key/value pairs from the constraints map
        if (a.getFeatures().keySet().containsAll(featureNames))
            annotationsToAdd.add(a);
    }
    // while
    if (annotationsToAdd.isEmpty())
        return emptyAS();
    return new ImmutableAnnotationSetImpl(doc, annotationsToAdd);
}
Also used : ArrayList(java.util.ArrayList) AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation)

Example 25 with Annotation

use of gate.Annotation in project gate-core by GateNLP.

the class AnnotationSetImpl method getStrict.

// get(startOfset, endOffset)
/**
 * Select annotations by offset. This returns the set of annotations that
 * overlap strictly with the interval defined by the two provided offsets.The
 * result will include all the annotations that start at the start offset and
 * end strictly at the end offset
 */
public AnnotationSet getStrict(Long startOffset, Long endOffset) {
    // start at the start offset and end strictly at the end offset
    if (annotsByStartNode == null)
        indexByStartOffset();
    List<Annotation> annotationsToAdd = null;
    Iterator<Annotation> annotsIter;
    Node currentNode;
    Annotation currentAnnot;
    // find all the annots that start at the start offset
    currentNode = nodesByOffset.get(startOffset);
    if (currentNode != null) {
        Collection<Annotation> objFromPoint = getAnnotsByStartNode(currentNode.getId());
        if (objFromPoint != null) {
            annotsIter = objFromPoint.iterator();
            while (annotsIter.hasNext()) {
                currentAnnot = annotsIter.next();
                if (currentAnnot.getEndNode().getOffset().compareTo(endOffset) == 0) {
                    if (annotationsToAdd == null)
                        annotationsToAdd = new ArrayList<Annotation>();
                    annotationsToAdd.add(currentAnnot);
                }
            // if
            }
        // while
        }
    // if
    }
    // if
    return new ImmutableAnnotationSetImpl(doc, annotationsToAdd);
}
Also used : Node(gate.Node) ArrayList(java.util.ArrayList) Annotation(gate.Annotation)

Aggregations

Annotation (gate.Annotation)69 AnnotationSet (gate.AnnotationSet)28 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)15 Node (gate.Node)10 HashSet (java.util.HashSet)10 List (java.util.List)10 FeatureMap (gate.FeatureMap)8 Map (java.util.Map)8 TreeSet (java.util.TreeSet)8 Document (gate.Document)7 InvalidOffsetException (gate.util.InvalidOffsetException)7 Point (java.awt.Point)6 LinkedList (java.util.LinkedList)5 Set (java.util.Set)5 StatusListener (gate.event.StatusListener)4 GateRuntimeException (gate.util.GateRuntimeException)3 Color (java.awt.Color)3 Stack (java.util.Stack)3 TreeMap (java.util.TreeMap)3