|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package opennlp.tools.namefind; |
| 19 | + |
| 20 | +import opennlp.tools.commons.ThreadSafe; |
| 21 | +import opennlp.tools.util.Span; |
| 22 | + |
| 23 | +/** |
| 24 | + * A thread-safe version of {@link NameFinderME}. Using it is completely transparent. |
| 25 | + * You can use it in a single-threaded context as well, it only incurs a minimal overhead. |
| 26 | + * |
| 27 | + * @implNote |
| 28 | + * This implementation uses a {@link ThreadLocal}. Although the implementation is |
| 29 | + * lightweight because the model is not duplicated, if you have many long-running threads, |
| 30 | + * you may run into memory problems. |
| 31 | + * <p> |
| 32 | + * Be careful when using this in a Jakarta EE application, for example. |
| 33 | + * </p> |
| 34 | + * The user is responsible for clearing the {@link ThreadLocal}. |
| 35 | + * |
| 36 | + * @see NameFinderME |
| 37 | + * @see TokenNameFinder |
| 38 | + */ |
| 39 | +@ThreadSafe |
| 40 | +public class ThreadSafeNameFinderME implements TokenNameFinder, AutoCloseable { |
| 41 | + |
| 42 | + private final TokenNameFinderModel model; |
| 43 | + |
| 44 | + private final ThreadLocal<NameFinderME> threadLocal = new ThreadLocal<>(); |
| 45 | + |
| 46 | + /** |
| 47 | + * Initializes a {@link ThreadSafeNameFinderME} with the specified {@code model}. |
| 48 | + * |
| 49 | + * @param model A valid {@link TokenNameFinderModel}. |
| 50 | + */ |
| 51 | + public ThreadSafeNameFinderME(TokenNameFinderModel model) { |
| 52 | + super(); |
| 53 | + this.model = model; |
| 54 | + } |
| 55 | + |
| 56 | + // If a thread-local version exists, return it. Otherwise, create, then return. |
| 57 | + private NameFinderME getNameFinder() { |
| 58 | + NameFinderME sd = threadLocal.get(); |
| 59 | + if (sd == null) { |
| 60 | + sd = new NameFinderME(model); |
| 61 | + threadLocal.set(sd); |
| 62 | + } |
| 63 | + return sd; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void close() { |
| 68 | + threadLocal.remove(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Span[] find(String[] tokens) { |
| 73 | + return getNameFinder().find(tokens); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void clearAdaptiveData() { |
| 78 | + getNameFinder().clearAdaptiveData(); |
| 79 | + } |
| 80 | +} |
0 commit comments