1 | def opendictionaryfile(file): |
2 | """ Every line from file Returned as dictionary type""" |
3 | dictionary= {} |
4 | file = open(file) |
5 | for line in file: |
6 | dictionary[line]=0 |
7 | |
8 | return dictionary |
9 | |
10 | def checkifvalidword(word): |
11 | """ Checks if word is a valid word. |
12 | Any nonalphabet or ' character in a word is considered not a word. |
13 | if word return 1 else 0""" |
14 | import re |
15 | pat = re.compile('[^a-zA-Z\']') |
16 | if pat.search(word) == None: |
17 | return 1 |
18 | else: |
19 | return 0 |
20 | |
21 | def printmistakes(misspelledwords): |
22 | "Prints Spelling mistakes in alphabetical order and number of occurances""" |
23 | alphalist = [] |
24 | for key in misspelledwords: |
25 | alphalist.append(key) |
26 | alphalist.sort() |
27 | for key in alphalist: |
28 | print key, |
29 | print ' (' + str(misspelledwords.get(key)) + ')' |
30 | |
31 | def spellcheckfile(txtfile, dictfile): |
32 | """ Spell check txtfile with dictionary of words specifed in dictfile |
33 | Prints Misspelled words and # occurances """ |
34 | dictionary = opendictionaryfile(dictfile) |
35 | misspelledwords = {} |
36 | txtfile = open(txtfile) |
37 | for line in txtfile: |
38 | for word in line.split(' '): |
39 | if checkifvalidword(word) == 1: |
40 | if (word + '\n' in dictionary) == 0: |
41 | misspelledwords[word] = misspelledwords.get(word, 0) + 1 |
42 | printmistakes(misspelledwords) |