Search in sources :

Example 1 with DataStore

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

the class MainFrame method createSerialDataStore.

// setTitle(String title)
/**
 * Method is used in NewDSAction
 * @return the new datastore or null if an error occurs
 */
protected DataStore createSerialDataStore() {
    DataStore ds = null;
    // get the URL (a file in this case)
    fileChooser.setDialogTitle("Please create a new empty directory");
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.setFileFilter(fileChooser.getAcceptAllFileFilter());
    fileChooser.setResource("gate.persist.SerialDataStore");
    if (fileChooser.showOpenDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
        try {
            URL dsURL = fileChooser.getSelectedFile().toURI().toURL();
            ds = Factory.createDataStore("gate.persist.SerialDataStore", dsURL.toExternalForm());
        } catch (MalformedURLException mue) {
            JOptionPane.showMessageDialog(MainFrame.this, "Invalid location for the datastore\n " + mue.toString(), "GATE", JOptionPane.ERROR_MESSAGE);
        } catch (PersistenceException pe) {
            JOptionPane.showMessageDialog(MainFrame.this, "Datastore creation error!\n " + pe.toString(), "GATE", JOptionPane.ERROR_MESSAGE);
        }
    // catch
    }
    return ds;
}
Also used : MalformedURLException(java.net.MalformedURLException) DataStore(gate.DataStore) PersistenceException(gate.persist.PersistenceException) URL(java.net.URL)

Example 2 with DataStore

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

the class MainFrame method openSearchableDataStore.

// createSearchableDataStore()
/**
 * Method is used in OpenDSAction
 * @return the opened datastore or null if an error occurs
 */
protected DataStore openSearchableDataStore() {
    DataStore ds = null;
    // get the URL (a file in this case)
    fileChooser.setDialogTitle("Select the datastore directory");
    fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fileChooser.setResource("gate.DataStore.data");
    if (fileChooser.showOpenDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
        try {
            URL dsURL = fileChooser.getSelectedFile().toURI().toURL();
            ds = Factory.openDataStore("gate.persist.LuceneDataStoreImpl", dsURL.toExternalForm());
        } catch (MalformedURLException mue) {
            JOptionPane.showMessageDialog(MainFrame.this, "Invalid location for the datastore\n " + mue.toString(), "GATE", JOptionPane.ERROR_MESSAGE);
        } catch (PersistenceException pe) {
            JOptionPane.showMessageDialog(MainFrame.this, "Datastore opening error!\n " + pe.toString(), "GATE", JOptionPane.ERROR_MESSAGE);
        }
    // catch
    }
    return ds;
}
Also used : MalformedURLException(java.net.MalformedURLException) DataStore(gate.DataStore) PersistenceException(gate.persist.PersistenceException) URL(java.net.URL)

Example 3 with DataStore

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

the class MainFrame method datastoreOpened.

/**
 * Called when a {@link gate.DataStore} has been opened
 */
@Override
public void datastoreOpened(CreoleEvent e) {
    final DataStore ds = e.getDatastore();
    if (ds.getName() == null || ds.getName().length() == 0) {
        String name = ds.getStorageUrl();
        StringBuilder nameBuilder = new StringBuilder();
        // quick and dirty FSA
        int state = 0;
        for (int i = name.length() - 1; i >= 0 && state != 2; i--) {
            char currentChar = name.charAt(i);
            switch(state) {
                case 0:
                    // consuming slashes at the end
                    if (currentChar == '/') {
                    // consume
                    } else {
                        // we got the first non-slash char
                        state = 1;
                        nameBuilder.insert(0, currentChar);
                    }
                    break;
                case 1:
                    // eating up name chars
                    if (currentChar == '/') {
                        // we're done!
                        state = 2;
                    } else {
                        // we got a non-slash char
                        nameBuilder.insert(0, currentChar);
                    }
                    break;
                default:
                    throw new GateRuntimeException("A phanthom state of things!");
            }
        }
        if (nameBuilder.length() > 0)
            name = nameBuilder.toString();
        ds.setName(name);
    }
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            NameBearerHandle handle = new NameBearerHandle(ds, MainFrame.this);
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(handle, false);
            resourcesTreeModel.insertNodeInto(node, datastoresRoot, 0);
            handle.addProgressListener(MainFrame.this);
            handle.addStatusListener(MainFrame.this);
        }
    });
// JPopupMenu popup = handle.getPopup();
// popup.addSeparator();
// // Create a CloseViewAction and a menu item based on it
// CloseViewAction cva = new CloseViewAction(handle);
// XJMenuItem menuItem = new XJMenuItem(cva, this);
// // Add an accelerator ATL+F4 for this action
// menuItem.setAccelerator(KeyStroke.getKeyStroke(
// KeyEvent.VK_H, ActionEvent.CTRL_MASK));
// popup.add(menuItem);
// // Put the action command in the component's action map
// if (handle.getLargeView() != null)
// handle.getLargeView().getActionMap().put("Hide current
// view",cva);
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) DataStore(gate.DataStore) GateRuntimeException(gate.util.GateRuntimeException) Point(java.awt.Point)

