All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.eb4j.pdic.DictionaryData Maven / Gradle / Ivy

There is a newer version: 0.3.3
Show newest version
/*
 * DSL4J, a parser library for LingoDSL format.
 * Copyright (C) 2021 Hiroshi Miura.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package io.github.eb4j.pdic;

import org.trie4j.MapTrie;
import org.trie4j.doublearray.MapDoubleArray;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;

/**
 * A class that encapsulates the storage and retrieval of string-keyed data.
 * Usage:
 * 
    *
  1. Retrieve data with {@link #lookUp(String)} *
* * @author Aaron Madlon-Kay * * @param * The type of data stored */ public final class DictionaryData { private final MapDoubleArray data; /** * POJO class to hold dictionary data. * @param trie source Trie object. */ public DictionaryData(final MapTrie trie) { data = new MapDoubleArray<>(trie); } /** * Look up the given word. * * @param word * The word to look up * @return A list of stored objects matching the given word */ public List> lookUp(final String word) throws IllegalStateException { return doLookUpWithLowerCase(word, false); } /** * Look up the given word using predictive completion; e.g. "term" will * match "terminology" (and "terminal", etc.). * * @param word * The word to look up * @return A list of stored objects matching the given word */ public List> lookUpPredictive(final String word) throws IllegalStateException { return doLookUpWithLowerCase(word, true); } private List> doLookUpWithLowerCase(final String word, final boolean predictive) { List> result = doLookUp(word, predictive); if (result.isEmpty()) { String lowerWord = word.toLowerCase(); result = doLookUp(lowerWord, predictive); } return result; } private List> doLookUp(final String word, final boolean predictive) throws IllegalStateException { if (data == null) { throw new IllegalStateException( "Object has not been finalized! You must call done() before doing any lookups."); } List> result = new ArrayList<>(); if (predictive) { data.predictiveSearch(word).forEach(w -> get(w, data.get(w), result)); } else { get(word, data.get(word), result); } return result; } /** * Unpack the given stored object (singular, or array) into the given * collection. * * @param key * @param value * @param into */ @SuppressWarnings("unchecked") private void get(final U key, final Object value, final Collection> into) { if (value == null) { return; } if (value instanceof Object[]) { for (Object o : (Object[]) value) { into.add(new AbstractMap.SimpleImmutableEntry<>(key, (T) o)); } } else { into.add(new AbstractMap.SimpleImmutableEntry<>(key, (T) value)); } } /** * Get the number of stored keys. * * @return The number of stored keys */ public int size() { return data.size(); } }