class LexiconLexicon
class supports efficient lookup operations for words and prefixes.
This version of Lexicon is implemented internally using a data structure called a prefix tree or trie (pronounced "try"), which allows for efficient prefix and word searching.
Some runtimes below are expressed in terms of "W" where W represents the length of the word being added.
| Constructor | ||
| O(1) | Initializes a new empty lexicon. | |
Lexicon(istream) |
O(WN) | Initializes a new lexicon that reads words from the given file or input stream. |
| Methods | ||
| O(W) | Returns true if word is contained in the lexicon. |
|
| O(W) | Returns true if any words in the lexicon begin with prefix. |
|
| O(1) | Returns the number of words contained in the lexicon. | |
| O(1) | Returns a printable string representation of this lexicon. | |
| Operators | ||
| O(N) | Iterates through the words in a lexicon in alphabetical order. | |
| O(N) | Outputs the contents of the lexicon to the given output stream. | |
| O(WN) | Reads the contents of the given input stream into the lexicon. | |
Lexicon(); Lexicon(istream& input); Lexicon(string filename);
EnglishWords.dat
containing a list of words in English. The standard code pattern
to initialize that lexicon looks like this:
Lexicon english("EnglishWords.dat");
Usage:
Lexicon lex; Lexicon lex(filename);
bool contains(string word) const;
true if word is contained in the
lexicon. In the Lexicon class, the case of letters is
ignored, so "Zoo" is the same as "ZOO" or "zoo".
Usage:
if (lex.contains(word)) ...
bool containsPrefix(string prefix) const;
prefix.
Like containsWord, this method ignores the case of letters
so that "MO" is a prefix of "monkey" or "Monday".
Usage:
if (lex.containsPrefix(prefix)) ...
int size() const;
Usage:
int n = lex.size();
string toString() const;
"{word1, word2, word3}".
Usage:
string str = lexicon.toString();
for (string word : lex)
Usage:
for (string word : lex) {
cout << word << endl;
}
Documented: Fall Quarter 2022
ostream& operator<<(const Lexicon& lex);Outputs the contents of
lex to the given output stream.
The output is in the form { word1, word2, word3 }. The words
will be listed in alphabetical order.
Usage:
cout << lex << endl;
Documented: Fall Quarter 2022
istream& operator>>(Lexicon& lex);Reads the contents of the given input stream into
lex. Any previous
contents of the lex are replaced.
The input is expected to be in the form { word1, word2, word3 }. If unable to read a proper
lexicon from the stream, the operation results in a stream fail state.
Usage:
if (infile >> lex) ...
Documented: Fall Quarter 2022