org.carrot2.language.SingleLanguageComponentsProviderImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of carrot2-core Show documentation
Show all versions of carrot2-core Show documentation
Carrot2 Text Clustering Library
/*
* Carrot2 project.
*
* Copyright (C) 2002-2020, Dawid Weiss, Stanisław Osiński.
* All rights reserved.
*
* Refer to the full license file "carrot2.LICENSE"
* in the root folder of the repository checkout or at:
* https://www.carrot2.org/carrot2.LICENSE
*/
package org.carrot2.language;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import org.carrot2.util.ClassRelativeResourceLookup;
import org.carrot2.util.ResourceLookup;
public abstract class SingleLanguageComponentsProviderImpl implements LanguageComponentsProvider {
private final String language;
private final String providerName;
private final LinkedHashMap, SupplierLoader>> components = new LinkedHashMap<>();
protected interface SupplierLoader {
Supplier get(String language, ResourceLookup resourceLookup) throws IOException;
}
protected SingleLanguageComponentsProviderImpl(String providerName, String language) {
this.language = language;
this.providerName = providerName;
}
@Override
public String name() {
return providerName;
}
@Override
public final Set languages() {
return Collections.singleton(language);
}
@Override
public ResourceLookup defaultResourceLookup() {
return new ClassRelativeResourceLookup(getClass());
}
@Override
public Set> componentTypes() {
return components.keySet();
}
@Override
public Map, Supplier>> load(
String language, ResourceLookup resourceLookup, Set> componentTypes)
throws IOException {
LinkedHashMap, Supplier>> result = new LinkedHashMap<>();
for (Class> componentType : componentTypes) {
SupplierLoader> supplierLoader = components.get(componentType);
if (supplierLoader == null) {
throw new IllegalArgumentException("Not a registered component: " + componentType);
}
result.put(componentType, supplierLoader.get(language, resourceLookup));
}
return result;
}
protected final void register(Class clazz, SupplierLoader extends T> loader) {
if (components.containsKey(clazz)) {
throw new IllegalArgumentException("Component class already registered: " + clazz);
}
components.put(clazz, loader);
}
protected final void registerResourceless(Class clazz, Supplier supplier) {
register(clazz, (language, resourceLoader) -> supplier);
}
protected static LexicalData loadLexicalData(String language, ResourceLookup resourceLookup)
throws IOException {
String langPrefix = language.toLowerCase(Locale.ROOT);
return new LexicalDataImpl(
resourceLookup, langPrefix + ".stopwords.utf8", langPrefix + ".stoplabels.utf8");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy