Skip to content

Commit

Permalink
Spelling Checker Script (HarshCasper#992)
Browse files Browse the repository at this point in the history
* added script for spelling checker

* added readme, modified requirements, script
  • Loading branch information
tanishq-arya authored Apr 21, 2021
1 parent 00c3c92 commit 3dfadbd
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Python/Automatic_Spelling_Checker_Corrector/README.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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()
8 changes: 8 additions & 0 deletions Python/Automatic_Spelling_Checker_Corrector/input.txt
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions Python/Automatic_Spelling_Checker_Corrector/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nltk==3.5
pyspellchecker==0.6.2

0 comments on commit 3dfadbd

Please sign in to comment.