Search in sources :

Example 26 with Annotation

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

the class AnnotationSetImpl method addToStartOffsetIndex.

// addToTypeIndex(a)
/**
 * Add an annotation to the start offset index. Does nothing if the index
 * doesn't exist.
 */
@SuppressWarnings("unchecked")
void addToStartOffsetIndex(Annotation a) {
    Node startNode = a.getStartNode();
    Node endNode = a.getEndNode();
    Long start = startNode.getOffset();
    Long end = endNode.getOffset();
    // add a's nodes to the offset index
    if (nodesByOffset != null) {
        nodesByOffset.put(start, startNode);
        nodesByOffset.put(end, endNode);
    }
    // add marking for longest annot
    long annotLength = end - start;
    if (annotLength > longestAnnot)
        longestAnnot = annotLength;
    // if there's no appropriate index give up
    if (annotsByStartNode == null)
        return;
    // get the annotations that start at the same node, or create new
    // set
    Object thisNodeObject = annotsByStartNode.get(startNode.getId());
    if (thisNodeObject == null) {
        // put directly the annotation
        annotsByStartNode.put(startNode.getId(), a);
    } else {
        // already something there : a single Annotation or a
        // Collection
        Set<Annotation> newCollection = null;
        if (thisNodeObject instanceof Annotation) {
            // at this Node
            if (thisNodeObject.equals(a))
                return;
            newCollection = new HashSet<Annotation>(3);
            newCollection.add((Annotation) thisNodeObject);
            annotsByStartNode.put(startNode.getId(), newCollection);
        } else
            newCollection = (Set<Annotation>) thisNodeObject;
        // get the existing set
        // add the new node annotation
        newCollection.add(a);
    }
}
Also used : AbstractSet(java.util.AbstractSet) HashSet(java.util.HashSet) AnnotationSet(gate.AnnotationSet) RelationSet(gate.relations.RelationSet) Set(java.util.Set) Node(gate.Node) Annotation(gate.Annotation)

Example 27 with Annotation

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

the class AnnotationSetImpl method addAllKeepIDs.

/**
 * 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. This method does not create copies of
 * the annotations like addAll() does but simply adds the new annotations to
 * the set. It is intended to be used solely by annotation sets in order to
 * construct the results for various get(...) methods.
 *
 * @param c
 *          a collection of annotations
 * @return <tt>true</tt> if the set has been modified as a result of this
 *         call.
 */
protected boolean addAllKeepIDs(Collection<? extends Annotation> c) {
    Iterator<? extends Annotation> annIter = c.iterator();
    boolean changed = false;
    while (annIter.hasNext()) {
        Annotation a = annIter.next();
        changed |= add(a);
    }
    return changed;
}
Also used : Annotation(gate.Annotation)

Example 28 with Annotation

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

the class CorpusAnnotationDiff method init.

/**
 * This method does the diff, Precision,Recall,FalsePositive
 * calculation and so on.
 */
