Search
  • English
Login Register
  • Mon - Sat 11.00 am - 8:00 pm
  • 1st-29, Atlanta Business Hub, VIP Road, Surat
  • +91 97129 28220
Code and Debug
  • Offline Courses
    • C Programming
    • Cpp Programming
    • Django Framework
    • Flutter Development
    • HTML & CSS
    • Javascript
    • MySQL
    • Node.js (Core)
    • Node.js (Advance)
    • Python Programming
  • About Us
  • Contact Us
  • Blog
  • Offline Courses
    • C Programming
    • Cpp Programming
    • Django Framework
    • Flutter Development
    • HTML & CSS
    • Javascript
    • MySQL
    • Node.js (Core)
    • Node.js (Advance)
    • Python Programming
  • About Us
  • Contact Us
  • Blog
Code and Debug > Blog > Project > Python Project > Mini Python Project > Spell and Grammar Checker in Python

Spell and Grammar Checker in Python

  • November 15, 2022
  • Posted by: Code and Debug
  • Category: Mini Python Project Project
No Comments

In this blog you will be making your own Spell and Grammar checker by using opensource tool knows as LanguageTool. 

What is Language Tool?

LanguageTool is an open-source grammar tool, also known as the spellchecker for OpenOffice. This library allows you to detect grammar errors and spelling mistakes through a Python script or through a command-line interface.

We will work with the language_tool_python python package which can be installed with the pip install language-tool-python command.

By default, language_tool_python will download a LanguageTool server .jar and run that in the background to detect grammar errors locally.

Prerequisite for running Language Tool

You need to have Java installed for Language tool run properly. Java is required because Language Tool runs its own server locally and can be unlimited amount of times.

Install Language tool python using pip install language-tool-python.

Using Language Tool in Python

Let us start by using an example so that our code detect grammar mistakes and also correct them. We will work with the following text.

“LanguageTool offers spell and grammar checking. Just paste your text here and click the ‘Check Text’ button. Click the colored phrases for details on potential errors. or use this text too see an few of of the problems that LanguageTool can detecd. What do you thinks of grammar checkers? Please not that they are not perfect. Style issues get a blue marker: It’s 5 P.M. in the afternoon. The weather was nice on Thursday, 27 June 2017“

The text highlighted in bold are some of the grammar errors. Let’s find it out using Python code and correct them.

See a sample code given below and run it.

				
					import language_tool_python

tool = language_tool_python.LanguageTool("en-US")

text = """LanguageTool offers spell and grammar checking. 
Just paste your text here and click the 'Check Text' button. 
Click the colored phrases for details on potential errors. 
or use this text too see an few of of the problems that LanguageTool can detecd. 
What do you thinks of grammar checkers? Please not that they are not perfect. 
Style issues get a blue marker: It's 5 P.M. in the afternoon. 
The weather was nice on Thursday, 27 June 2017"""


# get the matches
matches = tool.check(text)

for match in matches:
    print(match)

				
			

And we get the following output:

				
					Offset 168, length 2, Rule ID: UPPERCASE_SENTENCE_START
Message: This sentence does not start with an uppercase letter.
Suggestion: Or
...hrases for details on potential errors. or use this text too see an few of of the ...
                                           ^^
Offset 185, length 7, Rule ID: TOO_TO
Message: Did you mean “to see”?
Suggestion: to see
...s on potential errors. or use this text too see an few of of the problems that Language...
                                           ^^^^^^^
Offset 193, length 2, Rule ID: EN_A_VS_AN
Message: Use “a” instead of ‘an’ if the following word doesn’t start with a vowel sound, e.g. ‘a sentence’, ‘a university’.
Suggestion: a
...ential errors. or use this text too see an few of of the problems that LanguageToo...
                                           ^^
Offset 200, length 5, Rule ID: ENGLISH_WORD_REPEAT_RULE
Message: Possible typo: you repeated a word
Suggestion: of
...errors. or use this text too see an few of of the problems that LanguageTool can dete...
                                           ^^^^^
Offset 241, length 6, Rule ID: MORFOLOGIK_RULE_EN_US
Message: Possible spelling mistake found.
Suggestion: detect
...f of the problems that LanguageTool can detecd. What do you thinks of grammar checkers...
                                           ^^^^^^
Offset 261, length 6, Rule ID: DO_VBZ
Message: After the auxiliary verb ‘do’, use the base form of the main verb. Did you mean “think”?
Suggestion: think
...at LanguageTool can detecd. What do you thinks of grammar checkers? Please not that th...
                                           ^^^^^^
