-
Notifications
You must be signed in to change notification settings - Fork 0
/
week8.py
48 lines (38 loc) · 1.22 KB
/
week8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
## Week 8
# spell checker
def readDictionaryFile(dictionaryFilename):
dictionaryWords = []
inputFile = open(dictionaryFilename, "r")
for line in inputFile:
word = line.strip()
dictionaryWords.append(word)
inputFile.close()
return dictionaryWords
def readTextFile(textFilename):
words = []
inputFile = open(textFilename, "r")
for line in inputFile:
wordsOnLine = line.strip().split()
for word in wordsOnLine:
words.append(word.strip(".,!\":;?").lower())
inputFile.close()
return words
def findErrors(dictionaryWords, textWords):
misspelledWords = []
for word in textWords:
if word not in dictionaryWords:
misspelledWords.append(word)
return misspelledWords
def printErrors(errorList):
print("The misspelled words are: ")
for word in errorList:
print(word)
def main():
print("Welcome to the spell checker")
dictionaryFile = input("Please enter the dictionary file: ")
textFile = input("Please enter the text file: ")
dictionaryList = readDictionaryFile(dictionaryFile)
textList = readTextFile(textFile)
errorList = findErrors(dictionaryList, textList)
printErrors(errorList)
main()