This is an automated email from the ASF dual-hosted git repository.
mawiesne pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opennlp.git
The following commit(s) were added to refs/heads/main by this push:
new 5310565f OPENNLP-1667: Add thread-safe version of ChunkerME (#708)
5310565f is described below
commit 5310565f6f402a93a41222050ab6f88f9f13d013
Author: Martin Wiesner <[email protected]>
AuthorDate: Fri Dec 13 08:42:16 2024 +0100
OPENNLP-1667: Add thread-safe version of ChunkerME (#708)
---
.../ThreadSafeChunkerME.java} | 57 +++++++++++++---------
.../tools/lemmatizer/ThreadSafeLemmatizerME.java | 10 ++--
.../tools/namefind/ThreadSafeNameFinderME.java | 20 ++++----
3 files changed, 49 insertions(+), 38 deletions(-)
diff --git
a/opennlp-tools/src/main/java/opennlp/tools/namefind/ThreadSafeNameFinderME.java
b/opennlp-tools/src/main/java/opennlp/tools/chunker/ThreadSafeChunkerME.java
similarity index 55%
copy from
opennlp-tools/src/main/java/opennlp/tools/namefind/ThreadSafeNameFinderME.java
copy to
opennlp-tools/src/main/java/opennlp/tools/chunker/ThreadSafeChunkerME.java
index fec411cd..5d92ba21 100644
---
a/opennlp-tools/src/main/java/opennlp/tools/namefind/ThreadSafeNameFinderME.java
+++ b/opennlp-tools/src/main/java/opennlp/tools/chunker/ThreadSafeChunkerME.java
@@ -15,13 +15,14 @@
* limitations under the License.
*/
-package opennlp.tools.namefind;
+package opennlp.tools.chunker;
import opennlp.tools.commons.ThreadSafe;
+import opennlp.tools.util.Sequence;
import opennlp.tools.util.Span;
/**
- * A thread-safe version of {@link NameFinderME}. Using it is completely
transparent.
+ * A thread-safe version of the {@link ChunkerME}. Using it is completely
transparent.
* You can use it in a single-threaded context as well, it only incurs a
minimal overhead.
*
* @implNote
@@ -33,48 +34,58 @@ import opennlp.tools.util.Span;
* </p>
* The user is responsible for clearing the {@link ThreadLocal}.
*
- * @see NameFinderME
- * @see TokenNameFinder
+ * @see Chunker
+ * @see ChunkerME
*/
@ThreadSafe
-public class ThreadSafeNameFinderME implements TokenNameFinder, AutoCloseable {
+public class ThreadSafeChunkerME implements Chunker, AutoCloseable {
- private final TokenNameFinderModel model;
+ private final ChunkerModel model;
- private final ThreadLocal<NameFinderME> threadLocal = new ThreadLocal<>();
+ private final ThreadLocal<ChunkerME> threadLocal = new ThreadLocal<>();
/**
- * Initializes a {@link ThreadSafeNameFinderME} with the specified {@code
model}.
+ * Initializes a {@link ThreadSafeChunkerME} with the specified {@code
model}.
*
- * @param model A valid {@link TokenNameFinderModel}.
+ * @param model A valid {@link ChunkerModel}.
*/
- public ThreadSafeNameFinderME(TokenNameFinderModel model) {
+ public ThreadSafeChunkerME(ChunkerModel model) {
super();
this.model = model;
}
- // If a thread-local version exists, return it. Otherwise, create, then
return.
- private NameFinderME getNameFinder() {
- NameFinderME sd = threadLocal.get();
- if (sd == null) {
- sd = new NameFinderME(model);
- threadLocal.set(sd);
+ private ChunkerME getChunker() {
+ ChunkerME c = threadLocal.get();
+ if (c == null) {
+ c = new ChunkerME(model);
+ threadLocal.set(c);
}
- return sd;
+ return c;
}
@Override
- public void close() {
- threadLocal.remove();
+ public String[] chunk(String[] toks, String[] tags) {
+ return getChunker().chunk(toks, tags);
+ }
+
+ @Override
+ public Span[] chunkAsSpans(String[] toks, String[] tags) {
+ return getChunker().chunkAsSpans(toks, tags);
}
@Override
- public Span[] find(String[] tokens) {
- return getNameFinder().find(tokens);
+ public Sequence[] topKSequences(String[] sentence, String[] tags) {
+ return getChunker().topKSequences(sentence, tags);
}
@Override
- public void clearAdaptiveData() {
- getNameFinder().clearAdaptiveData();
+ public Sequence[] topKSequences(String[] sentence, String[] tags, double
minSequenceScore) {
+ return getChunker().topKSequences(sentence, tags, minSequenceScore);
}
+
+ @Override
+ public void close() {
+ threadLocal.remove();
+ }
+
}
diff --git
a/opennlp-tools/src/main/java/opennlp/tools/lemmatizer/ThreadSafeLemmatizerME.java
b/opennlp-tools/src/main/java/opennlp/tools/lemmatizer/ThreadSafeLemmatizerME.java
index ba84c83b..fc3aba08 100644
---
a/opennlp-tools/src/main/java/opennlp/tools/lemmatizer/ThreadSafeLemmatizerME.java
+++
b/opennlp-tools/src/main/java/opennlp/tools/lemmatizer/ThreadSafeLemmatizerME.java
@@ -55,12 +55,12 @@ public class ThreadSafeLemmatizerME implements Lemmatizer,
AutoCloseable {
}
private LemmatizerME getLemmatizer() {
- LemmatizerME tagger = threadLocal.get();
- if (tagger == null) {
- tagger = new LemmatizerME(model);
- threadLocal.set(tagger);
+ LemmatizerME l = threadLocal.get();
+ if (l == null) {
+ l = new LemmatizerME(model);
+ threadLocal.set(l);
}
- return tagger;
+ return l;
}
@Override
diff --git
a/opennlp-tools/src/main/java/opennlp/tools/namefind/ThreadSafeNameFinderME.java
b/opennlp-tools/src/main/java/opennlp/tools/namefind/ThreadSafeNameFinderME.java
index fec411cd..e820605c 100644
---
a/opennlp-tools/src/main/java/opennlp/tools/namefind/ThreadSafeNameFinderME.java
+++
b/opennlp-tools/src/main/java/opennlp/tools/namefind/ThreadSafeNameFinderME.java
@@ -55,17 +55,12 @@ public class ThreadSafeNameFinderME implements
TokenNameFinder, AutoCloseable {
// If a thread-local version exists, return it. Otherwise, create, then
return.
private NameFinderME getNameFinder() {
- NameFinderME sd = threadLocal.get();
- if (sd == null) {
- sd = new NameFinderME(model);
- threadLocal.set(sd);
+ NameFinderME nf = threadLocal.get();
+ if (nf == null) {
+ nf = new NameFinderME(model);
+ threadLocal.set(nf);
}
- return sd;
- }
-
- @Override
- public void close() {
- threadLocal.remove();
+ return nf;
}
@Override
@@ -77,4 +72,9 @@ public class ThreadSafeNameFinderME implements
TokenNameFinder, AutoCloseable {
public void clearAdaptiveData() {
getNameFinder().clearAdaptiveData();
}
+
+ @Override
+ public void close() {
+ threadLocal.remove();
+ }
}