Search in sources :

Example 16 with FeatureMap

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

the class TestXSchema method testFactoryCreation.

// testFromAndToXSchema()
/**
 * Test creation of annotation schemas via gate.Factory
 */
public void testFactoryCreation() throws Exception {
    ResourceData resData = Gate.getCreoleRegister().get("gate.creole.AnnotationSchema");
    FeatureMap parameters = Factory.newFeatureMap();
    parameters.put(AnnotationSchema.FILE_URL_PARAM_NAME, new URL(TestDocument.getTestServerName() + "tests/xml/POSSchema.xml"));
    AnnotationSchema schema = (AnnotationSchema) Factory.createResource("gate.creole.AnnotationSchema", parameters);
    if (DEBUG) {
        Out.prln("schema RD: " + resData);
        Out.prln("schema: " + schema);
    }
}
Also used : FeatureMap(gate.FeatureMap) URL(java.net.URL)

Example 17 with FeatureMap

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

the class TestXSchema method testFromAndToXSchema.

// setUp
/**
 * A test
 */
public void testFromAndToXSchema() throws Exception {
    // esourceData resData = Gate.getCreoleRegister().get("gate.creole.AnnotationSchema");
    FeatureMap parameters = Factory.newFeatureMap();
    parameters.put(AnnotationSchema.FILE_URL_PARAM_NAME, new URL(TestDocument.getTestServerName() + "tests/xml/POSSchema.xml"));
    AnnotationSchema annotSchema = (AnnotationSchema) Factory.createResource("gate.creole.AnnotationSchema", parameters);
    String s = annotSchema.toXSchema();
    // write back the XSchema fom memory
    // File file = Files.writeTempFile(new ByteArrayInputStream(s.getBytes()));
    // load it again.
    // annotSchema.fromXSchema(file.toURI().toURL());
    annotSchema.fromXSchema(new ByteArrayInputStream(s.getBytes()));
}
Also used : FeatureMap(gate.FeatureMap) ByteArrayInputStream(java.io.ByteArrayInputStream) URL(java.net.URL)

Example 18 with FeatureMap

use of gate.FeatureMap 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 19 with FeatureMap

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

the class ResourcePersistence method createObject.

@Override
public Object createObject() throws PersistenceException, ResourceInstantiationException {
    if (initParams != null)
        initParams = PersistenceManager.getTransientRepresentation(initParams, containingControllerName, initParamOverrides);
    if (features != null)
        features = PersistenceManager.getTransientRepresentation(features, containingControllerName, initParamOverrides);
    if (initParamOverrides != null) {
        // check if there is a map for this resource Id in the overrides
        String containingControllerNameToUse = containingControllerName == null ? "" : containingControllerName;
        String resourceId = containingControllerNameToUse + "\t" + resourceName;
        if (initParamOverrides.containsKey(resourceId)) {
            Map<String, Object> parmOverrides = initParamOverrides.get(resourceId);
            if (initParams instanceof FeatureMap) {
                FeatureMap fm = (FeatureMap) initParams;
                // do this in a loop instead of using putAll so we can log the changes
                for (String name : parmOverrides.keySet()) {
                    Object obj = parmOverrides.get(name);
                    log.info("Overriding init parameter " + name + " for " + containingControllerNameToUse + "//" + resourceName + " with " + obj);
                    fm.put(name, obj);
                }
            }
        }
    }
    Resource res = Factory.createResource(resourceType, (FeatureMap) initParams, (FeatureMap) features, resourceName);
    return res;
}
Also used : FeatureMap(gate.FeatureMap) Resource(gate.Resource)

Example 20 with FeatureMap

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

the class TestPersist method testMultipleLrs.

// testSimple()
/**
 * Test multiple LRs
 */
