campy.datastructures.lexicon module

This file exports a Lexicon class, a compact structure for storing a list of words.

This Lexicon implementation is backed by a data structure called a prefix tree or trie (“try”).

class campy.datastructures.lexicon.Lexicon(file=None)[source]


Representation of a Lexicon, or word list.

The main difference between a lexicon and a dictionary is that a lexicon does not provide any mechanism for storing definitions; the lexicon contains only words, with no associated information.

It is therefore similar to a set of strings, but with a more space-efficient internal representation. The Lexicon class supports efficient lookup operations for words and prefixes.

For example, the following program lists all of the two-letter words in the lexicon stored at english.lex:

lex = Lexicon('english.lex')
for word in lex:
    if len(word) == 2:

Add an element.


This is slow (creates N new iterators!) but effective.


Remove an element. If not a member, raise a KeyError.


Remove an element. If not a member, raise a KeyError.