Search in sources :

Example 6 with ResourceInstantiationException

use of gate.creole.ResourceInstantiationException in project gate-core by GateNLP.

the class CorpusBenchmarkTool method evaluateAllThree.

protected void evaluateAllThree(Document persDoc, Document cleanDoc, Document markedDoc, File errDir) throws ResourceInstantiationException {
    // first start the table and its header
    printTableHeader();
    // store annotation diff in .err file
    Writer errWriter = null;
    if (isMoreInfoMode && errDir != null) {
        StringBuffer docName = new StringBuffer(cleanDoc.getName());
        docName.replace(cleanDoc.getName().lastIndexOf("."), docName.length(), ".err");
        File errFile = new File(errDir, docName.toString());
        // String encoding = ( (gate.corpora.DocumentImpl) cleanDoc).getEncoding();
        try {
            errWriter = new FileWriter(errFile, false);
        /*
                 if(encoding == null) {
          errWriter = new OutputStreamWriter(
              new FileOutputStream(errFile, false));
                 } else {
          errWriter = new OutputStreamWriter(
              new FileOutputStream(errFile, false), encoding);
                 }*/
        } catch (Exception ex) {
            Out.prln("Exception when creating the error file " + errFile + ": " + ex.getMessage());
            errWriter = null;
        }
    }
    for (int jj = 0; jj < annotTypes.size(); jj++) {
        String annotType = annotTypes.get(jj);
        AnnotationDiffer annotDiffer = measureDocs(markedDoc, cleanDoc, annotType);
        // we don't have this annotation type in this document
        if (annotDiffer == null)
            continue;
        // increase the number of processed documents
        docNumber++;
        // add precison and recall to the sums
        updateStatistics(annotDiffer, annotType);
        AnnotationDiffer annotDiffer1 = measureDocs(markedDoc, persDoc, annotType);
        Out.prln("<TR>");
        if (isMoreInfoMode && annotDiffer1 != null && (annotDiffer1.getPrecisionAverage() != annotDiffer.getPrecisionAverage() || annotDiffer1.getRecallAverage() != annotDiffer.getRecallAverage()))
            Out.prln("<TD> " + annotType + "_new" + "</TD>");
        else
            Out.prln("<TD> " + annotType + "</TD>");
        if (isMoreInfoMode) {
            if (annotDiffer1 != null)
                updateStatisticsProc(annotDiffer1, annotType);
            Out.prln("<TD>" + annotDiffer.getCorrectMatches() + "</TD>");
            Out.prln("<TD>" + annotDiffer.getPartiallyCorrectMatches() + "</TD>");
            Out.prln("<TD>" + annotDiffer.getMissing() + "</TD>");
            Out.prln("<TD>" + annotDiffer.getSpurious() + "</TD>");
        }
        Out.prln("<TD>");
        // check the precision first
        if (annotDiffer1 != null) {
            if (annotDiffer1.getPrecisionAverage() < annotDiffer.getPrecisionAverage()) {
                Out.prln("<P><Font color=blue> ");
                Out.prln(annotDiffer.getPrecisionAverage());
                if (!isMoreInfoMode) {
                    Out.pr("<BR>Precision increase on human-marked from ");
                    Out.pr(annotDiffer1.getPrecisionAverage() + " to ");
                    Out.prln(annotDiffer.getPrecisionAverage());
                }
                Out.prln(" </Font></P>");
            } else if (annotDiffer1.getPrecisionAverage() > annotDiffer.getPrecisionAverage()) {
                Out.prln("<P><Font color=red> ");
                Out.prln(annotDiffer.getPrecisionAverage());
                if (!isMoreInfoMode) {
                    Out.pr("<BR>Precision decrease on human-marked from ");
                    Out.pr(annotDiffer1.getPrecisionAverage() + " to ");
                    Out.prln(annotDiffer.getPrecisionAverage());
                }
                Out.prln(" </Font></P>");
            } else
                Out.prln("<P> " + annotDiffer.getPrecisionAverage() + " </P>");
        } else
            Out.prln("<P> " + annotDiffer.getPrecisionAverage() + " </P>");
        Out.prln("</TD>");
        Out.prln("<TD>");
        // check the recall now
        if (annotDiffer1 != null) {
            if (annotDiffer1.getRecallAverage() < annotDiffer.getRecallAverage()) {
                Out.prln("<P><Font color=blue> ");
                Out.prln(annotDiffer.getRecallAverage());
                if (!isMoreInfoMode) {
                    Out.pr("<BR>Recall increase on human-marked from ");
                    Out.pr(annotDiffer1.getRecallAverage() + " to ");
                    Out.prln(annotDiffer.getRecallAverage());
                }
                Out.prln(" </Font></P>");
            } else if (annotDiffer1.getRecallAverage() > annotDiffer.getRecallAverage()) {
                Out.prln("<P><Font color=red> ");
                Out.prln(annotDiffer.getRecallAverage());
                if (!isMoreInfoMode) {
                    Out.pr("<BR>Recall decrease on human-marked from ");
                    Out.pr(annotDiffer1.getRecallAverage() + " to ");
                    Out.prln(annotDiffer.getRecallAverage());
                }
                Out.prln(" </Font></P>");
            } else
                Out.prln("<P> " + annotDiffer.getRecallAverage() + " </P>");
        } else
            Out.prln("<P> " + annotDiffer.getRecallAverage() + " </P>");
        Out.prln("</TD>");
        // check the recall now
        if (isVerboseMode) {
            Out.prln("<TD>");
            if (annotDiffer.getRecallAverage() < threshold || annotDiffer.getPrecisionAverage() < threshold) {
                printAnnotations(annotDiffer, markedDoc, cleanDoc);
            } else {
                Out.prln("&nbsp;");
            }
            Out.prln("</TD>");
        }
        Out.prln("</TR>");
        // show one more table line for processed document
        if (isMoreInfoMode && annotDiffer1 != null && (annotDiffer1.getPrecisionAverage() != annotDiffer.getPrecisionAverage() || annotDiffer1.getRecallAverage() != annotDiffer.getRecallAverage())) {
            Out.prln("<TR>");
            Out.prln("<TD> " + annotType + "_old" + "</TD>");
            Out.prln("<TD>" + annotDiffer1.getCorrectMatches() + "</TD>");
            Out.prln("<TD>" + annotDiffer1.getPartiallyCorrectMatches() + "</TD>");
            Out.prln("<TD>" + annotDiffer1.getMissing() + "</TD>");
            Out.prln("<TD>" + annotDiffer1.getSpurious() + "</TD>");
            Out.prln("<TD>");
            if (annotDiffer1.getPrecisionAverage() < annotDiffer.getPrecisionAverage())
                Out.prln("<P><Font color=blue> " + annotDiffer1.getPrecisionAverage() + "</Font></P>");
            else if (annotDiffer1.getPrecisionAverage() > annotDiffer.getPrecisionAverage())
                Out.prln("<P><Font color=red> " + annotDiffer1.getPrecisionAverage() + " </Font></P>");
            else
                Out.prln(annotDiffer1.getPrecisionAverage());
            Out.prln("</TD>");
            Out.prln("<TD>");
            if (annotDiffer1.getRecallAverage() < annotDiffer.getRecallAverage())
                Out.prln("<P><Font color=blue> " + annotDiffer1.getRecallAverage() + " </Font></P>");
            else if (annotDiffer1.getRecallAverage() > annotDiffer.getRecallAverage())
                Out.prln("<P><Font color=red> " + annotDiffer1.getRecallAverage() + " </Font></P>");
            else
                Out.prln(annotDiffer1.getRecallAverage());
            Out.prln("</TD>");
            // check the recall now
            if (isVerboseMode) {
                // create error file and start writing
                Out.prln("<TD>");
                if (annotDiffer.getRecallAverage() < threshold || annotDiffer.getPrecisionAverage() < threshold) {
                    printAnnotations(annotDiffer, markedDoc, cleanDoc);
                } else {
                    Out.prln("&nbsp;");
                }
                Out.prln("</TD>");
            }
            Out.prln("</TR>");
        }
        if (isMoreInfoMode && errDir != null)
            storeAnnotations(annotType, annotDiffer, markedDoc, cleanDoc, errWriter);
    }
    // for loop through annotation types
    Out.prln("</TABLE>");
    try {
        if (errWriter != null)
            errWriter.close();
    } catch (Exception ex) {
        Out.prln("Exception on close of error file " + errWriter + ": " + ex.getMessage());
    }
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) ResourceInstantiationException(gate.creole.ResourceInstantiationException) ExecutionException(gate.creole.ExecutionException) IOException(java.io.IOException) PersistenceException(gate.persist.PersistenceException)

