diff --git a/opennlp-docs/src/docbkx/model-loading.xml b/opennlp-docs/src/docbkx/model-loading.xml new file mode 100644 index 000000000..266dbe2d4 --- /dev/null +++ b/opennlp-docs/src/docbkx/model-loading.xml @@ -0,0 +1,147 @@ + + + + + + Classpath Loading of OpenNLP Models + + Since version 2.4.0, OpenNLP supports the ability to load pre-trained OpenNLP models from the classpath. + It relies on either a simple implementation using the application's classpath or on the + classgraph + library to locate OpenNLP model JAR files. + Our pre-trained models are bundled from the OpenNLP Models + repository. + + This section describes + + + + how to load and use a pre-trained OpenNLP model from the classpath. + + + how to bundle a custom OpenNLP model to be loadable as a JAR file from the classpath. + + + + + +
+ Loading a pre-trained OpenNLP model from the classpath + + First, you need to add the following dependency to your classpath: + + + + org.apache.opennlp + opennlp-tools-models + CURRENT_OPENNLP_VERSION + +]]> + + + by using our pre-trained models or by building custom models as described later in this chapter. + If you need advanced classpath scanning capabilities, you should also add the classgraph library to your classpath. + + + + io.github.classgraph + classgraph + CURRENT_CLASSGRAPH_VERSION + +]]> + + + Make sure you replace the placeholders with the appropriate version values. + + Next, you can search for such a model and load it from the classpath: + + + models = finder.findModels(false); +for(ClassPathModelEntry entry : models) { + + final ClassPathModel model = loader.load(model); + + if(model != null) { + System.out.println(model.name()); + System.out.println(model.sha256()); + System.out.println(model.version()); + System.out.println(model.language()); + // do something with the model by consuming the byte array + } +}]]> + + + +
+ + +
+ Bundling a custom trained OpenNLP model for the classpath + + If you intend to provide your own custom trained OpenNLP models as JAR files for classpath discovery, + we recommend that you have a look at our setup in the OpenNLP Models + repository. We recommend to put one model per JAR file. + + Make sure you add a model.properties file with the following content + + + + + + Make sure to replace the values accordingly and configure your build tool to include the binary model and the model.properties + in the resulting JAR file. + + To load such a custom model, you may need to adjust the pattern for classpath scanning, i.e. if you name your model "custom-opennlp-model", + you would need the following code to successfully find and load it: + + + models = finder.findModels(false); +for(ClassPathModelEntry entry : models) { + + final ClassPathModel model = loader.load(model); + + if(model != null) { + System.out.println(model.name()); + System.out.println(model.sha256()); + System.out.println(model.version()); + System.out.println(model.language()); + // do something with the model by consuming the byte array + } +}]]> + + + +
+
\ No newline at end of file diff --git a/opennlp-docs/src/docbkx/opennlp.xml b/opennlp-docs/src/docbkx/opennlp.xml index 0d2115a28..1f62e8940 100644 --- a/opennlp-docs/src/docbkx/opennlp.xml +++ b/opennlp-docs/src/docbkx/opennlp.xml @@ -94,7 +94,8 @@ under the License. - + +