public void testMultipleLrs() throws Exception {
    // create a temporary directory; because File.createTempFile actually
    // writes the bloody thing, we need to delete it from disk before calling
    // DataStore.create
    File storageDir = File.createTempFile("TestPersist__", "__StorageDir");
    storageDir.delete();
    // create and open a serial data store
    SerialDataStore sds = new SerialDataStore(storageDir.toURI().toURL().toString());
    sds.create();
    sds.open();
    // create a document with some annotations / features on it
    String server = TestDocument.getTestServerName();
    Document doc = Factory.newDocument(new URL(server + "tests/doc0.html"));
    doc.getFeatures().put("hi there", new Integer(23232));
    doc.getAnnotations().add(new Long(5), new Long(25), "ThingyMaJig", Factory.newFeatureMap());
    // create another document with some annotations / features on it
    Document doc2 = Factory.newDocument(new URL(server + "tests/html/test1.htm"));
    doc.getFeatures().put("hi there again", new Integer(23232));
    doc.getAnnotations().add(new Long(5), new Long(25), "dog poo irritates", Factory.newFeatureMap());
    // create a corpus with the documents
    Corpus corp = Factory.newCorpus("Hamish test corpus");
    corp.add(doc);
    corp.add(doc2);
    LanguageResource persCorpus = sds.adopt(corp);
    sds.sync(persCorpus);
    // read the documents back
    List<Resource> lrsFromDisk = new ArrayList<Resource>();
    List<String> lrIds = sds.getLrIds("gate.corpora.SerialCorpusImpl");
    Iterator<String> idsIter = lrIds.iterator();
    while (idsIter.hasNext()) {
        String lrId = idsIter.next();
        FeatureMap features = Factory.newFeatureMap();
        features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
        features.put(DataStore.LR_ID_FEATURE_NAME, lrId);
        Resource lr = Factory.createResource("gate.corpora.SerialCorpusImpl", features);
        lrsFromDisk.add(lr);
    }
    if (DEBUG)
        System.out.println("LRs on disk" + lrsFromDisk);
    // check that the versions we read back match the originals
    Corpus diskCorp = (Corpus) lrsFromDisk.get(0);
    Document diskDoc = diskCorp.get(0);
    if (DEBUG)
        Out.prln("Documents in corpus: " + corp.getDocumentNames());
    assertTrue("corp name != mem name", corp.getName().equals(diskCorp.getName()));
    if (DEBUG)
        Out.prln("Memory features " + corp.getFeatures());
    if (DEBUG)
        Out.prln("Disk features " + diskCorp.getFeatures());
    assertTrue("corp feat != mem feat", corp.getFeatures().equals(diskCorp.getFeatures()));
    if (DEBUG)
        Out.prln("Annotations in doc: " + diskDoc.getAnnotations());
    assertTrue("doc annotations from disk not equal to memory version", TestEqual.annotationSetsEqual(doc.getAnnotations(), diskDoc.getAnnotations()));
    assertTrue("doc from disk not equal to memory version", TestEqual.documentsEqual(doc, diskDoc));
    Iterator<Document> corpusIter = diskCorp.iterator();
    while (corpusIter.hasNext()) {
        if (DEBUG)
            Out.prln(corpusIter.next().getName());
        else
            corpusIter.next();
    }
    // assertTrue("doc2 from disk not equal to memory version",
    // doc2.equals(diskDoc2));
    // delete the datastore
    sds.delete();
}
Also used : LanguageResource(gate.LanguageResource) Resource(gate.Resource) LanguageResource(gate.LanguageResource) ArrayList(java.util.ArrayList) TestDocument(gate.corpora.TestDocument) Document(gate.Document) URL(java.net.URL) Corpus(gate.Corpus) FeatureMap(gate.FeatureMap) File(java.io.File)

Aggregations

FeatureMap (gate.FeatureMap)55 Document (gate.Document)15 URL (java.net.URL)14 ResourceInstantiationException (gate.creole.ResourceInstantiationException)11 File (java.io.File)10 Resource (gate.Resource)8 GateRuntimeException (gate.util.GateRuntimeException)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 PersistenceException (gate.persist.PersistenceException)6 Annotation (gate.Annotation)5 AnnotationSet (gate.AnnotationSet)5 DataStore (gate.DataStore)5 LanguageResource (gate.LanguageResource)5 TestDocument (gate.corpora.TestDocument)4 ResourceData (gate.creole.ResourceData)4 SerialDataStore (gate.persist.SerialDataStore)4 InvalidOffsetException (gate.util.InvalidOffsetException)4 Corpus (gate.Corpus)3 ProcessingResource (gate.ProcessingResource)3