Package com.gengoai.hermes.lexicon
Class TrieWordList
- java.lang.Object
-
- com.gengoai.hermes.lexicon.TrieWordList
-
- All Implemented Interfaces:
PrefixSearchable
,WordList
,Serializable
,Iterable<String>
public class TrieWordList extends Object implements WordList, PrefixSearchable, Serializable
Implementation of aWordList
backed by a Trie- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description TrieWordList(@NonNull WordList... wordLists)
Instantiates a new TrieWordListTrieWordList(@NonNull Iterable<String> words)
Instantiates a new TrieWordList
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(String string)
Is the String contained in the WordListboolean
isPrefixMatch(HString hString)
Check if a prefix matches the givenHString
boolean
isPrefixMatch(String hString)
Check if a prefix matches the given StringIterator<String>
iterator()
Set<String>
prefixes(String string)
Gets the prefixes that match the given stringstatic TrieWordList
read(@NonNull Resource resource)
Reads the word list from the given resource where each term is on its own line and "#" represents comments.int
size()
Number of words in the listMap<String,Integer>
suggest(String string)
Suggests potential matches based on the given elements.Map<String,Integer>
suggest(String string, int maxCost)
Suggests potential matches based on the given elements.Map<String,Integer>
suggest(String string, int maxCost, int substitutionCost)
Suggests potential matches based on the given elements.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
TrieWordList
public TrieWordList(@NonNull @NonNull Iterable<String> words)
Instantiates a new TrieWordList- Parameters:
words
- the words
-
TrieWordList
public TrieWordList(@NonNull @NonNull WordList... wordLists)
Instantiates a new TrieWordList- Parameters:
wordLists
- the wordLists
-
-
Method Detail
-
read
public static TrieWordList read(@NonNull @NonNull Resource resource) throws IOException
Reads the word list from the given resource where each term is on its own line and "#" represents comments.
Note that convention states that if the first line of a word list is a comment stating "case-insensitive" then loading of that word list will result in all words being lower-cased.
- Parameters:
resource
- the resource- Returns:
- the trie word list
- Throws:
IOException
- the io exception
-
contains
public boolean contains(String string)
Description copied from interface:WordList
Is the String contained in the WordList
-
isPrefixMatch
public boolean isPrefixMatch(HString hString)
Description copied from interface:PrefixSearchable
Check if a prefix matches the givenHString
- Specified by:
isPrefixMatch
in interfacePrefixSearchable
- Parameters:
hString
- theHString
to check for a prefix match- Returns:
- True if a prefix matches, False otherwise
-
isPrefixMatch
public boolean isPrefixMatch(String hString)
Description copied from interface:PrefixSearchable
Check if a prefix matches the given String- Specified by:
isPrefixMatch
in interfacePrefixSearchable
- Parameters:
hString
- the String to check for a prefix match- Returns:
- True if a prefix matches, False otherwise
-
prefixes
public Set<String> prefixes(String string)
Description copied from interface:PrefixSearchable
Gets the prefixes that match the given string- Specified by:
prefixes
in interfacePrefixSearchable
- Parameters:
string
- the string- Returns:
- the set of matching prefixes
-
size
public int size()
Description copied from interface:WordList
Number of words in the list
-
suggest
public Map<String,Integer> suggest(String string)
Suggests potential matches based on the given elements.- Parameters:
string
- the string to generate suggestions for- Returns:
- the map of suggestions with their costs
-
suggest
public Map<String,Integer> suggest(String string, int maxCost)
Suggests potential matches based on the given elements.- Parameters:
string
- the string to generate suggestions formaxCost
- the maximum cost of the suggestions- Returns:
- the map of suggestions with their costs
-
suggest
public Map<String,Integer> suggest(String string, int maxCost, int substitutionCost)
Suggests potential matches based on the given elements.- Parameters:
string
- the string to generate suggestions formaxCost
- the maximum cost of the suggestionssubstitutionCost
- the cost for string substitutions- Returns:
- the map of suggestions with their costs
-
-