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]

Bases: collections.abc.MutableSet

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:
        print(word)
add(word)[source]

Add an element.

clear()[source]

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

contains_prefix(word)[source]
discard(word)

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

remove(word)[source]

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

remove_prefix(word)[source]