Example 4 with DataStore

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

the class SerialDataStore method adopt.

// delete(lr)
/**
 * Adopt a resource for persistence.
 */
@Override
public LanguageResource adopt(LanguageResource lr) throws PersistenceException {
    // ignore security info
    // check the LR's current DS
    DataStore currentDS = lr.getDataStore();
    if (currentDS == null) {
        // an orphan - do the adoption
        LanguageResource res = lr;
        if (lr instanceof Corpus) {
            FeatureMap features1 = Factory.newFeatureMap();
            features1.put("transientSource", lr);
            try {
                // here we create the persistent LR via Factory, so it's registered
                // in GATE
                res = (LanguageResource) Factory.createResource("gate.corpora.SerialCorpusImpl", features1);
            // Here the transient corpus is not deleted from the CRI, because
            // this might not always be the desired behaviour
            // since we chose that it is for the GUI, this functionality is
            // now move to the 'Save to' action code in NameBearerHandle
            } catch (gate.creole.ResourceInstantiationException ex) {
                throw new GateRuntimeException(ex.getMessage());
            }
        }
        res.setDataStore(this);
        // let the world know
        fireResourceAdopted(new DatastoreEvent(this, DatastoreEvent.RESOURCE_ADOPTED, lr, null));
        return res;
    } else if (// adopted already here
    currentDS.equals(this))
        return lr;
    else {
        // someone else's child
        throw new PersistenceException("Can't adopt a resource which is already in a different datastore");
    }
}
Also used : FeatureMap(gate.FeatureMap) LanguageResource(gate.LanguageResource) DataStore(gate.DataStore) GateRuntimeException(gate.util.GateRuntimeException) DatastoreEvent(gate.event.DatastoreEvent) Corpus(gate.Corpus)

Example 5 with DataStore

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

the class CorpusBenchmarkTool method evaluateCorpus.

