Search in sources :

Example 11 with LanguageResource

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

the class TestCreole method testIntrospection.

// testArbitraryMetadata()
/**
 * Test resource introspection
 */
@SuppressWarnings("unused")
public void testIntrospection() throws Exception {
    // get the gate.Document resource and its class
    ResourceData docRd = reg.get("gate.corpora.DocumentImpl");
    assertNotNull("couldn't find document res data (2)", docRd);
    Class<?> resClass = docRd.getResourceClass();
    // get the beaninfo and property descriptors for the resource
    BeanInfo docBeanInfo = Introspector.getBeanInfo(resClass, Object.class);
    PropertyDescriptor[] propDescrs = docBeanInfo.getPropertyDescriptors();
    // print all the properties in the reource's bean info;
    // remember the setFeatures method
    Method setFeaturesMethod = null;
    for (int i = 0; i < propDescrs.length; i++) {
        Method getMethodDescr = null;
        Method setMethodDescr = null;
        Class<?> propClass = null;
        PropertyDescriptor propDescr = propDescrs[i];
        propClass = propDescr.getPropertyType();
        getMethodDescr = propDescr.getReadMethod();
        setMethodDescr = propDescr.getWriteMethod();
        if (setMethodDescr != null && setMethodDescr.getName().equals("setFeatures"))
            setFeaturesMethod = setMethodDescr;
        if (DEBUG)
            printProperty(propDescrs[i]);
    }
    // try setting the features property
    // invoke(Object obj, Object[] args)
    LanguageResource res = (LanguageResource) resClass.newInstance();
    FeatureMap feats = Factory.newFeatureMap();
    feats.put("things are sunny in sunny countries", "aren't they?");
    Object[] args = new Object[1];
    args[0] = feats;
    setFeaturesMethod.invoke(res, args);
    assertTrue("features not added to resource properly", res.getFeatures().get("things are sunny in sunny countries").equals("aren't they?"));
}
Also used : FeatureMap(gate.FeatureMap) PropertyDescriptor(java.beans.PropertyDescriptor) LanguageResource(gate.LanguageResource) SimpleBeanInfo(java.beans.SimpleBeanInfo) BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method)

Example 12 with LanguageResource

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

the class SerialControllerEditor method resourceUnloaded.

@Override
public void resourceUnloaded(CreoleEvent e) {
    if (Gate.getHiddenAttribute(e.getResource().getFeatures()))
        return;
    if (e.getResource() instanceof ProcessingResource) {
        ProcessingResource pr = (ProcessingResource) e.getResource();
        if (controller != null && controller.getPRs().contains(pr)) {
            controller.remove(pr);
        }
        refreshPRLists();
    } else if (e.getResource() instanceof LanguageResource) {
        if (e.getResource() instanceof Corpus && corpusControllerMode) {
            Corpus c = (Corpus) e.getResource();
            if (controller instanceof CorpusController) {
                if (c == ((CorpusController) controller).getCorpus()) {
                    // setCorpus(null) is also called in the controller's
                    // resourceUnloaded(), but we can't be sure which handler is
                    // called first...
                    ((CorpusController) controller).setCorpus(null);
                }
            } else {
                throw new GateRuntimeException("Controller editor in analyser mode " + "but the target controller is not an " + "analyser!");
            }
            corpusComboModel.fireDataChanged();
        }
    }
}
Also used : LanguageResource(gate.LanguageResource) CorpusController(gate.CorpusController) GateRuntimeException(gate.util.GateRuntimeException) ProcessingResource(gate.ProcessingResource) Corpus(gate.Corpus)

Example 13 with LanguageResource

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

the class CorpusBenchmarkTool method generateCorpus.

// setStartDirectory
protected void generateCorpus(File fileDir, File outputDir) {
    // 1. check if we have input files
    if (fileDir == null)
        return;
    // 2. create the output directory or clean it up if needed
    File outDir = outputDir;
    if (outputDir == null) {
        outDir = new File(currDir, PROCESSED_DIR_NAME);
    } else {
        // get rid of the directory, coz datastore wants it clean
        if (!Files.rmdir(outDir))
            Out.prln("cannot delete old output directory: " + outDir);
    }
    outDir.mkdir();
    // create the datastore and process each document
    try {
        SerialDataStore sds = new SerialDataStore(outDir.toURI().toURL().toString());
        sds.create();
        sds.open();
        File[] files = fileDir.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (!files[i].isFile())
                continue;
            // create a document
            Out.prln("Processing and storing document: " + files[i].toURI().toURL() + "<P>");
            FeatureMap params = Factory.newFeatureMap();
            params.put(Document.DOCUMENT_URL_PARAMETER_NAME, files[i].toURI().toURL());
            params.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, documentEncoding);
            FeatureMap features = Factory.newFeatureMap();
            // Gate.setHiddenAttribute(features, true);
            // create the document
            final Document doc = (Document) Factory.createResource("gate.corpora.DocumentImpl", params, features);
            doc.setName(files[i].getName());
            processDocument(doc);
            final LanguageResource lr = sds.adopt(doc);
            sds.sync(lr);
            javax.swing.SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    Factory.deleteResource(doc);
                    Factory.deleteResource(lr);
                }
            });
        }
        // for
        sds.close();
    } catch (java.net.MalformedURLException ex) {
        throw (GateRuntimeException) new GateRuntimeException("CorpusBenchmark: " + ex.getMessage()).initCause(ex);
    } catch (PersistenceException ex1) {
        throw (GateRuntimeException) new GateRuntimeException("CorpusBenchmark: " + ex1.getMessage()).initCause(ex1);
    } catch (ResourceInstantiationException ex2) {
        throw (GateRuntimeException) new GateRuntimeException("CorpusBenchmark: " + ex2.getMessage()).initCause(ex2);
    }
}
Also used : LanguageResource(gate.LanguageResource) Document(gate.Document) ResourceInstantiationException(gate.creole.ResourceInstantiationException) FeatureMap(gate.FeatureMap) PersistenceException(gate.persist.PersistenceException) File(java.io.File) SerialDataStore(gate.persist.SerialDataStore)

Aggregations

LanguageResource (gate.LanguageResource)13 Document (gate.Document)5 Corpus (gate.Corpus)4 FeatureMap (gate.FeatureMap)4 ProcessingResource (gate.ProcessingResource)4 Resource (gate.Resource)4 Controller (gate.Controller)3 ConditionalSerialAnalyserController (gate.creole.ConditionalSerialAnalyserController)3 File (java.io.File)3 CorpusController (gate.CorpusController)2 VisualResource (gate.VisualResource)2 AnnotationSchema (gate.creole.AnnotationSchema)2 PackagedController (gate.creole.PackagedController)2 GateRuntimeException (gate.util.GateRuntimeException)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 DataStore (gate.DataStore)1 TestDocument (gate.corpora.TestDocument)1 AbstractVisualResource (gate.creole.AbstractVisualResource)1 ConditionalController (gate.creole.ConditionalController)1 ResourceInstantiationException (gate.creole.ResourceInstantiationException)1