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?"));
}
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();
}
}
}
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);
}
}
Aggregations