Example 7 with ResourceInstantiationException

use of gate.creole.ResourceInstantiationException in project gate-core by GateNLP.

the class ResourcePersistence method extractDataFromSource.

@Override
public void extractDataFromSource(Object source) throws PersistenceException {
    if (!(source instanceof Resource)) {
        throw new UnsupportedOperationException(getClass().getName() + " can only be used for " + Resource.class.getName() + " objects!\n" + source.getClass().getName() + " is not a " + Resource.class.getName());
    }
    Resource res = (Resource) source;
    resourceType = res.getClass().getName();
    resourceName = ((NameBearer) res).getName();
    ResourceData rData = Gate.getCreoleRegister().get(resourceType);
    if (rData == null)
        throw new PersistenceException("Could not find CREOLE data for " + resourceType);
    ParameterList params = rData.getParameterList();
    try {
        // get the values for the init time parameters
        initParams = Factory.newFeatureMap();
        // this is a list of lists
        Iterator<List<Parameter>> parDisjIter = params.getInitimeParameters().iterator();
        while (parDisjIter.hasNext()) {
            Iterator<Parameter> parIter = parDisjIter.next().iterator();
            while (parIter.hasNext()) {
                Parameter parameter = parIter.next();
                String parName = parameter.getName();
                Object parValue = res.getParameterValue(parName);
                if (storeParameterValue(parValue, parameter.getDefaultValue())) {
                    ((FeatureMap) initParams).put(parName, parValue);
                }
            }
        }
        initParams = PersistenceManager.getPersistentRepresentation(initParams);
        // get the features
        if (res.getFeatures() != null) {
            features = Factory.newFeatureMap();
            ((FeatureMap) features).putAll(res.getFeatures());
            features = PersistenceManager.getPersistentRepresentation(features);
        }
    } catch (ResourceInstantiationException | ParameterException rie) {
        throw new PersistenceException(rie);
    }
}
Also used : ResourceData(gate.creole.ResourceData) Resource(gate.Resource) ResourceInstantiationException(gate.creole.ResourceInstantiationException) FeatureMap(gate.FeatureMap) PersistenceException(gate.persist.PersistenceException) ParameterList(gate.creole.ParameterList) Parameter(gate.creole.Parameter) ParameterList(gate.creole.ParameterList) List(java.util.List) ParameterException(gate.creole.ParameterException)

