Search in sources :

Example 46 with Annotation

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

the class CorpusAnnotationDiff method detectResponseType.

// detectKeyType
/**
 *  Decide what type is the responseAnnotation
 *  (PARTIALLY_CORRECT_TYPE, SPURIOUS or NULL_TYPE)
 *  This method must be applied only on annotation from response set.
 *  @param anAnnot is an annotation from the key set.
 *  @return three possible value(PARTIALLY_CORRECT_TYPE, SPURIOUS or NULL_TYPE)
 */
private int detectResponseType(Annotation anAnnot) {
    if (anAnnot == null)
        return NULL_TYPE;
    if (responsePartiallySet.contains(anAnnot))
        return PARTIALLY_CORRECT_TYPE;
    Iterator<Annotation> iter = keyPartiallySet.iterator();
    while (iter.hasNext()) {
        Annotation a = iter.next();
        if (a.isPartiallyCompatible(anAnnot, keyFeatureNamesSet))
            return PARTIALLY_CORRECT_TYPE;
    }
    // End while
    iter = keyAnnotList.iterator();
    while (iter.hasNext()) {
        Annotation a = iter.next();
        if (a.isPartiallyCompatible(anAnnot, keyFeatureNamesSet)) {
            responsePartiallySet.add(anAnnot);
            keyPartiallySet.add(a);
            return PARTIALLY_CORRECT_TYPE;
        }
    // End if
    }
    // End while
    return SPURIOUS_TYPE;
}
Also used : Annotation(gate.Annotation)

Example 47 with Annotation

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

the class CorpusAnnotationDiff method doDiff.

// printStructure
/**
 * This method is the brain of the AnnotationSet diff and creates a set with
 * diffSetElement objects.
 * @param aKeyAnnotList a list containing the annotations from key. If this
 * param is <b>null</b> then the method will simply return and will not do a
 * thing.
 * @param aResponseAnnotList a list containing the annotation from response.
 * If this param is <b>null</b> the method will return.
 */
