Assignment 3 Part 4 (submitted by David on Feb 10, 17:40)

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 11, 22:38 - David (line 22): I should have included '-' in this.

Feb 15, 17:22 - David (line 35): Should have used a dict instead of a list. Faster.

Feb 23, 16:11 - Ping (line 34): word is a local variable, not a reference to a position within the list. Reassigning the variable can never change the value of something else; it just changes what word refers to.

Please log in if you would like to add comments.

   

  1 # David Wallace
  2 # Homework 3.1: check spelling of words in input.txt
  3 
  4 # Unfortunately I didn't find a partner, so
  5 # I wrote 'speling' first, then the checker of 'input.txt'.
  6 
  7 import re
  8 
  9 dictfile = "dict.txt"
 10 dictdata = None
 11 
 12 def correct(word):
 13     """Decide if a word is spelled correctly"""
 14     loaddict()
 15     for dictword in dictdata:
 16         if word == dictword or word.lower() == dictword:
 17             return True
 18     return False
 19 
 20 def split_text_to_words(text):
 21     """Convert string to list of words, ignoring punctuation"""
 22     notword = re.compile("[^a-zA-Z']")    
 23     return notword.sub(' ',text).split() # convert non-word chars to spaces
 24 
 25 
 26 # This should be a private function somehow.
 27 def loaddict():
 28     """Read dictionary file into memory, if not already present."""
 29     global dictdata
 30     if not dictdata:
 31         print "loading dictionary..."
 32         file = open(dictfile, 'r')
 33         dictdata = file.readlines()
 34         #for word in dictdata: word = word.strip()  # why doesn't this work?
 35         dictdata = [word.strip() for word in dictdata] 
 36         file.close()