org.apache.lucene.analysis.hunspell.Dictionary Java Examples

The following examples show how to use org.apache.lucene.analysis.hunspell.Dictionary. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HunspellService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Inject
public HunspellService(final Settings settings, final Environment env, final Map<String, Dictionary> knownDictionaries) throws IOException {
    super(settings);
    this.knownDictionaries = knownDictionaries;
    this.hunspellDir = resolveHunspellDirectory(settings, env);
    this.defaultIgnoreCase = settings.getAsBoolean(HUNSPELL_IGNORE_CASE, false);
    dictionaries = CacheBuilder.newBuilder().build(new CacheLoader<String, Dictionary>() {
        @Override
        public Dictionary load(String locale) throws Exception {
            Dictionary dictionary = knownDictionaries.get(locale);
            if (dictionary == null) {
                dictionary = loadDictionary(locale, settings, env);
            }
            return dictionary;
        }
    });
    if (!settings.getAsBoolean(HUNSPELL_LAZY_LOAD, false)) {
        scanAndLoadDictionaries();
    }
}
 
Example #2
Source File: HunspellService.java    From crate with Apache License 2.0 6 votes vote down vote up
public HunspellService(final Settings settings, final Environment env, final Map<String, Dictionary> knownDictionaries)
        throws IOException {
    this.knownDictionaries = Collections.unmodifiableMap(knownDictionaries);
    this.hunspellDir = resolveHunspellDirectory(env);
    this.defaultIgnoreCase = HUNSPELL_IGNORE_CASE.get(settings);
    this.loadingFunction = (locale) -> {
        try {
            return loadDictionary(locale, settings, env);
        } catch (Exception e) {
            throw new IllegalStateException("failed to load hunspell dictionary for locale: " + locale, e);
        }
    };
    if (!HUNSPELL_LAZY_LOAD.get(settings)) {
        scanAndLoadDictionaries();
    }

}
 
Example #3
Source File: HunspellService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the hunspell dictionary for the given locale.
 *
 * @param locale The name of the locale
 */
public Dictionary getDictionary(String locale) {
    Dictionary dictionary = knownDictionaries.get(locale);
    if (dictionary == null) {
        dictionary = dictionaries.computeIfAbsent(locale, loadingFunction);
    }
    return dictionary;
}
 
Example #4
Source File: IndicesModule.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public void registerHunspellDictionary(String name, Dictionary dictionary) {
    hunspellDictionaries.registerExtension(name, dictionary);
}
 
Example #5
Source File: HunspellService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public HunspellService(final Settings settings, final Environment env) throws IOException {
    this(settings, env, Collections.<String, Dictionary>emptyMap());
}
 
Example #6
Source File: HunspellService.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Loads the hunspell dictionary for the given local.
 *
 * @param locale       The locale of the hunspell dictionary to be loaded.
 * @param nodeSettings The node level settings
 * @param env          The node environment (from which the conf path will be resolved)
 * @return The loaded Hunspell dictionary
 * @throws Exception when loading fails (due to IO errors or malformed dictionary files)
 */
private Dictionary loadDictionary(String locale, Settings nodeSettings, Environment env) throws Exception {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Loading hunspell dictionary [{}]...", locale);
    }
    Path dicDir = hunspellDir.resolve(locale);
    if (FileSystemUtils.isAccessibleDirectory(dicDir, LOGGER) == false) {
        throw new ElasticsearchException(String.format(Locale.ROOT, "Could not find hunspell dictionary [%s]", locale));
    }

    // merging node settings with hunspell dictionary specific settings
    Settings dictSettings = HUNSPELL_DICTIONARY_OPTIONS.get(nodeSettings);
    nodeSettings = loadDictionarySettings(dicDir, dictSettings.getByPrefix(locale + "."));

    boolean ignoreCase = nodeSettings.getAsBoolean("ignore_case", defaultIgnoreCase);

    Path[] affixFiles = FileSystemUtils.files(dicDir, "*.aff");
    if (affixFiles.length == 0) {
        throw new ElasticsearchException(String.format(Locale.ROOT, "Missing affix file for hunspell dictionary [%s]", locale));
    }
    if (affixFiles.length != 1) {
        throw new ElasticsearchException(String.format(Locale.ROOT, "Too many affix files exist for hunspell dictionary [%s]", locale));
    }
    InputStream affixStream = null;

    Path[] dicFiles = FileSystemUtils.files(dicDir, "*.dic");
    List<InputStream> dicStreams = new ArrayList<>(dicFiles.length);
    try {

        for (int i = 0; i < dicFiles.length; i++) {
            dicStreams.add(Files.newInputStream(dicFiles[i]));
        }

        affixStream = Files.newInputStream(affixFiles[0]);

        try (Directory tmp = new SimpleFSDirectory(env.tmpFile())) {
            return new Dictionary(tmp, "hunspell", affixStream, dicStreams, ignoreCase);
        }

    } catch (Exception e) {
        LOGGER.error(() -> new ParameterizedMessage("Could not load hunspell dictionary [{}]", locale), e);
        throw e;
    } finally {
        IOUtils.close(affixStream);
        IOUtils.close(dicStreams);
    }
}
 
Example #7
Source File: HunspellService.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the hunspell dictionary for the given locale.
 *
 * @param locale The name of the locale
 */
public Dictionary getDictionary(String locale) {
    return dictionaries.getUnchecked(locale);
}