# spellcheck.py
# Steve Marschner (srm2)
# February 18, 2013
"""Spell checker lecture demo"""
import sys

# On Unix systems, including Mac OS, this file, or /usr/dict/words, usually 
# holds all the words in an English dictionary, one word per line.
#DICT_FILE = '/usr/share/dict/words'

# But I am using another word list, from Kevin Atkinson's SCOWL (Spell Checker
# Oriented Word Lists) project, see wordlist.sourceforge.net.
DICT_FILE = 'scowl_utf-8.txt'
PUNCTUATION = '.,:;?!-()"\''

def check_spelling(text):
    """Check the spelling in the string <text>.

    Checks the words in the given string against a dictionary, and prints
    messages about any words that are not found.  The method I am using here is
    very inefficient but will do for checking small files."""
    for word in text.split():
        word = word.strip(PUNCTUATION)
        if word and not word in dictionary and not word.lower() in dictionary:
            print 'I think the word "' + word + '" is misspelled.'

# Read the lines from the word list, strip off the newlines, resulting in a
# list of a couple hundred thousand strings.
dictionary = open(DICT_FILE).read().split('\n')

if __name__ == '__main__':
    # If the user gave us one filename on the command line, check it; otherwise
    # complain that there should have been one.
    if len(sys.argv) == 2:
        text = open(sys.argv[1]).read()
        check_spelling(text)
    else:
        print "Usage: python spellcheck.py <text file>"