Offset 296, length 3, Rule ID: PLEASE_NOT_THAT
Message: Did you mean “note”?
Suggestion: note
... you thinks of grammar checkers? Please not that they are not perfect. Style issues...
                                           ^^^
Offset 366, length 21, Rule ID: PM_IN_THE_EVENING
Message: This is redundant. Consider using “P.M.”
Suggestion: P.M.
... Style issues get a blue marker: It's 5 P.M. in the afternoon. The weather was nice on Thursday, 27 J...
                                           ^^^^^^^^^^^^^^^^^^^^^
Offset 413, length 22, Rule ID: DATE_WEEKDAY
Message: The date 27 June 2017 is not a Thursday, but a Tuesday.
... the afternoon. The weather was nice on Thursday, 27 June 2017
                                           ^^^^^^^^^^^^^^^^^^^^^^
				
			

Here we can see we get offset means at which index the error has occurred, length – the length of error occurred and lastly Rule ID.

A very detailed explantation of RuleID can be found in the LanguageTool Community.

Now since we are able to detect mistakes, lets refactor our code to correct them. Below is the code to just print the replacement suggested by language tool. Also we can print the starting, ending position of where error occurs.

				
					import language_tool_python

tool = language_tool_python.LanguageTool("en-US")

text = """LanguageTool offers spell and grammar checking.
Just paste your text here and click the 'Check Text' button.
Click the colored phrases for details on potential errors.
or use this text too see an few of of the problems that
LanguageTool can detecd. What do you thinks of grammar checkers?
Please not that they are not perfect. Style issues get a blue marker:
It's 5 P.M. in the afternoon. The weather was nice on Thursday, 27 June 2017"""


matches = tool.check(text)

for match in matches:
    print(match)
    print(f"Error starts at => {match.offset}")
    print(f"Error ends at => {match.offset+match.errorLength}")
    print(f"Error in text => {text[match.offset:match.offset+match.errorLength]}")
    print(f"Can be replaced with => {match.replacements}", end="\n\n")
    print("--------------")

				
			

And we get the following output:

				
					Offset 168, length 2, Rule ID: UPPERCASE_SENTENCE_START
Message: This sentence does not start with an uppercase letter.
Suggestion: Or
...hrases for details on potential errors. or use this text too see an few of of the ...
                                           ^^
Error starts at => 168
Error ends at => 170
Error in text => or
Can be replaced with => ['Or']

--------------
Offset 185, length 7, Rule ID: TOO_TO
Message: Did you mean “to see”?
Suggestion: to see
...s on potential errors. or use this text too see an few of of the problems that Language...
                                           ^^^^^^^
Error starts at => 185
Error ends at => 192
Error in text => too see
Can be replaced with => ['to see']

--------------
Offset 193, length 2, Rule ID: EN_A_VS_AN
Message: Use “a” instead of ‘an’ if the following word doesn’t start with a vowel sound, e.g. ‘a sentence’, ‘a university’.
Suggestion: a
...ential errors. or use this text too see an few of of the problems that LanguageToo...
                                           ^^
Error starts at => 193
Error ends at => 195
Error in text => an
Can be replaced with => ['a']

--------------
Offset 200, length 5, Rule ID: ENGLISH_WORD_REPEAT_RULE
Message: Possible typo: you repeated a word
Suggestion: of
...errors. or use this text too see an few of of the problems that LanguageTool can dete...
                                           ^^^^^
Error starts at => 200
Error ends at => 205
Error in text => of of
Can be replaced with => ['of']

--------------
Offset 241, length 6, Rule ID: MORFOLOGIK_RULE_EN_US
Message: Possible spelling mistake found.
Suggestion: detect
...f of the problems that LanguageTool can detecd. What do you thinks of grammar checkers...
                                           ^^^^^^
Error starts at => 241
Error ends at => 247
Error in text => detecd
Can be replaced with => ['detect']

--------------
Offset 261, length 6, Rule ID: DO_VBZ
Message: After the auxiliary verb ‘do’, use the base form of the main verb. Did you mean “think”?
Suggestion: think
...at LanguageTool can detecd. What do you thinks of grammar checkers? Please not that th...
                                           ^^^^^^
Error starts at => 261
Error ends at => 267
Error in text => thinks
Can be replaced with => ['think']

--------------
Offset 296, length 3, Rule ID: PLEASE_NOT_THAT
Message: Did you mean “note”?
Suggestion: note
... you thinks of grammar checkers? Please not that they are not perfect. Style issues...
                                           ^^^