Example 8 with ResourceInstantiationException

use of gate.creole.ResourceInstantiationException in project gate-core by GateNLP.

the class CorpusAnnotationDiff method setParameterValue.

/**
 * Sets the value for a specified parameter.
 *
 * @param paramaterName the name for the parameteer
 * @param parameterValue the value the parameter will receive
 */
@Override
public void setParameterValue(String paramaterName, Object parameterValue) throws ResourceInstantiationException {
    // get the beaninfo for the resource bean, excluding data about Object
    BeanInfo resBeanInf = null;
    try {
        resBeanInf = Introspector.getBeanInfo(this.getClass(), Object.class);
    } catch (Exception e) {
        throw new ResourceInstantiationException("Couldn't get bean info for resource " + this.getClass().getName() + Strings.getNl() + "Introspector exception was: " + e);
    }
    AbstractResource.setParameterValue(this, resBeanInf, paramaterName, parameterValue);
}
Also used : BeanInfo(java.beans.BeanInfo) ResourceInstantiationException(gate.creole.ResourceInstantiationException) ResourceInstantiationException(gate.creole.ResourceInstantiationException)

Example 9 with ResourceInstantiationException

use of gate.creole.ResourceInstantiationException 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 10 with ResourceInstantiationException

use of gate.creole.ResourceInstantiationException in project gate-core by GateNLP.

