You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@opennlp.apache.org by "mawiesne (via GitHub)" <gi...@apache.org> on 2025/04/29 12:25:33 UTC
[PR] OPENNLP-1729: Provide easier loading of Models for given model lang and type (opennlp)
mawiesne opened a new pull request, #775:
URL: https://github.com/apache/opennlp/pull/775
Change
-
- extracts `ModelType` from `DownloadUtil`
- adds new methods to `ClassPathModelLoader` to obtain actual model instances easily
- adds `ClassPathModelProvider` interface
- adds `DefaultClassPathModelProvider` which combines existing classes to achieve easier access to model objects via classpath loading
- adds JUnit tests for the new classes
- adds and improves JavaDoc
Tasks
-
Thank you for contributing to Apache OpenNLP.
In order to streamline the review of the contribution we ask you
to ensure the following steps have been taken:
### For all changes:
- [x] Is there a JIRA ticket associated with this PR? Is it referenced
in the commit message?
- [x] Does your PR title start with OPENNLP-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
- [x] Has your PR been rebased against the latest commit within the target branch (typically main)?
- [x] Is your initial contribution a single, squashed commit?
### For code changes:
- [x] Have you ensured that the full suite of tests is executed via mvn clean install at the root opennlp folder?
- [x] Have you written or updated unit tests to verify your changes?
- [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)?
- [ ] If applicable, have you updated the LICENSE file, including the main LICENSE file in opennlp folder?
- [ ] If applicable, have you updated the NOTICE file, including the main NOTICE file found in opennlp folder?
### For documentation related changes:
- [ ] Have you ensured that format looks appropriate for the output in which it is rendered?
### Note:
Please ensure that once the PR is submitted, you check GitHub Actions for build issues and submit an update to your PR as soon as possible.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] OPENNLP-1729: Provide easier loading of Models for given model lang and type (opennlp)
Posted by "rzo1 (via GitHub)" <gi...@apache.org>.
rzo1 commented on code in PR #775:
URL: https://github.com/apache/opennlp/pull/775#discussion_r2066593502
##########
opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelLoader.java:
##########
@@ -59,4 +66,75 @@ public ClassPathModel load(ClassPathModelEntry entry) throws IOException {
return new ClassPathModel(properties, model);
}
+
+ /**
+ * Restores a {@link T model} among a set {@link ClassPathModelEntry classpath entries}
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param classPathEntries A non-empty set of {@link ClassPathModelEntry candidates} to find a matching
+ * model in. Must not be {@code null}. If it is empty, the result will
+ * be {@code null}.
+ * @param lang The language code of the requested model. If {@code null}, the result will
+ * be {@code null} as well.
+ * @param type The {@link ModelType} to select the model variant.
+ * @param modelType The class of model type parameter {@link T} to create an instance of.
+ *
+ * @return An model instance of type {@link T}, or {@code null} if no match was found
+ * for the specified parameters.
+ *
+ * @throws IllegalArgumentException Thrown if parameters were invalid.
+ * @throws ClassPathLoaderException Thrown if {@link T} could not be instantiated correctly.
+ * @throws IOException Thrown if something went wrong during reading resources from the classpath.
+ */
+ <T extends BaseModel> T load(Set<ClassPathModelEntry> classPathEntries, String lang,
+ ModelType type, Class<T> modelType) throws IOException {
+ return load(classPathEntries, lang, type.getName(), modelType);
Review Comment:
Possible NPE. `type` needs a check because `getName()` would be invoked on NULL.
##########
opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelLoader.java:
##########
@@ -59,4 +66,75 @@ public ClassPathModel load(ClassPathModelEntry entry) throws IOException {
return new ClassPathModel(properties, model);
}
+
+ /**
+ * Restores a {@link T model} among a set {@link ClassPathModelEntry classpath entries}
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param classPathEntries A non-empty set of {@link ClassPathModelEntry candidates} to find a matching
+ * model in. Must not be {@code null}. If it is empty, the result will
+ * be {@code null}.
+ * @param lang The language code of the requested model. If {@code null}, the result will
+ * be {@code null} as well.
+ * @param type The {@link ModelType} to select the model variant.
+ * @param modelType The class of model type parameter {@link T} to create an instance of.
+ *
+ * @return An model instance of type {@link T}, or {@code null} if no match was found
+ * for the specified parameters.
+ *
+ * @throws IllegalArgumentException Thrown if parameters were invalid.
+ * @throws ClassPathLoaderException Thrown if {@link T} could not be instantiated correctly.
+ * @throws IOException Thrown if something went wrong during reading resources from the classpath.
+ */
+ <T extends BaseModel> T load(Set<ClassPathModelEntry> classPathEntries, String lang,
+ ModelType type, Class<T> modelType) throws IOException {
+ return load(classPathEntries, lang, type.getName(), modelType);
+ }
+
+ /**
+ * Restores a {@link T model} among a set {@link ClassPathModelEntry classpath entries}
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param classPathEntries A non-empty set of {@link ClassPathModelEntry candidates} to find a matching
+ * model in. Must not be {@code null}. If it is empty, the result will
+ * be {@code null}.
+ * @param lang The language code of the requested model. If {@code null}, the result will
+ * be {@code null} as well.
+ * @param type The type string to narrow down the model variant. Note: Custom naming patterns
+ * can be applied here, as the {@code type} fragment will be used for a 'contains'
+ * check internally.
+ * @param modelType The class of model type parameter {@link T} to create an instance of.
+ *
+ * @return An model instance of type {@link T}, or {@code null} if no match was found
+ * for the specified parameters.
+ *
+ * @throws IllegalArgumentException Thrown if parameters were invalid.
+ * @throws ClassPathLoaderException Thrown if {@link T} could not be instantiated correctly.
+ * @throws IOException Thrown if something went wrong during reading resources from the classpath.
+ */
+ <T extends BaseModel> T load(Set<ClassPathModelEntry> classPathEntries, String lang,
+ String type, Class<T> modelType) throws IOException {
+ if (classPathEntries == null) {
+ throw new IllegalArgumentException("The provided ClassPath entries must not be null!");
+ }
+ T result = null;
+ if (classPathEntries.isEmpty()) {
+ return result;
+ }
+ try {
+ for (ClassPathModelEntry entry : classPathEntries) {
+ final ClassPathModel cpm = load(entry);
+ if (cpm != null && cpm.getModelLanguage().equals(lang) && cpm.getModelName().contains(type)) {
+ try (InputStream is = new BufferedInputStream(new ByteArrayInputStream(cpm.model()))) {
+ result = modelType.getConstructor(InputStream.class).newInstance(is);
Review Comment:
`modelType` needs a check for != null (also in Javadoc)
##########
opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.models;
+
+import java.io.IOException;
+
+import opennlp.tools.util.model.BaseModel;
+
+/**
+ * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from
+ * an application's classpath.
+ * <p>
+ * Providing models available in the classpath requires a {@link ClassPathModelFinder finder}
+ * and a {@link ClassPathModelLoader} at runtime. Depending on the environment,
+ * a custom {@code finder} can be used.
+ *
+ * @see BaseModel
+ * @see ClassPathModelFinder
+ * @see ClassPathModelLoader
+ */
+public interface ClassPathModelProvider {
+
+ /**
+ * Restores a {@link T model} among all classpath models at runtime
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param lang The ISO language code of the requested model. If {@code null}, the result will
+ * be {@code null} as well.
+ * @param type The type string to narrow down the model variant. Note: Custom naming patterns
+ * can be applied here, as the {@code type} fragment will be used for a 'contains'
+ * check internally.
+ * @param modelType The class of model type parameter {@link T} to create an instance of.
Review Comment:
`It must not be {@code null}.`
##########
opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.models;
+
+import java.io.IOException;
+
+import opennlp.tools.util.model.BaseModel;
+
+/**
+ * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from
+ * an application's classpath.
+ * <p>
+ * Providing models available in the classpath requires a {@link ClassPathModelFinder finder}
+ * and a {@link ClassPathModelLoader} at runtime. Depending on the environment,
+ * a custom {@code finder} can be used.
+ *
+ * @see BaseModel
+ * @see ClassPathModelFinder
+ * @see ClassPathModelLoader
+ */
+public interface ClassPathModelProvider {
+
+ /**
+ * Restores a {@link T model} among all classpath models at runtime
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param lang The ISO language code of the requested model. If {@code null}, the result will
+ * be {@code null} as well.
+ * @param type The type string to narrow down the model variant. Note: Custom naming patterns
+ * can be applied here, as the {@code type} fragment will be used for a 'contains'
+ * check internally.
+ * @param modelType The class of model type parameter {@link T} to create an instance of.
+ *
+ * @return An model instance of type {@link T}, or {@code null} if no match was found
+ * for the specified parameters.
+ * @throws IllegalArgumentException Thrown if parameters were invalid.
+ * @throws IOException Thrown if something went wrong during reading resources from the classpath.
+ */
+ <T extends BaseModel> T load(String lang, ModelType type, Class<T> modelType)
+ throws IOException;
+
+ /**
+ * Restores a {@link T model} among all classpath models at runtime
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param lang The ISO language code of the requested model. If {@code null}, the result will
Review Comment:
What happens if emptry string?
##########
opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.models;
+
+import java.io.IOException;
+
+import opennlp.tools.util.model.BaseModel;
+
+/**
+ * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from
+ * an application's classpath.
+ * <p>
+ * Providing models available in the classpath requires a {@link ClassPathModelFinder finder}
+ * and a {@link ClassPathModelLoader} at runtime. Depending on the environment,
+ * a custom {@code finder} can be used.
+ *
+ * @see BaseModel
+ * @see ClassPathModelFinder
+ * @see ClassPathModelLoader
+ */
+public interface ClassPathModelProvider {
+
+ /**
+ * Restores a {@link T model} among all classpath models at runtime
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param lang The ISO language code of the requested model. If {@code null}, the result will
Review Comment:
What happens if empty string?
##########
opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.models;
+
+import java.io.IOException;
+
+import opennlp.tools.util.model.BaseModel;
+
+/**
+ * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from
+ * an application's classpath.
+ * <p>
+ * Providing models available in the classpath requires a {@link ClassPathModelFinder finder}
+ * and a {@link ClassPathModelLoader} at runtime. Depending on the environment,
+ * a custom {@code finder} can be used.
+ *
+ * @see BaseModel
+ * @see ClassPathModelFinder
+ * @see ClassPathModelLoader
+ */
+public interface ClassPathModelProvider {
+
+ /**
+ * Restores a {@link T model} among all classpath models at runtime
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param lang The ISO language code of the requested model. If {@code null}, the result will
+ * be {@code null} as well.
+ * @param type The type string to narrow down the model variant. Note: Custom naming patterns
+ * can be applied here, as the {@code type} fragment will be used for a 'contains'
+ * check internally.
+ * @param modelType The class of model type parameter {@link T} to create an instance of.
+ *
+ * @return An model instance of type {@link T}, or {@code null} if no match was found
+ * for the specified parameters.
+ * @throws IllegalArgumentException Thrown if parameters were invalid.
+ * @throws IOException Thrown if something went wrong during reading resources from the classpath.
+ */
+ <T extends BaseModel> T load(String lang, ModelType type, Class<T> modelType)
+ throws IOException;
+
+ /**
+ * Restores a {@link T model} among all classpath models at runtime
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param lang The ISO language code of the requested model. If {@code null}, the result will
+ * be {@code null} as well.
+ * @param type The type string to narrow down the model variant. Note: Custom naming patterns
+ * can be applied here, as the {@code type} fragment will be used for a 'contains'
+ * check internally.
+ * @param modelType The class of model type parameter {@link T} to create an instance of.
Review Comment:
`It must not be {@code null}.`
##########
opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.models;
+
+import java.io.IOException;
+
+import opennlp.tools.util.model.BaseModel;
+
+/**
+ * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from
+ * an application's classpath.
+ * <p>
+ * Providing models available in the classpath requires a {@link ClassPathModelFinder finder}
+ * and a {@link ClassPathModelLoader} at runtime. Depending on the environment,
+ * a custom {@code finder} can be used.
+ *
+ * @see BaseModel
+ * @see ClassPathModelFinder
+ * @see ClassPathModelLoader
+ */
+public interface ClassPathModelProvider {
+
+ /**
+ * Restores a {@link T model} among all classpath models at runtime
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param lang The ISO language code of the requested model. If {@code null}, the result will
+ * be {@code null} as well.
+ * @param type The type string to narrow down the model variant. Note: Custom naming patterns
+ * can be applied here, as the {@code type} fragment will be used for a 'contains'
+ * check internally.
+ * @param modelType The class of model type parameter {@link T} to create an instance of.
+ *
+ * @return An model instance of type {@link T}, or {@code null} if no match was found
+ * for the specified parameters.
+ * @throws IllegalArgumentException Thrown if parameters were invalid.
+ * @throws IOException Thrown if something went wrong during reading resources from the classpath.
+ */
+ <T extends BaseModel> T load(String lang, ModelType type, Class<T> modelType)
+ throws IOException;
+
+ /**
+ * Restores a {@link T model} among all classpath models at runtime
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param lang The ISO language code of the requested model. If {@code null}, the result will
+ * be {@code null} as well.
+ * @param type The type string to narrow down the model variant. Note: Custom naming patterns
Review Comment:
`type` is not a string here. Most likely a ref towards `ModelType` ?
##########
opennlp-tools-models/src/main/java/opennlp/tools/models/ClassPathModelProvider.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package opennlp.tools.models;
+
+import java.io.IOException;
+
+import opennlp.tools.util.model.BaseModel;
+
+/**
+ * A provider for obtaining pre-trained OpenNLP {@link BaseModel models} from
+ * an application's classpath.
+ * <p>
+ * Providing models available in the classpath requires a {@link ClassPathModelFinder finder}
+ * and a {@link ClassPathModelLoader} at runtime. Depending on the environment,
+ * a custom {@code finder} can be used.
+ *
+ * @see BaseModel
+ * @see ClassPathModelFinder
+ * @see ClassPathModelLoader
+ */
+public interface ClassPathModelProvider {
+
+ /**
+ * Restores a {@link T model} among all classpath models at runtime
+ * according to the specified parameters {@code lang} and {@code type}.
+ *
+ * @param lang The ISO language code of the requested model. If {@code null}, the result will
+ * be {@code null} as well.
+ * @param type The type string to narrow down the model variant. Note: Custom naming patterns
Review Comment:
type is not a string here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] OPENNLP-1729: Provide easier loading of Models for given model lang and type (opennlp)
Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on PR #775:
URL: https://github.com/apache/opennlp/pull/775#issuecomment-2841029723
@rzo1 Thx for the review and related feedback. I addressed all of it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] OPENNLP-1729: Provide easier loading of Models for given model lang and type (opennlp)
Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne merged PR #775:
URL: https://github.com/apache/opennlp/pull/775
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org