Download this file.
Feb 15, 20:44 - Varun: Wow! very well-thought out implementation. Certainly provides the user with a lot of functionality.
Feb 15, 20:46 - Varun (line 18): Some comments to explain sub sections of the methods will be helpful. For instance saying "Checking to see if word with lower case or higher case first letter is present."
Feb 23, 16:04 - Ping (lines 15, 16): The first set of parentheses on this line is unnecessary.
Feb 23, 16:05 - Ping (line 43): This seems like a strange rule for determining what constitutes a word. What about numbers?
Feb 23, 16:06 - Ping (line 45): An easier way to check if a character is in a string is to use the in operator. For example, ' ' in word
Feb 23, 16:06 - Ping (lines 59, 65): The parentheses here are not needed.
Feb 23, 16:08 - Ping (line 72): I see you're looking at the value of the argument to decide whether to check a file or a string. This is generally not a good design choice, because you can't predict in advance what all files or strings will look like. I might have a file on my disk called "hello" that needs checking, or a string containing "hello" that needs checking. Writing one function to serve two different purposes like this is called overloading. In this case it would be better to simply write two separate functions.
Please log in if you would like to add comments. | |
1 | dict = {}
| 2 |
| 3 | def AddWord(word):
| 4 | """add a word to dictionary
| 5 | return true if sucessful add, otherwise false"""
| 6 | dict[word] = word
| 7 |
| 8 | def DelWord(word):
| 9 | """delete a word from dictionary"""
| 10 | """return true if sucessful, otherwise false"""
| 11 | dict[word] = None
| 12 |
| 13 | def CheckWord(word):
| 14 | """check if a word is in the dictionary"""
| 15 | upper = (word[0]).upper() + word[1:]
| 16 | lower = (word[0]).lower() + word[1:]
| 17 | """return 'word' if the word is mispelled"""
| 18 | if (dict.get(word) == None) and (dict.get(upper) == None) and (dict.get(lower) == None):
| 19 | return word
| 20 | else:
| 21 | return None
| 22 |
| 23 | def CheckSent(sent):
| 24 | """spell check the given sent
| 25 | return an array of mispelled word, and [] if no mispelled words"""
| 26 | for char in '0123456789,./;\'[]\\=<>?:"{}|+-_)(*&^%$#@!~':
| 27 | sent = sent.replace(char, ' ')
| 28 | res = [];
| 29 | for word in sent.split():
| 30 | if (CheckWord(word) != None):
| 31 | res.append(word)
| 32 | return res
| 33 |
| 34 | def LoadDic(filename):
| 35 | """add all the words in the given file to dictionary"""
| 36 | file = open(filename)
| 37 | for line in file:
| 38 | for word in line.split():
| 39 | AddWord(word)
| 40 |
| 41 | def IsWord(word):
| 42 | """return true if the input is a word"""
| 43 | # if it doesn't contain space, it's word
| 44 | # if it contain '~', it's not word
| 45 | if (word.replace('~', ' ') == word):
| 46 | return None
| 47 | if (word.replace(' ', '~') == word):
| 48 | return word
| 49 |
| 50 | def IsSent(sent):
| 51 | """return true if input is a sentence"""
| 52 | return sent
| 53 |
| 54 | def IsFN(filename):
| 55 | """return true if the input is a filename"""
| 56 | # the filename must end with .???
| 57 | if len(filename) <= 4:
| 58 | return None
| 59 | if (filename[-4] != '.'):
| 60 | return None
| 61 |
| 62 | # the file can not contain any funny symbols
| 63 | for fn in filename:
| 64 | for char in '!@#$%^&*(){}[]|<>,?':
| 65 | if (fn == char):
| 66 | return None
| 67 | return filename
| 68 |
| 69 | def SpellCheck(target):
| 70 | """Spellcheck the target
| 71 | target could be a word, a sentence, an essay, or a filename"""
| 72 | if IsFN(target):
| 73 | file = open(target)
| 74 | res = []
| 75 | for line in file:
| 76 | res.extend(CheckSent(line))
| 77 | return res
| 78 | else:
| 79 | return CheckSent(target) |
|