Assignment 3 Part 4 (submitted by Morgan on Feb 9, 20:03)

Beautiful Code
Ka-Ping Yee

AUTHORS:   Adam   Calvin   Chris   David   Derek   Hunter   Jacob   Jason   Jun   Karl   Kevin   Michael   Morgan   Nadia   Nerissa   Omair   Peter   Peterson   Ping   Richard   Scott   Thanh   Varun

Download this file.

COMMENTS

Feb 13, 23:47 - Karl (line 19): It is inefficient to re-load the dictionary every time you want to find the next mis-spelled word. Instead it's better to load the dictionary once.

Feb 13, 23:48 - Karl (line 19): It's very inefficient to use a list, a dictionary is better.

Feb 13, 23:50 - Karl (line 11): The API of passing in an index to a list of words in the document seems to restrict the library user to a specific kind of "for" loop and the combination of module and program could be simpler without this restriciton

Feb 13, 23:51 - Karl (line 19): Nice compact way of reading words (just use a dictionary instead of a list)

Please log in if you would like to add comments.

   

  1 dictionary = 'dict.txt'
  2 
  3 def set_dictionary(filename='dict.txt'):
  4     """
  5     Load the specified file as the dictionary, overwriting 
  6     any previous dictionaries.
  7     """
  8     dictionary = filename
  9 
 10 
 11 def spellcheck_text(document_list, index=0):
 12     """
 13     Takes a list of words with whitespace removed, and an integer index 
 14     positive to count from left of list, negative to count from right).
 15     Starting at word <index>, return the index of the first word in the
 16     list not in the dictionary, or -1 if no such words are found.
 17     """
 18     dictionary_file = open(dictionary)
 19     dictionary_list = dictionary_file.read().split()
 20 
 21     count = index
 22     for word in document_list[index:]:
 23         if dictionary_list.count(word) == 0:
 24             return count
 25         else:
 26             count = count + 1
 27     return -1
 28 
 29 ###############################
 30 ##      Main for testing     ##
 31 ###############################
 32 
 33 def remove_punc(word):
 34   if word.isalpha(): 
 35     return word
 36   else:
 37     stripped_word = ''
 38     for char in word:
 39       if char.isalpha() or char == "'":
 40         stripped_word = stripped_word + char
 41     return stripped_word
 42 
 43 def test(filename='input.txt', index=0):
 44   input_file = open(filename)
 45   input_string = input_file.read()
 46   stripped_words = map(remove_punc, input_string.split())
 47   testhelp(stripped_words, 0)
 48 
 49 def testhelp(stripped_words, index):
 50   nextmisspell = spellcheck_text(stripped_words, index)
 51   print stripped_words[nextmisspell]
 52 
 53   testhelp(stripped_words, nextmisspell+1)
 54 
 55 ###############################
 56 ## Non-implemented functions ##
 57 ###############################
 58 
 59 def add_dictionary(filename):
 60     """
 61     Add the words in the specified file to the dictionary.
 62     """
 63 
 64 
 65 def add_to_dictionary(word):
 66     """
 67     Add the argument to the dictionary.
 68     """
 69 
 70 
 71 def delete_from_dictionary(word):
 72     """
 73     Remove the argument from the dictionary.
 74     """
 75 
 76 
 77 def ignore_word(word):
 78     """
 79     Do not find this word as misspelled until the spellchecker exits or is
 80     reloaded, even though it is not in the dictionary.
 81     """
 82 
 83 
 84 def find_alternate_words(word):
 85     """
 86     Return a list of the closest matches if the word is not in the dictionary,
 87     or an empty list if it is.
 88     """