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

com.davidthomasbernal.stardict.searchers.TriePrefixSearcher Maven / Gradle / Ivy

There is a newer version: 1.1
Show newest version
package com.davidthomasbernal.stardict.searchers;

import com.davidthomasbernal.stardict.Dictionary;
import com.davidthomasbernal.stardict.util.datastructures.TrieMap;

import java.util.List;
import java.util.Set;

/**
 * Created by david on 4/5/15.
 */
public class TriePrefixSearcher {
    protected final Dictionary dictionary;
    private final TrieMap trie = new TrieMap<>();
    private boolean trieInitialized = false;

    public TriePrefixSearcher(Dictionary dictionary) {
        this.dictionary = dictionary;
    }

    public void ensureTrie() {
        if (!trieInitialized) {
            Set words = this.dictionary.getWords();
            for (String word : words) {
                trie.put(word, null);
            }
            trieInitialized = true;
        }
    }

    public List search(String prefix) {
        ensureTrie();
        return trie.prefixSearch(prefix);
    }
}