krickert commented on code in PR #1056: URL: https://github.com/apache/opennlp/pull/1056#discussion_r3281206873
########## opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/stopword/StopwordLists.java: ########## @@ -0,0 +1,183 @@ +/* + * 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.stopword; + +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.nio.charset.Charset; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +import opennlp.tools.util.LanguageCodeValidator; + +/** + * Static factory for {@link StopwordFilter} instances backed by bundled + * language-specific stopword resources or caller-supplied input streams. + * <p> + * Bundled lists ship for the eleven languages enumerated in + * <a href="https://issues.apache.org/jira/browse/OPENNLP-660">OPENNLP-660</a>: + * Bulgarian (bg), Danish (da), German (de), English (en), Spanish (es), + * Finnish (fi), French (fr), Italian (it), Dutch (nl), Portuguese (pt), + * Russian (ru). Each list is keyed by its ISO 639-1 two-letter code. + */ +public final class StopwordLists { + + private static final String RESOURCE_PATH_PREFIX = "/opennlp/tools/stopword/"; + + private static final Set<String> SUPPORTED_LANGUAGES; + + /** + * ISO 639-2 bibliographic codes that {@link Locale#getISO3Language()} does + * not produce (the JDK returns only the terminologic forms), but which + * {@link LanguageCodeValidator} accepts. Mapped to their ISO 639-1 + * equivalents here so that callers can use either form. + */ + private static final Map<String, String> BIBLIOGRAPHIC_TO_ISO6391; + + static { + final Set<String> langs = new LinkedHashSet<>(); + Collections.addAll(langs, + "bg", "da", "de", "en", "es", "fi", "fr", "it", "nl", "pt", "ru"); + SUPPORTED_LANGUAGES = Collections.unmodifiableSet(langs); + + final Map<String, String> biblio = new HashMap<>(); + biblio.put("dut", "nl"); // Dutch + biblio.put("fre", "fr"); // French + biblio.put("ger", "de"); // German + BIBLIOGRAPHIC_TO_ISO6391 = Collections.unmodifiableMap(biblio); + } + + private StopwordLists() { + // utility class + } + + /** + * Returns a case-insensitive {@link StopwordFilter} for the given ISO 639 + * language code. Three-letter codes are normalized to their two-letter + * equivalent when a bundled list exists for the latter. + * + * @param iso639Code The ISO 639-1 or ISO 639-2/3 language code. + * Must not be {@code null}. + * @return A {@link StopwordFilter} backed by the bundled resource. + * @throws IllegalArgumentException if {@code iso639Code} is {@code null}, + * is not a valid ISO 639 code, or has no bundled list for this language. + * @throws UncheckedIOException if reading the bundled resource fails. + */ + public static StopwordFilter forLanguage(final String iso639Code) { Review Comment: forLanguage reloads and parses the classpath resource on every call. Since the bundled filters are immutable, please cache them per normalized language code so repeated use does not re-parse the same file. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
