Search in sources :

Example 16 with AnnotationSet

use of gate.AnnotationSet 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 17 with AnnotationSet

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

the class DocumentImpl method getAnnotations.

// getAnnotations()
/**
 * Get a named set of annotations. Creates a new set if one with this name
 * doesn't exist yet. If the provided name is null or the empty string then
 * it returns the default annotation set.
 */
@Override
public AnnotationSet getAnnotations(String name) {
    if (name == null || "".equals(name))
        return getAnnotations();
    if (namedAnnotSets == null) {
        namedAnnotSets = new HashMap<String, AnnotationSet>();
    }
    AnnotationSet namedSet = namedAnnotSets.get(name);
    if (namedSet == null) {
        namedSet = new AnnotationSetImpl(this, name);
        namedAnnotSets.put(name, namedSet);
        DocumentEvent evt = new DocumentEvent(this, DocumentEvent.ANNOTATION_SET_ADDED, name);
        fireAnnotationSetAdded(evt);
    }
    return namedSet;
}
Also used : AnnotationSetImpl(gate.annotation.AnnotationSetImpl) AnnotationSet(gate.AnnotationSet) DocumentEvent(gate.event.DocumentEvent)

Example 18 with AnnotationSet

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

the class DocumentImpl method writeStartTag.

// writeStartTag
/**
 * Returns a string representing a start tag based on the input annot
 */
private String writeStartTag(Annotation annot, boolean includeFeatures, boolean includeNamespace) {
    // Get the annot feature used to store the namespace prefix, if it
    // has been defined
    String nsPrefix = null;
    if (serializeNamespaceInfo)
        nsPrefix = (String) annot.getFeatures().get(namespacePrefixFeature);
    AnnotationSet originalMarkupsAnnotSet = this.getAnnotations(GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME);
    StringBuffer strBuff = new StringBuffer("");
    if (annot == null)
        return strBuff.toString();
    // if (!addGatePreserveFormatTag && isRootTag){
    if (theRootAnnotation != null && annot.getId().equals(theRootAnnotation.getId())) {
        // spoil all links in an HTML file!
        if (includeFeatures) {
            strBuff.append("<");
            if (nsPrefix != null && !nsPrefix.isEmpty())
                strBuff.append(nsPrefix + ":");
            strBuff.append(annot.getType());
            strBuff.append(" ");
            if (includeNamespace) {
                // but don't add the gate ns declaration if it's already there!
                if (annot.getFeatures().get("xmlns:gate") == null)
                    strBuff.append("xmlns:gate=\"http://www.gate.ac.uk\"");
                strBuff.append(" gate:");
            }
            strBuff.append("gateId=\"");
            strBuff.append(annot.getId());
            strBuff.append("\"");
            strBuff.append(" ");
            if (includeNamespace) {
                strBuff.append("gate:");
            }
            strBuff.append("annotMaxId=\"");
            strBuff.append(nextAnnotationId);
            strBuff.append("\"");
            strBuff.append(writeFeatures(annot.getFeatures(), includeNamespace));
            strBuff.append(">");
        } else if (originalMarkupsAnnotSet.contains(annot)) {
            strBuff.append("<");
            if (nsPrefix != null && !nsPrefix.isEmpty())
                strBuff.append(nsPrefix + ":");
            strBuff.append(annot.getType());
            strBuff.append(writeFeatures(annot.getFeatures(), includeNamespace));
            strBuff.append(">");
        } else {
            strBuff.append("<");
            if (nsPrefix != null && !nsPrefix.isEmpty())
                strBuff.append(nsPrefix + ":");
            strBuff.append(annot.getType());
            strBuff.append(">");
        }
    } else {
        // spoil all links in an HTML file!
        if (includeFeatures) {
            strBuff.append("<");
            if (nsPrefix != null && !nsPrefix.isEmpty())
                strBuff.append(nsPrefix + ":");
            strBuff.append(annot.getType());
            strBuff.append(" ");
            if (includeNamespace) {
                strBuff.append("gate:");
            }
            // if includeNamespaces
            strBuff.append("gateId=\"");
            strBuff.append(annot.getId());
            strBuff.append("\"");
            strBuff.append(writeFeatures(annot.getFeatures(), includeNamespace));
            strBuff.append(">");
        } else if (originalMarkupsAnnotSet.contains(annot)) {
            strBuff.append("<");
            if (nsPrefix != null && !nsPrefix.isEmpty())
                strBuff.append(nsPrefix + ":");
            strBuff.append(annot.getType());
            strBuff.append(writeFeatures(annot.getFeatures(), includeNamespace));
            strBuff.append(">");
        } else {
            strBuff.append("<");
            if (nsPrefix != null && !nsPrefix.isEmpty())
                strBuff.append(nsPrefix + ":");
            strBuff.append(annot.getType());
            strBuff.append(">");
        }
    }
    // End if
    return strBuff.toString();
}
Also used : AnnotationSet(gate.AnnotationSet)

