diff --git a/Python/Automatic_Spelling_Checker_Corrector/README.md b/Python/Automatic_Spelling_Checker_Corrector/README.md new file mode 100644 index 000000000..cea38db76 --- /dev/null +++ b/Python/Automatic_Spelling_Checker_Corrector/README.md @@ -0,0 +1,30 @@ +### Automatic Spelling Checker and Corrector + +This Script is used to detect spelling errors in a text and correct them if the user wishes to do so. + +### Setup + +1. Create a Virtual Environment. +2. Install the requirements by using `pip3 install -r requiremnts.txt` +3. Hurray.! You're ready to use the Script. + +### Prerequisites + +1. If you are using text place it in input.txt file. +2. If you want to use your own file place it in the current directory where the program exists. + +### Running a file + +1. Run the script using `python3 automatic_spelling_checker_corrector.py` + +2. When asked for input file enter `input.txt` + +3. Errors are displayed. If you want to correct the errors enter `y` or `yes` when asked. + +### Sample Test Case + +The input.txt file contains some misspelt text. + +Run the script and observe that output.txt file is created and it contains the corrected text. + +#### All the requirements for this script is mentioned in **requirements.txt** file. \ No newline at end of file diff --git a/Python/Automatic_Spelling_Checker_Corrector/automatic_spelling_checker_corrector.py b/Python/Automatic_Spelling_Checker_Corrector/automatic_spelling_checker_corrector.py new file mode 100644 index 000000000..831c20a4f --- /dev/null +++ b/Python/Automatic_Spelling_Checker_Corrector/automatic_spelling_checker_corrector.py @@ -0,0 +1,92 @@ +# imports +import sys +from spellchecker import SpellChecker +from nltk import word_tokenize + +# create an instance of the spellchecker +spell = SpellChecker() + +# tokens --> stores the tokenized words +tokens = [] + +def readTextFile(textFilename): + """This function is used to read the input file""" + global tokens + words = [] + inputFile = open(textFilename, "r") + tokens = word_tokenize(inputFile.read()) + + # Create a list of words from these tokens checking if the word is alphanumeric + words = [ + word + for word in tokens if word.isalpha() + ] + inputFile.close() + return words + +def findErrors(textWords): + """This function is used to detect the errors in file if any""" + misspelledWords = [] + for word in textWords: + # correction() --> method of spellchecker module to correct the word + if spell.correction(word) != word: + misspelledWords.append(word) + + return misspelledWords + +def printErrors(errorList): + """This function is used to print the errors""" + print("---------------------") + print("Misspelled words are:") + print("---------------------") + for word in errorList: + # candidates() --> method of spellchecker module to find suitable corrections of the word + print(f'{word} : {spell.candidates(word)}') + +def correctErrors(errorList): + """This function is used to correct the errors and + write the corrected text in output.txt file""" + # open a new file to write the corrected text + outputFile = open("output.txt","w") + for word in tokens: + if word in errorList: + # if word is incorrect we replace it with the corrected word + word = spell.correction(word) + + # this writes text to the new output.txt file + outputFile.write(" ".join(tokens)) + + outputFile.close() + +def main(): + """This is the main function""" + textFile = input("Enter text file: ") + + textList = readTextFile(textFile) + errorList = findErrors(textList) + + # if there are no errors + if len(errorList) == 0: + print("No errors detected") + return + + # call to printErrors function + printErrors(errorList) + + # ask if user needs to correct the text + user_answer = input("Do you want to auto correct the errors, Y/N ? ") + + if user_answer.lower()=='y' or user_answer.lower()=='yes': + # call to correctErrors function + correctErrors(errorList) + print("-------------------------------------------------") + print("Check the output.txt file for the corrected text.") + print("-------------------------------------------------") + print("Thankyou for using spelling checker program.") + else: + print("--------------------------------------------") + print("Thankyou for using spelling checker program.") + print("--------------------------------------------") + +# call to the main function +main() diff --git a/Python/Automatic_Spelling_Checker_Corrector/input.txt b/Python/Automatic_Spelling_Checker_Corrector/input.txt new file mode 100644 index 000000000..9fdc36357 --- /dev/null +++ b/Python/Automatic_Spelling_Checker_Corrector/input.txt @@ -0,0 +1,8 @@ +This free software tool helps you automaticaly produce misspelled text from the correct one. +Pytohn is an intrepreted high-leelv genreal-puprsoe prgoramimng language. Pyhton's design philospohy emhpasizes code readabiilty with its ntaoble ues of sginiifcant indentation. Its lanugage constructs as wlel as its obejtc-orientde approach ami to hlep prgroammres write clear, lgocial code fro samll adn lrage-scale projcets.[30] + +Ptyhon is dynamically-typde adn garbage-colcleted. It supoprts mlutiple prgoramimng paardigms, including strcutrued (paritcluarly, procedural), ojbect-oriented adn funcitonal programming. Pytonh is often descbried as a "batetires incluedd" language due to ist copmrehensive standard library.[31] + +Giduo van Rossum began wokring on Pythno in the laet 198s0, as a succsesor to the ABC prgoarmming lagnuage, and frist released it in 1919 as Ptyhon 0.9.0.[32] Pyhton 2.0 was releasde in 2000 and introduced nwe featrues, schu as list comprehenisons adn a garbage colcletion ssytme usngi refrenece coutnnig adn was dsicontinued with version 2.7.18 in 2002.[33] Pythno 3.0 wsa releasde in 2008 and wsa a major rveiison of the lagnuage taht is nto comlpetely bacwkard-compatible and much Pytohn 2 cdoe does not run unmodified on Python 3. + +Python consistetnly ranks as one of teh mots populra prorgamming lagnuages. \ No newline at end of file diff --git a/Python/Automatic_Spelling_Checker_Corrector/requirements.txt b/Python/Automatic_Spelling_Checker_Corrector/requirements.txt new file mode 100644 index 000000000..e4b7a057b --- /dev/null +++ b/Python/Automatic_Spelling_Checker_Corrector/requirements.txt @@ -0,0 +1,2 @@ +nltk==3.5 +pyspellchecker==0.6.2 \ No newline at end of file