// generateCorpus
protected void evaluateCorpus(File fileDir, File processedDir, File markedDir, File errorDir) {
    // 1. check if we have input files and the processed Dir
    if (fileDir == null || !fileDir.exists())
        return;
    if (processedDir == null || !processedDir.exists())
        // if the user wants evaluation of marked and stored that's not possible
        if (isMarkedStored) {
            Out.prln("Cannot evaluate because no processed documents exist.");
            return;
        } else
            isMarkedClean = true;
    // create the error directory or clean it up if needed
    File errDir = null;
    if (isMoreInfoMode) {
        errDir = errorDir;
        if (errDir == null) {
            errDir = new File(currDir, ERROR_DIR_NAME);
        } else {
            // get rid of the directory, coz we wants it clean
            if (!Files.rmdir(errDir))
                Out.prln("cannot delete old error directory: " + errDir);
        }
        Out.prln("Create error directory: " + errDir + "<BR><BR>");
        errDir.mkdir();
    }
    // looked for marked texts only if the directory exists
    boolean processMarked = markedDir != null && markedDir.exists();
    if (!processMarked && (isMarkedStored || isMarkedClean)) {
        Out.prln("Cannot evaluate because no human-annotated documents exist.");
        return;
    }
    if (isMarkedStored) {
        evaluateMarkedStored(markedDir, processedDir, errDir);
        return;
    } else if (isMarkedClean) {
        evaluateMarkedClean(markedDir, fileDir, errDir);
        return;
    }
    Document persDoc = null;
    Document cleanDoc = null;
    Document markedDoc = null;
    // open the datastore and process each document
    try {
        // open the data store
        DataStore sds = Factory.openDataStore("gate.persist.SerialDataStore", processedDir.toURI().toURL().toExternalForm());
        List<String> lrIDs = sds.getLrIds("gate.corpora.DocumentImpl");
        for (int i = 0; i < lrIDs.size(); i++) {
            String docID = lrIDs.get(i);
            // read the stored document
            FeatureMap features = Factory.newFeatureMap();
            features.put(DataStore.DATASTORE_FEATURE_NAME, sds);
            features.put(DataStore.LR_ID_FEATURE_NAME, docID);
            FeatureMap hparams = Factory.newFeatureMap();
            // Gate.setHiddenAttribute(hparams, true);
            persDoc = (Document) Factory.createResource("gate.corpora.DocumentImpl", features, hparams);
            if (isMoreInfoMode) {
                StringBuffer errName = new StringBuffer(persDoc.getName());
                errName.replace(persDoc.getName().lastIndexOf("."), persDoc.getName().length(), ".err");
                Out.prln("<H2>" + "<a href=\"err/" + errName.toString() + "\">" + persDoc.getName() + "</a>" + "</H2>");
            } else
                Out.prln("<H2>" + persDoc.getName() + "</H2>");
            File cleanDocFile = new File(fileDir, persDoc.getName());
            // try reading the original document from clean
            if (!cleanDocFile.exists()) {
                Out.prln("Warning: Cannot find original document " + persDoc.getName() + " in " + fileDir);
            } else {
                FeatureMap params = Factory.newFeatureMap();
                params.put(Document.DOCUMENT_URL_PARAMETER_NAME, cleanDocFile.toURI().toURL());
                params.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, documentEncoding);
                // create the document
                cleanDoc = (Document) Factory.createResource("gate.corpora.DocumentImpl", params, hparams);
                cleanDoc.setName(persDoc.getName());
            }
            // try finding the marked document
            StringBuffer docName = new StringBuffer(persDoc.getName());
            if (!isMarkedDS) {
                docName.replace(persDoc.getName().lastIndexOf("."), docName.length(), ".xml");
                File markedDocFile = new File(markedDir, docName.toString());
                if (!processMarked || !markedDocFile.exists()) {
                    Out.prln("Warning: Cannot find human-annotated document " + markedDocFile + " in " + markedDir);
                } else {
                    FeatureMap params = Factory.newFeatureMap();
                    params.put(Document.DOCUMENT_URL_PARAMETER_NAME, markedDocFile.toURI().toURL());
                    params.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, documentEncoding);
                    // create the document
                    markedDoc = (Document) Factory.createResource("gate.corpora.DocumentImpl", params, hparams);
                    markedDoc.setName(persDoc.getName());
                }
            } else {
                // open marked from a DS
                // open the data store
                DataStore sds1 = Factory.openDataStore("gate.persist.SerialDataStore", markedDir.toURI().toURL().toExternalForm());
                List<String> lrIDs1 = sds1.getLrIds("gate.corpora.DocumentImpl");
                boolean found = false;
                int k = 0;
                // search for the marked doc with the same name
                while (k < lrIDs1.size() && !found) {
                    String docID1 = lrIDs1.get(k);
                    // read the stored document
                    FeatureMap features1 = Factory.newFeatureMap();
                    features1.put(DataStore.DATASTORE_FEATURE_NAME, sds1);
                    features1.put(DataStore.LR_ID_FEATURE_NAME, docID1);
                    Document tempDoc = (Document) Factory.createResource("gate.corpora.DocumentImpl", features1, hparams);
                    // check whether this is our doc
                    if (((String) tempDoc.getFeatures().get("gate.SourceURL")).endsWith(persDoc.getName())) {
                        found = true;
                        markedDoc = tempDoc;
                    } else
                        k++;
                }
            }
            evaluateDocuments(persDoc, cleanDoc, markedDoc, errDir);
            if (persDoc != null) {
                final gate.Document pd = persDoc;
                javax.swing.SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        Factory.deleteResource(pd);
                    }
                });
            }
            if (cleanDoc != null) {
                final gate.Document cd = cleanDoc;
                javax.swing.SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        Factory.deleteResource(cd);
                    }
                });
            }
            if (markedDoc != null) {
                final gate.Document md = markedDoc;
                javax.swing.SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        Factory.deleteResource(md);
                    }
                });
            }
        }
        // for loop through saved docs
        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 : Document(gate.Document) ResourceInstantiationException(gate.creole.ResourceInstantiationException) FeatureMap(gate.FeatureMap) SerialDataStore(gate.persist.SerialDataStore) DataStore(gate.DataStore) PersistenceException(gate.persist.PersistenceException) Document(gate.Document) File(java.io.File)

Aggregations

DataStore (gate.DataStore)14 PersistenceException (gate.persist.PersistenceException)9 File (java.io.File)6 URL (java.net.URL)6 Document (gate.Document)5 FeatureMap (gate.FeatureMap)5 ResourceInstantiationException (gate.creole.ResourceInstantiationException)5 GateRuntimeException (gate.util.GateRuntimeException)5 MalformedURLException (java.net.MalformedURLException)5 SerialDataStore (gate.persist.SerialDataStore)3 TestDocument (gate.corpora.TestDocument)2 GateException (gate.util.GateException)2 BenchmarkReportException (gate.util.reporting.exceptions.BenchmarkReportException)2 Point (java.awt.Point)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URISyntaxException (java.net.URISyntaxException)2 Corpus (gate.Corpus)1 DataStoreRegister (gate.DataStoreRegister)1 LanguageResource (gate.LanguageResource)1