Example 19 with AnnotationSet

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

the class DocumentImpl method insertsSafety.

// End toXml()
/**
 * This method verifies if aSourceAnnotation can ve inserted safety into the
 * aTargetAnnotSet. Safety means that it doesn't violate the crossed over
 * contition with any annotation from the aTargetAnnotSet.
 *
 * @param aTargetAnnotSet
 *          the annotation set to include the aSourceAnnotation
 * @param aSourceAnnotation
 *          the annotation to be inserted into the aTargetAnnotSet
 * @return true if the annotation inserts safety, or false otherwise.
 */
private boolean insertsSafety(AnnotationSet aTargetAnnotSet, Annotation aSourceAnnotation) {
    if (aTargetAnnotSet == 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
    AnnotationSet as = aTargetAnnotSet.get(start, end);
    // 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 : AnnotationSet(gate.AnnotationSet) Annotation(gate.Annotation)

Example 20 with AnnotationSet

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

the class DocumentXmlUtils method toXml.

/**
 * Returns a GateXml document that is a custom XML format for wich there is a
 * reader inside GATE called gate.xml.GateFormatXmlHandler. What it does is to
 * serialize a GATE document in an XML format.
 *
 * @param doc the document to serialize.
 * @return a string representing a Gate Xml document.
 */
public static String toXml(TextualDocument doc) {
    // Initialize the xmlContent several time the size of the current document.
    // This is because of the tags size. This measure is made to increase the
    // performance of StringBuffer.
    StringBuffer xmlContent = new StringBuffer(DOC_SIZE_MULTIPLICATION_FACTOR * (doc.getContent().size().intValue()));
    // Add xml header
    xmlContent.append("<?xml version=\"1.0\" encoding=\"");
    xmlContent.append(doc.getEncoding());
    xmlContent.append("\" ?>");
    xmlContent.append(Strings.getNl());
    // Add the root element
    xmlContent.append("<GateDocument>\n");
    xmlContent.append("<!-- The document's features-->\n\n");
    xmlContent.append("<GateDocumentFeatures>\n");
    xmlContent.append(featuresToXml(doc.getFeatures(), null));
    xmlContent.append("</GateDocumentFeatures>\n");
    xmlContent.append("<!-- The document content area with serialized" + " nodes -->\n\n");
    // Add plain text element
    xmlContent.append("<TextWithNodes>");
    xmlContent.append(textWithNodes(doc, doc.getContent().toString()));
    xmlContent.append("</TextWithNodes>\n");
    // Serialize as XML all document's annotation sets
    // Serialize the default AnnotationSet
    StatusListener sListener = (StatusListener) gate.Gate.getListeners().get("gate.event.StatusListener");
    if (sListener != null)
        sListener.statusChanged("Saving the default annotation set ");
    xmlContent.append("<!-- The default annotation set -->\n\n");
    annotationSetToXml(doc.getAnnotations(), xmlContent);
    // Serialize all others AnnotationSets
    // namedAnnotSets is a Map containing all other named Annotation Sets.
    Map<String, AnnotationSet> namedAnnotSets = doc.getNamedAnnotationSets();
    if (namedAnnotSets != null) {
        Iterator<AnnotationSet> iter = namedAnnotSets.values().iterator();
        while (iter.hasNext()) {
            AnnotationSet annotSet = iter.next();
            xmlContent.append("<!-- Named annotation set -->\n\n");
            // Serialize it as XML
            if (sListener != null)
                sListener.statusChanged("Saving " + annotSet.getName() + " annotation set ");
            annotationSetToXml(annotSet, xmlContent);
        }
    // End while
    }
    // End if
    // Add the end of GateDocument
    xmlContent.append("</GateDocument>");
    if (sListener != null)
        sListener.statusChanged("Done !");
    // return the XmlGateDocument
    return xmlContent.toString();
}
Also used : AnnotationSet(gate.AnnotationSet) StatusListener(gate.event.StatusListener)

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