|
| 1 | +# imports |
| 2 | +import sys |
| 3 | +from spellchecker import SpellChecker |
| 4 | +from nltk import word_tokenize |
| 5 | + |
| 6 | +# create an instance of the spellchecker |
| 7 | +spell = SpellChecker() |
| 8 | + |
| 9 | +# tokens --> stores the tokenized words |
| 10 | +tokens = [] |
| 11 | + |
| 12 | +def readTextFile(textFilename): |
| 13 | + """This function is used to read the input file""" |
| 14 | + global tokens |
| 15 | + words = [] |
| 16 | + inputFile = open(textFilename, "r") |
| 17 | + tokens = word_tokenize(inputFile.read()) |
| 18 | + |
| 19 | + # Create a list of words from these tokens checking if the word is alphanumeric |
| 20 | + words = [ |
| 21 | + word |
| 22 | + for word in tokens if word.isalpha() |
| 23 | + ] |
| 24 | + inputFile.close() |
| 25 | + return words |
| 26 | + |
| 27 | +def findErrors(textWords): |
| 28 | + """This function is used to detect the errors in file if any""" |
| 29 | + misspelledWords = [] |
| 30 | + for word in textWords: |
| 31 | + # correction() --> method of spellchecker module to correct the word |
| 32 | + if spell.correction(word) != word: |
| 33 | + misspelledWords.append(word) |
| 34 | + |
| 35 | + return misspelledWords |
| 36 | + |
| 37 | +def printErrors(errorList): |
| 38 | + """This function is used to print the errors""" |
| 39 | + print("---------------------") |
| 40 | + print("Misspelled words are:") |
| 41 | + print("---------------------") |
| 42 | + for word in errorList: |
| 43 | + # candidates() --> method of spellchecker module to find suitable corrections of the word |
| 44 | + print(f'{word} : {spell.candidates(word)}') |
| 45 | + |
| 46 | +def correctErrors(errorList): |
| 47 | + """This function is used to correct the errors and |
| 48 | + write the corrected text in output.txt file""" |
| 49 | + # open a new file to write the corrected text |
| 50 | + outputFile = open("output.txt","w") |
| 51 | + for word in tokens: |
| 52 | + if word in errorList: |
| 53 | + # if word is incorrect we replace it with the corrected word |
| 54 | + word = spell.correction(word) |
| 55 | + |
| 56 | + # this writes text to the new output.txt file |
| 57 | + outputFile.write(" ".join(tokens)) |
| 58 | + |
| 59 | + outputFile.close() |
| 60 | + |
| 61 | +def main(): |
| 62 | + """This is the main function""" |
| 63 | + textFile = input("Enter text file: ") |
| 64 | + |
| 65 | + textList = readTextFile(textFile) |
| 66 | + errorList = findErrors(textList) |
| 67 | + |
| 68 | + # if there are no errors |
| 69 | + if len(errorList) == 0: |
| 70 | + print("No errors detected") |
| 71 | + return |
| 72 | + |
| 73 | + # call to printErrors function |
| 74 | + printErrors(errorList) |
| 75 | + |
| 76 | + # ask if user needs to correct the text |
| 77 | + user_answer = input("Do you want to auto correct the errors, Y/N ? ") |
| 78 | + |
| 79 | + if user_answer.lower()=='y' or user_answer.lower()=='yes': |
| 80 | + # call to correctErrors function |
| 81 | + correctErrors(errorList) |
| 82 | + print("-------------------------------------------------") |
| 83 | + print("Check the output.txt file for the corrected text.") |
| 84 | + print("-------------------------------------------------") |
| 85 | + print("Thankyou for using spelling checker program.") |
| 86 | + else: |
| 87 | + print("--------------------------------------------") |
| 88 | + print("Thankyou for using spelling checker program.") |
| 89 | + print("--------------------------------------------") |
| 90 | + |
| 91 | +# call to the main function |
| 92 | +main() |
0 commit comments