@Override
public Resource init() throws ResourceInstantiationException {
    colors[DEFAULT_TYPE] = WHITE;
    colors[CORRECT_TYPE] = GREEN;
    colors[SPURIOUS_TYPE] = RED;
    colors[PARTIALLY_CORRECT_TYPE] = BLUE;
    colors[MISSING_TYPE] = YELLOW;
    // Initialize the partially sets...
    keyPartiallySet = new HashSet<Annotation>();
    responsePartiallySet = new HashSet<Annotation>();
    // Do the diff, P&R calculation and so on
    AnnotationSet keyAnnotSet = null;
    AnnotationSet responseAnnotSet = null;
    if (annotationSchema == null)
        throw new ResourceInstantiationException("No annotation schema defined !");
    if (keyCorpus == null || 0 == keyCorpus.size())
        throw new ResourceInstantiationException("No key corpus or empty defined !");
    if (responseCorpus == null || 0 == responseCorpus.size())
        throw new ResourceInstantiationException("No response corpus or empty defined !");
    // init counters and do difference for documents by pairs
    for (int type = 0; type < MAX_TYPES; type++) typeCounter[type] = 0;
    diffSet = new HashSet<DiffSetElement>();
    for (int i = 0; i < keyCorpus.size(); ++i) {
        keyDocument = keyCorpus.get(i);
        // find corresponding responce document if any
        Document doc;
        responseDocument = null;
        for (int j = 0; j < responseCorpus.size(); ++j) {
            doc = responseCorpus.get(j);
            if (0 == doc.getName().compareTo(keyDocument.getName()) || 0 == doc.getSourceUrl().getFile().compareTo(keyDocument.getSourceUrl().getFile())) {
                responseDocument = doc;
                // response corpus loop
                break;
            }
        // if
        }
        if (null == responseDocument) {
            Out.prln("There is no mach in responce corpus for document '" + keyDocument.getName() + "' from key corpus");
            // key corpus loop
            continue;
        }
        if (keyAnnotationSetName == null) {
            // Get the default key AnnotationSet from the keyDocument
            keyAnnotSet = keyDocument.getAnnotations().get(annotationSchema.getAnnotationName());
        } else {
            keyAnnotSet = keyDocument.getAnnotations(keyAnnotationSetName).get(annotationSchema.getAnnotationName());
        }
        if (keyAnnotSet == null)
            // The diff will run with an empty set.All annotations from response
            // would be spurious.
            keyAnnotList = new LinkedList<Annotation>();
        else
            // The alghoritm will modify this annotation set. It is better to make a
            // separate copy of them.
            keyAnnotList = new LinkedList<Annotation>(keyAnnotSet);
        if (responseAnnotationSetName == null)
            // Get the response AnnotationSet from the default set
            responseAnnotSet = responseDocument.getAnnotations().get(annotationSchema.getAnnotationName());
        else
            responseAnnotSet = responseDocument.getAnnotations(responseAnnotationSetName).get(annotationSchema.getAnnotationName());
        if (responseAnnotSet == null)
            // The diff will run with an empty set.All annotations from key
            // would be missing.
            responseAnnotList = new LinkedList<Annotation>();
        else
            // The alghoritm will modify this annotation set. It is better to make a
            // separate copy of them.
            responseAnnotList = new LinkedList<Annotation>(responseAnnotSet);
        // Sort them ascending on Start offset (the comparator does that)
        AnnotationSetComparator asComparator = new AnnotationSetComparator();
        Collections.sort(keyAnnotList, asComparator);
        Collections.sort(responseAnnotList, asComparator);
        // Calculate the diff Set. This set will be used later with graphic
        // visualisation.
        doDiff(keyAnnotList, responseAnnotList);
    }
    // If it runs under text mode just stop here.
    if (textMode)
        return this;
    // Show it
    // Configuring the formatter object. It will be used later to format
    // precision and recall
    formatter.setMaximumIntegerDigits(1);
    formatter.setMinimumFractionDigits(4);
    formatter.setMinimumFractionDigits(4);
    // Create an Annotation diff table model
    AnnotationDiffTableModel diffModel = new AnnotationDiffTableModel(diffSet);
    // Create a XJTable based on this model
    diffTable = new XJTable(diffModel);
    diffTable.setAlignmentX(Component.LEFT_ALIGNMENT);
    // Set the cell renderer for this table.
    AnnotationDiffCellRenderer cellRenderer = new AnnotationDiffCellRenderer();
    diffTable.setDefaultRenderer(java.lang.String.class, cellRenderer);
    diffTable.setDefaultRenderer(java.lang.Long.class, cellRenderer);
    // Put the table into a JScroll
    // Arange all components on a this JPanel
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            arangeAllComponents();
        }
    });
    if (DEBUG)
        printStructure(diffSet);
    return this;
}
Also used : XJTable(gate.swing.XJTable) AnnotationSet(gate.AnnotationSet) Document(gate.Document) Annotation(gate.Annotation) LinkedList(java.util.LinkedList) ResourceInstantiationException(gate.creole.ResourceInstantiationException)

Example 29 with Annotation

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

the class DocumentImpl method getAnnotationsForOffset.

// saveAnnotationSetAsXmlInOrig()
/**
 * This method returns a list with annotations ordered that way that they can
 * be serialized from left to right, at the offset. If one of the params is
 * null then an empty list will be returned.
 *
 * @param aDumpAnnotSet
 *          is a set containing all annotations that will be dumped.
 * @param offset
 *          represent the offset at witch the annotation must start AND/OR
 *          end.
 * @return a list with those annotations that need to be serialized.
 */