Error starts at => 296
Error ends at => 299
Error in text => not
Can be replaced with => ['note']

--------------
Offset 366, length 21, Rule ID: PM_IN_THE_EVENING
Message: This is redundant. Consider using “P.M.”
Suggestion: P.M.
... Style issues get a blue marker: It's 5 P.M. in the afternoon. The weather was nice on Thursday, 27 J...
                                           ^^^^^^^^^^^^^^^^^^^^^
Error starts at => 366
Error ends at => 387
Error in text => P.M. in the afternoon
Can be replaced with => ['P.M.']

--------------
Offset 413, length 22, Rule ID: DATE_WEEKDAY
Message: The date 27 June 2017 is not a Thursday, but a Tuesday.
... the afternoon. The weather was nice on Thursday, 27 June 2017
                                           ^^^^^^^^^^^^^^^^^^^^^^
Error starts at => 413
Error ends at => 435
Error in text => Thursday, 27 June 2017
Can be replaced with => []

--------------
				
			

Code to correct our mistakes

We saw how we can print the starting, ending position of our mistakes and also a replacement suggested by LanguageTool. 

Now we will store all our mistakes, errorStartPosition, errorEndPosition and corrections in a list.

And after that we will write our logic to correct and replace the text.

				
					import language_tool_python

tool = language_tool_python.LanguageTool("en-US")

text = """LanguageTool offers spell and grammar checking. 
Just paste your text here and click the 'Check Text' button. 
Click the colored phrases for details on potential errors.
or use this text too see an few of of the problems that 
LanguageTool can detecd. What do you thinks of grammar checkers?
Please not that they are not perfect. Style issues get a blue marker:
It's 5 P.M. in the afternoon. The weather was nice on Thursday, 27 June 2017"""


matches = tool.check(text)

corrections = []
mistakes = []
errorStartPosition = []
errorEndPosition = []

for match in matches:
    # To check if there are any correct replacement available
    if len(match.replacements) > 0:
        errorStartPosition.append(match.offset)
        errorEndPosition.append(match.offset + match.errorLength)
        mistakes.append(text[match.offset : match.offset + match.errorLength])
        corrections.append(match.replacements[0])

# Converting our originaltext into list
newText = list(text)

for i in range(len(errorStartPosition)):
    for j in range(len(text)):
        newText[errorStartPosition[i]] = corrections[i]
        if j > errorStartPosition[i] and j < errorEndPosition[i]:
            newText[j] = ""

# Joining our list to convert to string
newText = "".join(newText)

print(text)
print("\n-------------------\n")
print(newText)

				
			

And we get the following output:

				
					LanguageTool offers spell and grammar checking.
Just paste your text here and click the 'Check Text' button.
Click the colored phrases for details on potential errors.
or use this text too see an few of of the problems that
LanguageTool can detecd. What do you thinks of grammar checkers?
Please not that they are not perfect. Style issues get a blue marker:
It's 5 P.M. in the afternoon. The weather was nice on Thursday, 27 June 2017

-------------------

LanguageTool offers spell and grammar checking.
Just paste your text here and click the 'Check Text' button.
Click the colored phrases for details on potential errors.
Or use this text to see a few of the problems that
LanguageTool can detect. What do you think of grammar checkers?
Please note that they are not perfect. Style issues get a blue marker:
It's 5 P.M.. The weather was nice on Thursday, 27 June 2017
				
			

We will get the output where the text above the line is the original text and below is the correction made by our python code.

Test on your own

Below are some text to check if our python codes really solves grammar and spelling mistakes. Try to run them on your own.

  1. hello i am developer. i am TRAINing at
    Code & Debug, they provide us free traning resources
    and projects.
  2. i am not that goood in english, can I use
    my python code to solve my error?
  3. I m not good at engishs.

Leave a Reply Cancel reply

About US

At Code & Debug, our mission is to continuously innovate the best ways to train the next generation of developers and to transform the the way tech education is delivered.

Code & Debug was founded in 2020 to bridge the knowledge gap between colleges and industry. Founded by Anirudh Khurana, Code & Debug has professional teaching faculty and a state-of-art learning platform for Coding education.
View Courses

Pages

  • About Us
  • Contact Us
  • Home
  • Offline Courses
  • User Account

Contact Us

  • 1st-29, Atlanta Business Hub, VIP Road, Surat
  • Tel.: +91 97129 28220
  • info@codeanddebug.in