#!/usr/bin/env python # # Copyright (c) 2008 Benjamin Schweizer. All rights reserved. # # Abstract # ~~~~~~~~ # simple spellchecker, see http://www.justin.tv/problems/spellcheck # (no, i'm not applying, i'm just fighting boredom:-) # # Authors # ~~~~~~~ # Benjamin Schweizer # # Changes # ~~~~~~~ # 2008-06-14, schweizer: initial release. # # Todo # ~~~~ # - we'll see # def strip_doubles(word): """strip double characters from string""" result = '' last_char = False for char in word.lower(): if char != last_char: result += char last_char = char return result def strip_vowels(word): """remove vowels from string""" result = '' last_char = False for char in word.lower(): if char not in ['a', 'e', 'i', 'o', 'u']: result += char last_char = char return result def strip_both(word): """remove voewls and douvles from string""" result = strip_doubles(strip_vowels(word)) return result lookup = {} fh = open('/usr/share/dict/words') line = fh.readline() while line: word = line[:-1] lookup[word] = word lookup[word.lower()] = word lookup[strip_doubles(word.lower())] = word lookup[strip_vowels(word.lower())] = word lookup[strip_doubles(strip_vowels(word.lower()))] = word line = fh.readline() if '' in lookup: del lookup[''] try: while True: input = raw_input('> ') if input in lookup: print lookup[input] elif input.lower() in lookup: print lookup[input.lower()] elif strip_doubles(input.lower()) in lookup: print lookup[strip_doubles(input.lower())] elif strip_vowels(input.lower()) in lookup: print lookup[strip_vowels(input.lower())] elif strip_both(input.lower()) in lookup: print lookup[strip_both(input.lower())] else: print 'NO SUGGESTION' except KeyboardInterrupt: print "bye." # eof.