protected void doDiff(List<Annotation> aKeyAnnotList, List<Annotation> aResponseAnnotList) {
    // If one of the annotation sets is null then is no point in doing the diff.
    if (aKeyAnnotList == null || aResponseAnnotList == null)
        return;
    // Iterate throught all elements from keyList and find those in the response
    // list which satisfies isCompatible() and isPartiallyCompatible() relations
    Iterator<Annotation> keyIterator = aKeyAnnotList.iterator();
    while (keyIterator.hasNext()) {
        Annotation keyAnnot = keyIterator.next();
        Iterator<Annotation> responseIterator = aResponseAnnotList.iterator();
        DiffSetElement diffElement = null;
        while (responseIterator.hasNext()) {
            Annotation responseAnnot = responseIterator.next();
            if (keyAnnot.isPartiallyCompatible(responseAnnot, keyFeatureNamesSet)) {
                keyPartiallySet.add(keyAnnot);
                responsePartiallySet.add(responseAnnot);
                if (keyAnnot.coextensive(responseAnnot)) {
                    // Found two compatible annotations
                    // Create a new DiffSetElement and add it to the diffSet
                    diffElement = new DiffSetElement(keyAnnot, responseAnnot, DEFAULT_TYPE, CORRECT_TYPE, keyDocument, responseDocument);
                    // Add this element to the DiffSet
                    addToDiffset(diffElement);
                }
            // End if (keyAnnot.coextensive(responseAnnot))
            } else if (keyAnnot.coextensive(responseAnnot)) {
                // Found two aligned annotations. We have to find out if the response
                // is partialy compatible with another key annotation.
                // Create a new DiffSetElement and add it to the diffSet
                diffElement = new DiffSetElement(keyAnnot, responseAnnot, detectKeyType(keyAnnot), detectResponseType(responseAnnot), keyDocument, responseDocument);
                // Add this element to the DiffSet
                addToDiffset(diffElement);
            }
            if (diffElement != null) {
                // Eliminate the response annotation from the list.
                responseIterator.remove();
                break;
            }
        // End if
        }
        // If diffElement != null it means that break was used
        if (diffElement == null) {
            if (keyPartiallySet.contains(keyAnnot))
                diffElement = new DiffSetElement(keyAnnot, null, DEFAULT_TYPE, NULL_TYPE, keyDocument, responseDocument);
            else {
                // If keyAnnot is not in keyPartiallySet then it has to be checked
                // agains all annotations in DiffSet to see if there is
                // a previous annotation from response set which is partially
                // compatible with the keyAnnot
                Iterator<DiffSetElement> respParIter = diffSet.iterator();
                while (respParIter.hasNext()) {
                    DiffSetElement diffElem = respParIter.next();
                    Annotation respAnnot = diffElem.getRightAnnotation();
                    if (respAnnot != null && keyAnnot.isPartiallyCompatible(respAnnot, keyFeatureNamesSet)) {
                        diffElement = new DiffSetElement(keyAnnot, null, DEFAULT_TYPE, NULL_TYPE, keyDocument, responseDocument);
                        break;
                    }
                // End if
                }
                // If is still nul then it means that the key annotation is missing
                if (diffElement == null)
                    diffElement = new DiffSetElement(keyAnnot, null, MISSING_TYPE, NULL_TYPE, keyDocument, responseDocument);
            }
            // End if
            addToDiffset(diffElement);
        }
        // End if
        keyIterator.remove();
    }
    // end while keyIterator
    DiffSetElement diffElem = null;
    Iterator<Annotation> responseIter = aResponseAnnotList.iterator();
    while (responseIter.hasNext()) {
        Annotation respAnnot = responseIter.next();
        if (responsePartiallySet.contains(respAnnot))
            diffElem = new DiffSetElement(null, respAnnot, NULL_TYPE, PARTIALLY_CORRECT_TYPE, keyDocument, responseDocument);
        else
            diffElem = new DiffSetElement(null, respAnnot, NULL_TYPE, SPURIOUS_TYPE, keyDocument, responseDocument);
        addToDiffset(diffElem);
        responseIter.remove();
    }
    // End while
    // CALCULATE ALL (NLP) MEASURES like:
    // Precistion, Recall, FalsePositive and F-Measure
    int possible = // this comes from Key or Resp
    typeCounter[CORRECT_TYPE] + // this comes from Resp
    typeCounter[PARTIALLY_CORRECT_TYPE] + // this comes from Key
    typeCounter[MISSING_TYPE];
    int actual = // this comes from Key or Resp
    typeCounter[CORRECT_TYPE] + // this comes from Resp
    typeCounter[PARTIALLY_CORRECT_TYPE] + // this comes from Resp
    typeCounter[SPURIOUS_TYPE];
    /*
    if (actual != responseSize)
      Err.prln("AnnotDiff warning: The response size(" + responseSize +
      ") is not the same as the computed value of" +
    " actual(Correct[resp or key]+Partial[resp]+Spurious[resp]=" + actual +")");
*/
    if (actual != 0) {
        precisionStrict = ((double) typeCounter[CORRECT_TYPE]) / ((double) actual);
        precisionLenient = ((double) (typeCounter[CORRECT_TYPE] + typeCounter[PARTIALLY_CORRECT_TYPE])) / ((double) actual);
        precisionAverage = (precisionStrict + precisionLenient) / 2;
    }
    // End if
    if (possible != 0) {
        recallStrict = ((double) typeCounter[CORRECT_TYPE]) / ((double) possible);
        recallLenient = ((double) (typeCounter[CORRECT_TYPE] + typeCounter[PARTIALLY_CORRECT_TYPE])) / ((double) possible);
        recallAverage = (recallStrict + recallLenient) / 2;
    }
    // End if
    int no = 0;
    // Annotations
    if (annotationTypeForFalsePositive != null)
        // Was it the default set ?
        if (responseAnnotationSetNameFalsePoz == null) {
            AnnotationSet aSet = responseDocument.getAnnotations().get(annotationTypeForFalsePositive);
            no = aSet == null ? 0 : aSet.size();
        } else {
            AnnotationSet aSet = responseDocument.getAnnotations(responseAnnotationSetNameFalsePoz).get(annotationTypeForFalsePositive);
            no = aSet == null ? 0 : aSet.size();
        }
    if (no != 0) {
        // No error here: the formula is the opposite to recall or precission
        falsePositiveStrict = ((double) (typeCounter[SPURIOUS_TYPE] + typeCounter[PARTIALLY_CORRECT_TYPE])) / ((double) no);
        falsePositiveLenient = ((double) typeCounter[SPURIOUS_TYPE]) / ((double) no);
        falsePositiveAverage = (falsePositiveStrict + falsePositiveLenient) / 2;
    }
    // End if
    // Calculate F-Measure Strict
    double denominator = weight * (precisionStrict + recallStrict);
    if (denominator != 0)
        fMeasureStrict = (precisionStrict * recallStrict) / denominator;
    else
        fMeasureStrict = 0.0;
    // Calculate F-Measure Lenient
    denominator = weight * (precisionLenient + recallLenient);
    if (denominator != 0)
        fMeasureLenient = (precisionLenient * recallLenient) / denominator;
    else
        fMeasureLenient = 0.0;
    // Calculate F-Measure Average
    fMeasureAverage = (fMeasureStrict + fMeasureLenient) / 2;
}
Also used : AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation)

