Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENNLP-1667: Add thread-safe version of ChunkerME #708

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.chunker;

import opennlp.tools.commons.ThreadSafe;
import opennlp.tools.util.Sequence;
import opennlp.tools.util.Span;

/**
* 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
* This implementation uses a {@link ThreadLocal}. Although the implementation is
* lightweight because the model is not duplicated, if you have many long-running threads,
* you may run into memory problems.
* <p>
* Be careful when using this in a Jakarta EE application, for example.
* </p>
* The user is responsible for clearing the {@link ThreadLocal}.
*
* @see Chunker
* @see ChunkerME
*/
@ThreadSafe
public class ThreadSafeChunkerME implements Chunker, AutoCloseable {

private final ChunkerModel model;

private final ThreadLocal<ChunkerME> threadLocal = new ThreadLocal<>();

/**
* Initializes a {@link ThreadSafeChunkerME} with the specified {@code model}.
*
* @param model A valid {@link ChunkerModel}.
*/
public ThreadSafeChunkerME(ChunkerModel model) {
super();
this.model = model;
}

private ChunkerME getChunker() {
ChunkerME c = threadLocal.get();
if (c == null) {
c = new ChunkerME(model);
threadLocal.set(c);
}
return c;
}

@Override
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 Sequence[] topKSequences(String[] sentence, String[] tags) {
return getChunker().topKSequences(sentence, tags);
}

@Override
public Sequence[] topKSequences(String[] sentence, String[] tags, double minSequenceScore) {
return getChunker().topKSequences(sentence, tags, minSequenceScore);
}

@Override
public void close() {
threadLocal.remove();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public ThreadSafeLemmatizerME(LemmatizerModel model) {
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,12 @@ public ThreadSafeNameFinderME(TokenNameFinderModel 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);
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
Expand All @@ -77,4 +72,9 @@ public Span[] find(String[] tokens) {
public void clearAdaptiveData() {
getNameFinder().clearAdaptiveData();
}

@Override
public void close() {
threadLocal.remove();
}
}
Loading