private List<Annotation> getAnnotationsForOffset(Set<Annotation> aDumpAnnotSet, Long offset) {
    List<Annotation> annotationList = new LinkedList<Annotation>();
    if (aDumpAnnotSet == null || offset == null)
        return annotationList;
    Set<Annotation> annotThatStartAtOffset = new TreeSet<Annotation>(new AnnotationComparator(ORDER_ON_END_OFFSET, DESC));
    Set<Annotation> annotThatEndAtOffset = new TreeSet<Annotation>(new AnnotationComparator(ORDER_ON_START_OFFSET, DESC));
    Set<Annotation> annotThatStartAndEndAtOffset = new TreeSet<Annotation>(new AnnotationComparator(ORDER_ON_ANNOT_ID, ASC));
    // Fill these tree lists with annotation tat start, end or start and
    // end at the offset.
    Iterator<Annotation> iter = aDumpAnnotSet.iterator();
    while (iter.hasNext()) {
        Annotation ann = iter.next();
        if (offset.equals(ann.getStartNode().getOffset())) {
            if (offset.equals(ann.getEndNode().getOffset()))
                annotThatStartAndEndAtOffset.add(ann);
            else
                annotThatStartAtOffset.add(ann);
        } else {
            if (offset.equals(ann.getEndNode().getOffset()))
                annotThatEndAtOffset.add(ann);
        }
    // End if
    }
    // End while
    annotationList.addAll(annotThatEndAtOffset);
    annotThatEndAtOffset = null;
    annotationList.addAll(annotThatStartAtOffset);
    annotThatStartAtOffset = null;
    iter = annotThatStartAndEndAtOffset.iterator();
    while (iter.hasNext()) {
        Annotation ann = iter.next();
        Iterator<Annotation> it = annotationList.iterator();
        boolean breaked = false;
        while (it.hasNext()) {
            Annotation annFromList = it.next();
            if (annFromList.getId().intValue() > ann.getId().intValue()) {
                annotationList.add(annotationList.indexOf(annFromList), ann);
                breaked = true;
                break;
            }
        // End if
        }
        // End while
        if (!breaked)
            annotationList.add(ann);
        iter.remove();
    }
    // End while
    return annotationList;
}
Also used : TreeSet(java.util.TreeSet) Annotation(gate.Annotation) LinkedList(java.util.LinkedList)

Example 30 with Annotation

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

the class DocumentImpl method insertsSafety.

// insertsSafety()
private boolean insertsSafety(List<Annotation> aTargetAnnotList, Annotation aSourceAnnotation) {
    if (aTargetAnnotList == null || aSourceAnnotation == null) {
        this.crossedOverAnnotation = null;
        return false;
    }
    if (aSourceAnnotation.getStartNode() == null || aSourceAnnotation.getStartNode().getOffset() == null) {
        this.crossedOverAnnotation = null;
        return false;
    }
    if (aSourceAnnotation.getEndNode() == null || aSourceAnnotation.getEndNode().getOffset() == null) {
        this.crossedOverAnnotation = null;
        return false;
    }
    // Get the start and end offsets
    Long start = aSourceAnnotation.getStartNode().getOffset();
    Long end = aSourceAnnotation.getEndNode().getOffset();
    // Read aSourceAnnotation offsets long
    long s2 = start.longValue();
    long e2 = end.longValue();
    // Obtain a set with all annotations annotations that overlap
    // totaly or partially with the interval defined by the two provided offsets
    List<Annotation> as = new ArrayList<Annotation>();
    for (int i = 0; i < aTargetAnnotList.size(); i++) {
        Annotation annot = aTargetAnnotList.get(i);
        if (annot.getStartNode().getOffset().longValue() >= s2 && annot.getStartNode().getOffset().longValue() <= e2)
            as.add(annot);
        else if (annot.getEndNode().getOffset().longValue() >= s2 && annot.getEndNode().getOffset().longValue() <= e2)
            as.add(annot);
    }
    // Investigate all the annotations from as to see if there is one that
    // comes in conflict with aSourceAnnotation
    Iterator<Annotation> it = as.iterator();
    while (it.hasNext()) {
        Annotation ann = it.next();
        // Read ann offsets
        long s1 = ann.getStartNode().getOffset().longValue();
        long e1 = ann.getEndNode().getOffset().longValue();
        if (s1 < s2 && s2 < e1 && e1 < e2) {
            this.crossedOverAnnotation = ann;
            return false;
        }
        if (s2 < s1 && s1 < e2 && e2 < e1) {
            this.crossedOverAnnotation = ann;
            return false;
        }
    }
    // End while
    return true;
}
Also used : 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