Package org.passay.dictionary
Class TernaryTreeDictionary
java.lang.Object
org.passay.dictionary.TernaryTreeDictionary
- All Implemented Interfaces:
Dictionary
Provides fast searching for dictionary words using a ternary tree. The entire dictionary is stored in memory, so heap
size may need to be adjusted to accommodate large dictionaries. It is highly recommended that sorted word lists be
inserted using their median. This helps to produce a balanced ternary tree which improves search time. This class
inherits the lower case property of the supplied word list.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates a dictionary that uses the supplied ternary tree for dictionary searches.TernaryTreeDictionary(WordList wordList) Creates a new balanced tree dictionary from the suppliedWordList.TernaryTreeDictionary(WordList wordList, boolean useMedian) Creates a new dictionary instance from the givenWordList. -
Method Summary
Modifier and TypeMethodDescriptionReturns the underlying ternary tree used by this dictionary.static voidProvides command line access to a ternary tree dictionary.nearSearch(CharSequence word, int distance) Returns an array of strings which are near to the supplied word by the supplied distance.partialSearch(CharSequence word) Returns an array of strings which partially match the supplied word.booleansearch(CharSequence word) Returns whether the supplied word exists in the dictionary.longsize()Returns the number of words in this dictionary
-
Field Details
-
tree
Ternary tree used for searching.
-
-
Constructor Details
-
TernaryTreeDictionary
Creates a new balanced tree dictionary from the suppliedWordList. This constructor creates a balanced tree by inserting from the median of the word list, which may require additional work depending on theWordListimplementation.NOTE While using an unsorted word list produces correct results, it may dramatically reduce search efficiency. Using a sorted word list is recommended.
- Parameters:
wordList- list of words used to back the dictionary. This list is used exclusively to initialize the internalTernaryTreeused by the dictionary, and may be safely discarded after dictionary creation.
-
TernaryTreeDictionary
Creates a new dictionary instance from the givenWordList.- Parameters:
wordList- list of words used to back the dictionary. This list is used exclusively to initialize the internalTernaryTreeused by the dictionary, and may be safely discarded after dictionary creation.NOTE While using an unsorted word list produces correct results, it may dramatically reduce search efficiency. Using a sorted word list is recommended.
useMedian- set to true to force creation of a balanced tree by inserting into the tree from the median of theWordListoutward. Depending on the word list implementation, this may require additional work to access the median element on each insert.
-
TernaryTreeDictionary
Creates a dictionary that uses the supplied ternary tree for dictionary searches.- Parameters:
tree- ternary tree used to back dictionary.
-
-
Method Details
-
size
public long size()Description copied from interface:DictionaryReturns the number of words in this dictionary- Specified by:
sizein interfaceDictionary- Returns:
- total number of words to search
-
search
Description copied from interface:DictionaryReturns whether the supplied word exists in the dictionary.- Specified by:
searchin interfaceDictionary- Parameters:
word- to search for- Returns:
- whether word was found
-
partialSearch
Returns an array of strings which partially match the supplied word. This search is case-sensitive by default. SeeTernaryTree.partialSearch(java.lang.CharSequence).- Parameters:
word- to search for- Returns:
- array of matching words
-
nearSearch
Returns an array of strings which are near to the supplied word by the supplied distance. This search is case-sensitive by default. SeeTernaryTree.nearSearch(java.lang.CharSequence, int).- Parameters:
word- to search fordistance- for valid match- Returns:
- array of matching words
-
getTernaryTree
Returns the underlying ternary tree used by this dictionary.- Returns:
- ternary tree
-
main
Provides command line access to a ternary tree dictionary.- Parameters:
args- command line arguments- Throws:
Exception- if an error occurs
-