#!/usr/bin/env python # Karl Chen # $Id: speling.py,v 1.8 2003/02/07 08:33:07 quarl Exp $ import re global dict def read_dictionary(dictionary_file): """Load a dictionary file. DICTIONARY_FILE should be a string filename.""" global dict dict = {} file = open(dictionary_file) while 1: line = file.readline() if not line: break dict[line.split('\n')[0]] = 1 def read_file(file): """Reads a file and returns a string of its contents.""" return ''.join(open(file).readlines()) def check_word(word): """Returns true if a word is spelled correctly.""" return dict.get(word) def tokenize_text(text): """Tokenize a block of text into a list of words. Input TEXT is any string. Returns a list of words.""" words = re.split('[^A-Za-z]+', text) if not words: return None if words and words[0] == '': words = words[1:] if words and words[-1] == '': words = words[0:-1] return words