1 | # Nerissa Ying - cs198-ad
|
2 | # CS198 - Craft #2
|
3 | # Date Created: 02/02/2003
|
4 | # Date Last Modified: 02/09/2003
|
5 | # Description: Design for module speling.
|
6 | # speling.py
|
7 |
|
8 | import re
|
9 |
|
10 | dictionary = {}
|
11 |
|
12 | def addWord(wordStr):
|
13 | """Adds a word to the dictionary.
|
14 | """
|
15 | pass
|
16 |
|
17 | def removeWord(wordStr):
|
18 | """Removes a word from the dictionary.
|
19 | """
|
20 | pass
|
21 |
|
22 | def checkWord(wordStr):
|
23 | """Checks to see if the wordStr matches a word in the
|
24 | dictionary.
|
25 |
|
26 | If wordStr is found within the dictionary, returns 1
|
27 | (true). Else, it returns 0 (false).
|
28 | """
|
29 | if dictionary.get(wordStr):
|
30 | return 1
|
31 | elif dictionary.get(wordStr.lower()):
|
32 | return 1
|
33 | else:
|
34 | return 0
|
35 |
|
36 | def checkSentence(sentenceStr):
|
37 | """Checks to make sure all the words in the sentence are
|
38 | spelled correctly.
|
39 |
|
40 | Returns a list of misspelled words in the 'sentence'. A
|
41 | 'sentence' is simply a line of text for this function.
|
42 | """
|
43 | sentence = sentenceStr.strip()
|
44 | punctiation = re.compile('[!-&(-@[-_{-~]+')
|
45 | sentence = punctiation.sub('', sentence)
|
46 | word_list = sentence.split()
|
47 | misspell_list = []
|
48 | for word in word_list:
|
49 | if checkWord(word) != 1:
|
50 | misspell_list.append(word)
|
51 | return misspell_list
|
52 |
|
53 | def checkPassage(passage):
|
54 | """Checks to make sure all the words in the passage are
|
55 | spelled correctly.
|
56 |
|
57 | Returns a list of misspelled words in the whole passage given
|
58 | a text file.
|
59 | """
|
60 | file = open(passage)
|
61 | misspell_list = []
|
62 | for line in file:
|
63 | misspell_list.extend(checkSentence(line))
|
64 | misspell_list.sort()
|
65 | return misspell_list
|
66 |
|
67 | # load dict.txt into the dictionary
|
68 | dict_file = open('dict.txt')
|
69 | for word in dict_file:
|
70 | dictionary[word.strip()] = word.strip() |