Skip to content

Commit 3dfadbd

Browse files
authored
Spelling Checker Script (HarshCasper#992)
* added script for spelling checker * added readme, modified requirements, script
1 parent 00c3c92 commit 3dfadbd

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
### Automatic Spelling Checker and Corrector
2+
3+
This Script is used to detect spelling errors in a text and correct them if the user wishes to do so.
4+
5+
### Setup
6+
7+
1. Create a Virtual Environment.
8+
2. Install the requirements by using `pip3 install -r requiremnts.txt`
9+
3. Hurray.! You're ready to use the Script.
10+
11+
### Prerequisites
12+
13+
1. If you are using text place it in input.txt file.
14+
2. If you want to use your own file place it in the current directory where the program exists.
15+
16+
### Running a file
17+
18+
1. Run the script using `python3 automatic_spelling_checker_corrector.py`
19+
20+
2. When asked for input file enter `input.txt`
21+
22+
3. Errors are displayed. If you want to correct the errors enter `y` or `yes` when asked.
23+
24+
### Sample Test Case
25+
26+
The input.txt file contains some misspelt text.
27+
28+
Run the script and observe that output.txt file is created and it contains the corrected text.
29+
30+
#### All the requirements for this script is mentioned in **requirements.txt** file.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This free software tool helps you automaticaly produce misspelled text from the correct one.
2+
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]
3+
4+
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]
5+
6+
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.
7+
8+
Python consistetnly ranks as one of teh mots populra prorgamming lagnuages.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
nltk==3.5
2+
pyspellchecker==0.6.2

0 commit comments

Comments
 (0)