SimpleCxxLib
packageclass Lexicon
Lexicon
class supports efficient lookup operations for words and prefixes.
As an example of the use of the Lexicon
class, the
following program lists all the two-letter words in the lexicon
stored in EnglishWords.dat
:
int main() { Lexicon english("EnglishWords.dat"); for (const string &word: english) { if (word.length() == 2) { cout << word << endl; } } return 0; }
Constructor | |
Lexicon(filename) | Initializes a new lexicon. |
Methods | |
Adds the specified word to the lexicon. | |
Reads the file and adds all of its words to the lexicon. | |
Removes all words from the lexicon. | |
Returns true if word is contained in the lexicon. | |
Returns true if any words in the lexicon begin with prefix . | |
Returns true if the lexicon contains no words. | |
Calls the specified function on each word in the lexicon. | |
Returns the number of words contained in the lexicon. |
Lexicon(); Lexicon(string filename);
English.dat
containing a list of words in English. The standard code pattern
to initialize that lexicon looks like this:
Lexicon english("English.dat");
Usage:
Lexicon lex; Lexicon lex(filename);
int size() const;
Usage:
int n = lex.size();
bool isEmpty() const;
true
if the lexicon contains no words.
Usage:
if (lex.isEmpty()) ...
void clear();
Usage:
lex.clear();
void add(string word);
Usage:
lex.add(word);
void addWordsFromFile(string filename);
Usage:
lex.addWordsFromFile(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)) ...
void mapAll(void (*fn)(string)) const; void mapAll(void (*fn)(const string &)) const; void mapAll(FunctorType fn) const;
Usage:
lexicon.mapAll(fn);