Standalone API Usage
This section describes the basic structure of a standalone application using the API. See the source referenced in Example Programs. Typically, your application takes the following actions:
-
Load the EDK library.
CopySystem.loadLibrary("edkjni");
-
Create an
EDKFactory
instance of aTextExtractionFactory
, supplying a valid license key.Copytry ( TextExtractionFactory<EDKMatch> factory = EDKFactory.fromLicenseKey("...") ) {
// use try-with-resources to automatically cleanup factory once finished or if an exception occurs
// rest of the processing code goes here ...
} -
Use the factory to create an instance of a
TextExtractionEngine
.Copytry ( TextExtractionEngine<EDKMatch> engine = factory.createEngineFromConfigFile(Paths.get("path", "to", "config.cfg")) ) {
// use try-with-resources to automatically cleanup engine once finished or if an exception occurs
// rest of the processing code goes here ...
}You can specify the options, grammar files, and entities to use in the configuration file. Alternatively the SDK provides functions for setting options programmatically (see the
FromSettings.java
example). -
Decide whether to push data to the engine when it becomes available, or have the engine read from a stream when it needs more data. The data must be UTF-8 encoded. The following example demonstrates how to read from a stream. The stream is passed to an
EDKJNIInputStream
object which reads the stream when required.Copytry ( FileInputStream in = new FileInputStream("input.txt") ) {
ReadableInputStream stream = new EDKJNIInputStream(in);
// ...
}Pass the
EDKJNIInputStream
to aTextExtractionSession
object, which maintains the state of the matching process. If your application is multi-threaded then each thread should use its ownTextExtractionSession
.Copytry ( TextExtractionSession<EDKMatch> session = engine.createSession() ) {
session.setInputStream(stream);
// ...
} -
Begin matching.
Copyfor (EDKMatch match : session) {
System.out.println(match.getText());
}NOTE: If you create your Eduction engine from a configuration file that includes post-processing tasks, the post-processing tasks automatically run as part of
EDKMatch
and you do not need to run them separately. - Release resources when done.