Search in sources :

Example 36 with AnnotationSet

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

the class CorefEditor method showTypeWiseAnnotations.

/**
 * When user preses the show Toggle button, this will show up annotations
 * of selected Type from selected AnnotationSet
 */
private void showTypeWiseAnnotations() {
    if (typeSpecificHighlightedTags == null) {
        highlightedTypeAnnots = new ArrayList<Annotation>();
        typeSpecificHighlightedTags = new ArrayList<Object>();
    }
    if (showAnnotations.isSelected()) {
        // get the annotationsSet and its type
        AnnotationSet set = getAnnotationSet((String) annotSets.getSelectedItem());
        String type = (String) annotTypes.getSelectedItem();
        if (type == null) {
            try {
                JOptionPane.showMessageDialog(MainFrame.getInstance(), "No annotation type found to display");
            } catch (Exception e) {
                e.printStackTrace();
            }
            showAnnotations.setSelected(false);
            return;
        }
        Color color = AnnotationSetsView.getColor(getAnnotationSet((String) annotSets.getSelectedItem()).getName(), type);
        if (type != null) {
            AnnotationSet typeSet = set.get(type);
            Iterator<Annotation> iter = typeSet.iterator();
            while (iter.hasNext()) {
                Annotation ann = iter.next();
                highlightedTypeAnnots.add(ann);
                try {
                    typeSpecificHighlightedTags.add(highlighter.addHighlight(ann.getStartNode().getOffset().intValue(), ann.getEndNode().getOffset().intValue(), new DefaultHighlighter.DefaultHighlightPainter(color)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            // typeSpecificHighlightedTags.add(textView.addHighlight(ann, getAnnotationSet((String)annotSets.getSelectedItem()),color));
            }
        }
    } else {
        for (int i = 0; i < typeSpecificHighlightedTags.size(); i++) {
            // textView.removeHighlight(typeSpecificHighlightedTags.get(i));
            highlighter.removeHighlight(typeSpecificHighlightedTags.get(i));
        }
        typeSpecificHighlightedTags = new ArrayList<Object>();
        highlightedTypeAnnots = new ArrayList<Annotation>();
        highlightedTypeAnnotsOffsets = null;
    }
    // This is to make process faster.. instead of accessing each annotation and
    // its offset, we create an array with its annotation offsets to search faster
    Collections.sort(highlightedTypeAnnots, new gate.util.OffsetComparator());
    highlightedTypeAnnotsOffsets = new int[highlightedTypeAnnots.size() * 2];
    for (int i = 0, j = 0; j < highlightedTypeAnnots.size(); i += 2, j++) {
        Annotation ann1 = highlightedTypeAnnots.get(j);
        highlightedTypeAnnotsOffsets[i] = ann1.getStartNode().getOffset().intValue();
        highlightedTypeAnnotsOffsets[i + 1] = ann1.getEndNode().getOffset().intValue();
    }
}
Also used : Color(java.awt.Color) AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation) Point(java.awt.Point)

Example 37 with AnnotationSet

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

the class CorefEditor method highlightAnnotations.

/**
 * This methods highlights the annotations
 */
public void highlightAnnotations() {
    if (highlightedTags == null) {
        highlightedTags = new HashMap<CorefTreeNode, List<Object>>();
        highlightedChainAnnots = new ArrayList<Annotation>();
    }
    AnnotationSet annotSet = getAnnotationSet((String) annotSets.getSelectedItem());
    CorefTreeNode annotSetNode = corefAnnotationSetNodesMap.get(annotSets.getSelectedItem());
    if (annotSetNode == null) {
        return;
    }
    Map<CorefTreeNode, List<Integer>> chainMap = corefChains.get(annotSetNode);
    Iterator<CorefTreeNode> iter = chainMap.keySet().iterator();
    while (iter.hasNext()) {
        CorefTreeNode currentNode = iter.next();
        if (currentSelections.get(currentNode.toString()).booleanValue()) {
            if (!highlightedTags.containsKey(currentNode)) {
                // find out the arrayList
                List<Integer> ids = chainMap.get(currentNode);
                ArrayList<Object> highlighTag = new ArrayList<Object>();
                if (ids != null) {
                    for (int i = 0; i < ids.size(); i++) {
                        Annotation ann = annotSet.get(ids.get(i));
                        highlightedChainAnnots.add(ann);
                        Color color = currentColors.get(currentNode.toString());
                        try {
                            highlighTag.add(highlighter.addHighlight(ann.getStartNode().getOffset().intValue(), ann.getEndNode().getOffset().intValue(), new DefaultHighlighter.DefaultHighlightPainter(color)));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    // highlighTag.add(textView.addHighlight(ann, getAnnotationSet((String) annotSets.getSelectedItem()), color));
                    }
                    highlightedTags.put(currentNode, highlighTag);
                }
            }
        } else {
            if (highlightedTags.containsKey(currentNode)) {
                List<Object> highlights = highlightedTags.get(currentNode);
                for (int i = 0; i < highlights.size(); i++) {
                    // textView.removeHighlight(highlights.get(i));
                    highlighter.removeHighlight(highlights.get(i));
                }
                highlightedTags.remove(currentNode);
                List<Integer> ids = chainMap.get(currentNode);
                if (ids != null) {
                    for (int i = 0; i < ids.size(); i++) {
                        Annotation ann = annotSet.get(ids.get(i));
                        highlightedChainAnnots.remove(ann);
                    }
                }
            }
        }
    }
    // This is to make process faster.. instead of accessing each annotation and
    // its offset, we create an array with its annotation offsets to search faster
    Collections.sort(highlightedChainAnnots, new gate.util.OffsetComparator());
    highlightedChainAnnotsOffsets = new int[highlightedChainAnnots.size() * 2];
    for (int i = 0, j = 0; j < highlightedChainAnnots.size(); i += 2, j++) {
        Annotation ann1 = highlightedChainAnnots.get(j);
        highlightedChainAnnotsOffsets[i] = ann1.getStartNode().getOffset().intValue();
        highlightedChainAnnotsOffsets[i + 1] = ann1.getEndNode().getOffset().intValue();
    }
}
Also used : Color(java.awt.Color) ArrayList(java.util.ArrayList) AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation) Point(java.awt.Point) List(java.util.List) ArrayList(java.util.ArrayList)

Example 38 with AnnotationSet

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

the class AnnotationMerging method mergeAnnogationMajorityNoFeat.

/**
 * The majority merging method for the annotaiton not specifying any annotation
 * feature for label.
 */
private static void mergeAnnogationMajorityNoFeat(AnnotationSet[] annsArr, HashMap<Annotation, String> mergeAnns, boolean isTheSameInstances) {
    int numA = annsArr.length;
    // First copy the annotatioin sets into a temp array
    @SuppressWarnings({ "unchecked", "rawtypes" }) Set<Annotation>[] annsArrTemp = new Set[numA];
    for (int i = 0; i < numA; ++i) {
        if (annsArr[i] != null) {
            annsArrTemp[i] = new HashSet<Annotation>();
            for (Annotation ann : annsArr[i]) annsArrTemp[i].add(ann);
        }
    }
    for (int iA = 0; iA < numA; ++iA) {
        if (annsArrTemp[iA] != null) {
            for (Annotation ann : annsArrTemp[iA]) {
                int numDisagreed = 0;
                // Already the iA annotators don't agree the annotation
                numDisagreed = iA;
                StringBuffer featDisa = new StringBuffer();
                if (iA > 0) {
                    featDisa.append("0");
                    for (int i = 1; i < iA; ++i) featDisa.append("-" + i);
                }
                int numAgreed = 1;
                StringBuffer featAdd = new StringBuffer();
                featAdd.append(iA);
                for (int i = iA + 1; i < numA; ++i) {
                    boolean isContained = false;
                    if (annsArrTemp[i] != null) {
                        Annotation annT = null;
                        for (Annotation ann0 : annsArrTemp[i]) {
                            if (ann0.coextensive(ann)) {
                                ++numAgreed;
                                annT = ann0;
                                isContained = true;
                                featAdd.append("-" + i);
                                break;
                            }
                        }
                        if (isContained)
                            annsArrTemp[i].remove(annT);
                    }
                    if (!isContained) {
                        if (numDisagreed == 0)
                            featDisa.append(i);
                        else
                            featDisa.append("-" + i);
                        ++numDisagreed;
                    }
                }
                // end of the loop for the following annotation set
                if (numAgreed >= numDisagreed) {
                    mergeAnns.put(ann, featAdd.toString());
                } else if (isTheSameInstances) {
                    mergeAnns.put(ann, featAdd.toString());
                }
            }
        // for each ann in the current annotation set
        }
    }
    return;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation)

Example 39 with AnnotationSet

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

the class ClassificationMeasures method calculateConfusionMatrix.

/**
 * Create a confusion matrix in which annotations of identical span
 * bearing the specified feature name are compared in terms of feature value.
 * Compiles list of classes (feature values) on the fly.
 *
 * @param aS1 annotation set to compare to the second
 * @param aS2 annotation set to compare to the first
 * @param type annotation type containing the features to compare
 * @param feature feature name whose values will be compared
 * @param verbose message error output when ignoring annotations
 */
public void calculateConfusionMatrix(AnnotationSet aS1, AnnotationSet aS2, String type, String feature, boolean verbose) {
    // We'll accumulate a list of the feature values (a.k.a. class labels)
    featureValues = new TreeSet<String>();
    // Make a hash of hashes for the counts.
    HashMap<String, HashMap<String, Float>> countMap = new HashMap<String, HashMap<String, Float>>();
    // Get all the annotations of the correct type containing
    // the correct feature
    HashSet<String> featureSet = new HashSet<String>();
    featureSet.add(feature);
    AnnotationSet relevantAnns1 = aS1.get(type, featureSet);
    AnnotationSet relevantAnns2 = aS2.get(type, featureSet);
    // For each annotation in aS1, find the match in aS2
    for (Annotation relevantAnn1 : relevantAnns1) {
        // First we need to check that this annotation is not identical in span
        // to anything else in the same set. Duplicates should be excluded.
        List<Annotation> dupeAnnotations = new ArrayList<Annotation>();
        for (Annotation aRelevantAnns1 : relevantAnns1) {
            if (aRelevantAnns1.equals(relevantAnn1)) {
                continue;
            }
            if (aRelevantAnns1.coextensive(relevantAnn1)) {
                dupeAnnotations.add(aRelevantAnns1);
                dupeAnnotations.add(relevantAnn1);
            }
        }
        if (dupeAnnotations.size() > 1) {
            if (verbose) {
                Out.prln("ClassificationMeasures: " + "Same span annotations in set 1 detected! Ignoring.");
                Out.prln(Arrays.toString(dupeAnnotations.toArray()));
            }
        } else {
            // Find the match in as2
            List<Annotation> coextensiveAnnotations = new ArrayList<Annotation>();
            for (Annotation relevantAnn2 : relevantAnns2) {
                if (relevantAnn2.coextensive(relevantAnn1)) {
                    coextensiveAnnotations.add(relevantAnn2);
                }
            }
            if (coextensiveAnnotations.size() == 0) {
                if (verbose) {
                    Out.prln("ClassificationMeasures: Annotation in set 1 " + "with no counterpart in set 2 detected! Ignoring.");
                    Out.prln(relevantAnn1.toString());
                }
            } else if (coextensiveAnnotations.size() == 1) {
                // What are our feature values?
                String featVal1 = String.valueOf(relevantAnn1.getFeatures().get(feature));
                String featVal2 = String.valueOf(coextensiveAnnotations.get(0).getFeatures().get(feature));
                // Make sure both are present in our feature value list
                featureValues.add(featVal1);
                featureValues.add(featVal2);
                // Update the matrix hash of hashes
                // Get the right hashmap for the as1 feature value
                HashMap<String, Float> subHash = countMap.get(featVal1);
                if (subHash == null) {
                    // This is a new as1 feature value, since it has no subhash yet
                    HashMap<String, Float> subHashForNewAS1FeatVal = new HashMap<String, Float>();
                    // Since it is a new as1 feature value, there can be no existing
                    // as2 feature values paired with it. So we make a new one for this
                    // as2 feature value
                    subHashForNewAS1FeatVal.put(featVal2, (float) 1);
                    countMap.put(featVal1, subHashForNewAS1FeatVal);
                } else {
                    // Increment the count
                    Float count = subHash.get(featVal2);
                    if (count == null) {
                        subHash.put(featVal2, (float) 1);
                    } else {
                        subHash.put(featVal2, (float) count.intValue() + 1);
                    }
                }
            } else if (coextensiveAnnotations.size() > 1) {
                if (verbose) {
                    Out.prln("ClassificationMeasures: " + "Same span annotations in set 2 detected! Ignoring.");
                    Out.prln(Arrays.toString(coextensiveAnnotations.toArray()));
                }
            }
        }
    }
    // Now we have this hash of hashes, but the calculation implementations
    // require an array of floats. So for now we can just translate it.
    confusionMatrix = convert2DHashTo2DFloatArray(countMap, featureValues);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation) HashSet(java.util.HashSet)

Example 40 with AnnotationSet

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

the class CorpusBenchmarkTool method countWords.

/**
 * Count all Token.kind=word annotations in the document
 */
protected int countWords(Document annotDoc) {
    int count = 0;
    if (annotDoc == null)
        return 0;
    // check for Token in outputSetName
    AnnotationSet tokens = annotDoc.getAnnotations(outputSetName).get("Token");
    if (tokens == null)
        return 0;
    Iterator<Annotation> it = tokens.iterator();
    Annotation currAnnotation;
    while (it.hasNext()) {
        currAnnotation = it.next();
        Object feature = currAnnotation.getFeatures().get("kind");
        if (feature != null && "word".equalsIgnoreCase((String) feature))
            ++count;
    }
    return count;
}
Also used : AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation)

Aggregations

AnnotationSet (gate.AnnotationSet)43 Annotation (gate.Annotation)27 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)11 Document (gate.Document)9 List (java.util.List)8 FeatureMap (gate.FeatureMap)7 InvalidOffsetException (gate.util.InvalidOffsetException)6 AnnotationSetImpl (gate.annotation.AnnotationSetImpl)5 Set (java.util.Set)5 StatusListener (gate.event.StatusListener)4 GateRuntimeException (gate.util.GateRuntimeException)4 Point (java.awt.Point)4 IOException (java.io.IOException)4 URL (java.net.URL)4 Map (java.util.Map)4 Color (java.awt.Color)3 TreeSet (java.util.TreeSet)3 TestDocument (gate.corpora.TestDocument)2