the class CreoleProxy method defaultDuplicate.

/**
 * Implementation of the default duplication algorithm described in
 * the comment for {@link #duplicate(Resource)}. This method is public
 * for the benefit of resources that implement
 * {@link CustomDuplication} but only need to do some post-processing
 * after the default duplication algorithm; they can call this method
 * to obtain an initial duplicate and then post-process it before
 * returning. If they need to duplicate child resources they should
 * call {@link #duplicate(Resource, DuplicationContext)} in the normal
 * way. Calls to this method made outside the context of a
 * {@link CustomDuplication#duplicate CustomDuplication.duplicate}
 * call will fail with a runtime exception.
 *
 * @param res the resource to duplicate
 * @param ctx the current context
 * @return a duplicate of the given resource, constructed using the
 *         default algorithm. In particular, if <code>res</code>
 *         implements {@link CustomDuplication} its own duplicate
 *         method will <i>not</i> be called.
 * @throws ResourceInstantiationException if an error occurs while
 *           duplicating the given resource.
 */
public static Resource defaultDuplicate(Resource res, DuplicationContext ctx) throws ResourceInstantiationException {
    checkDuplicationContext(ctx);
    String className = res.getClass().getName();
    ResourceData resData = Gate.getCreoleRegister().get(className);
    if (resData == null) {
        throw new ResourceInstantiationException("Could not find CREOLE data for " + className);
    }
    String resName = res.getName();
    FeatureMap newResFeatures = duplicate(res.getFeatures(), ctx);
    // init parameters
    FeatureMap initParams = AbstractResource.getInitParameterValues(res);
    // remove parameters that are also sharable properties
    for (String propName : resData.getSharableProperties()) {
        initParams.remove(propName);
    }
    // duplicate any Resources in the params map (excluding sharable
    // ones)
    initParams = duplicate(initParams, ctx);
    // may be registered parameters but others may not be.
    for (String propName : resData.getSharableProperties()) {
        initParams.put(propName, res.getParameterValue(propName));
    }
    // create the new resource
    Resource newResource = createResource(className, initParams, newResFeatures, resName);
    if (newResource instanceof ProcessingResource) {
        // runtime params
        FeatureMap runtimeParams = AbstractProcessingResource.getRuntimeParameterValues(res);
        // remove parameters that are also sharable properties
        for (String propName : resData.getSharableProperties()) {
            runtimeParams.remove(propName);
        }
        // duplicate any Resources in the params map (excluding sharable
        // ones)
        runtimeParams = duplicate(runtimeParams, ctx);
        // do not need to add sharable properties here, they have already
        // been injected by createResource
        newResource.setParameterValues(runtimeParams);
    }
    return newResource;
}
Also used : ResourceData(gate.creole.ResourceData) AbstractProcessingResource(gate.creole.AbstractProcessingResource) AbstractResource(gate.creole.AbstractResource) AbstractProcessingResource(gate.creole.AbstractProcessingResource) ResourceInstantiationException(gate.creole.ResourceInstantiationException)

Aggregations

ResourceInstantiationException (gate.creole.ResourceInstantiationException)29 FeatureMap (gate.FeatureMap)10 PersistenceException (gate.persist.PersistenceException)10 ResourceData (gate.creole.ResourceData)9 GateRuntimeException (gate.util.GateRuntimeException)6 Document (gate.Document)5 File (java.io.File)5 ParameterException (gate.creole.ParameterException)4 SerialDataStore (gate.persist.SerialDataStore)4 IOException (java.io.IOException)4 List (java.util.List)4 ProcessingResource (gate.ProcessingResource)3 Resource (gate.Resource)3 ExecutionException (gate.creole.ExecutionException)3 Parameter (gate.creole.Parameter)3 ParameterList (gate.creole.ParameterList)3 Plugin (gate.creole.Plugin)3 Dimension (java.awt.Dimension)3 BeanInfo (java.beans.BeanInfo)3 URL (java.net.URL)3