Example 48 with Annotation

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

the class CorpusAnnotationDiff method detectKeyType.

// doDiff
/**
 * Decide what type is the keyAnnotation (DEFAULT_TYPE, MISSING or NULL_TYPE)
 *  This method must be applied only on annotation from key set.
 *  @param anAnnot is an annotation from the key set.
 *  @return three possible value(DEFAULT_TYPE, MISSING or NULL_TYPE)
 */
private int detectKeyType(Annotation anAnnot) {
    if (anAnnot == null)
        return NULL_TYPE;
    if (keyPartiallySet.contains(anAnnot))
        return DEFAULT_TYPE;
    Iterator<Annotation> iter = responsePartiallySet.iterator();
    while (iter.hasNext()) {
        Annotation a = iter.next();
        if (anAnnot.isPartiallyCompatible(a, keyFeatureNamesSet))
            return DEFAULT_TYPE;
    }
    // End while
    iter = responseAnnotList.iterator();
    while (iter.hasNext()) {
        Annotation a = iter.next();
        if (anAnnot.isPartiallyCompatible(a, keyFeatureNamesSet)) {
            responsePartiallySet.add(a);
            keyPartiallySet.add(anAnnot);
            return DEFAULT_TYPE;
        }
    // End if
    }
    // End while
    return MISSING_TYPE;
}
Also used : Annotation(gate.Annotation)

Example 49 with Annotation

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

the class TestXml method buildID2AnnotMap.

/**
 * Scans a target Doc for all Annotations and builds a map (from anot ID to annot) in the process
 * I also checks to see if there are two annotations with the same ID.
 * @param aDoc The GATE doc to be scaned
 * @return a Map ID2Annot
 */
private Map<Integer, Annotation> buildID2AnnotMap(Document aDoc) {
    Map<Integer, Annotation> id2AnnMap = new HashMap<Integer, Annotation>();
    // Scan the default annotation set
    AnnotationSet annotSet = aDoc.getAnnotations();
    addAnnotSet2Map(annotSet, id2AnnMap);
    // Scan all named annotation sets
    if (aDoc.getNamedAnnotationSets() != null) {
        for (Iterator<AnnotationSet> namedAnnotSetsIter = aDoc.getNamedAnnotationSets().values().iterator(); namedAnnotSetsIter.hasNext(); ) {
            addAnnotSet2Map(namedAnnotSetsIter.next(), id2AnnMap);
        }
    // End while
    }
    // End if
    return id2AnnMap;
}
Also used : HashMap(java.util.HashMap) AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation)

Example 50 with Annotation

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

the class TestXml method addAnnotSet2Map.

// End of compareAnnot()
private Map<Integer, Annotation> addAnnotSet2Map(AnnotationSet annotSet, Map<Integer, Annotation> id2AnnMap) {
    for (Iterator<Annotation> it = annotSet.iterator(); it.hasNext(); ) {
        Annotation a = it.next();
        Integer id = a.getId();
        assertTrue("Found two annotations(one with type = " + a.getType() + ")with the same ID=" + id, !id2AnnMap.keySet().contains(id));
        id2AnnMap.put(id, a);
    }
    // End for
    return id2AnnMap;
}
Also used : 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