4 Ways to Correct Grammar with Python

Deepanshu Bhalla Add Comment

This tutorial explains various methods for checking and correcting grammar using Python. Automatic grammar correction helps students, professionals and content creators to make sure their writing follows proper grammar rules.

Check and correct grammatical errors using python
Table of Contents

1. LanguageTool

We can use LanguageTool in python using the library language-tool-python. It is an open source tool to check grammar and spelling mistakes which means it is available for free without any registration.

To install the python library for LanguageTool, run the command below.

pip install language-tool-python

Let's take a sample text to explain this library with an example.

Sample Text : I is testng grammar tool using python. It does not costt anythng.
Code

In the program below, we are loading the library first and then running the library on input text. It returns the corrected text after fixing spelling mistakes and grammar.

import language_tool_python

mytext = """
I is testng grammar tool using python. It does not costt anythng.
"""

def grammarCorrector(text):
    tool = language_tool_python.LanguageTool('en-US')
    result = tool.correct(text)
    return result

output_data = grammarCorrector(mytext)
print(output_data)
Output
I am testing grammar tool using python. It does not cost anything.
How to Check Grammar

In the previous section, it automatically corrects grammar in the sentence. Here we are interested to see the grammar and spelling mistakes in the text along with the replacements suggested by the tool.

def grammarChecker(text):
    tool = language_tool_python.LanguageTool('en-US')
    result = tool.check(text)
    return result

output_data = grammarChecker(mytext)
print(output_data)
Output

[Match({'ruleId': 'PERS_PRONOUN_AGREEMENT', 'message': 'Did you mean “am” or “will be”?', 'replacements': ['am', 'will be'], 'offsetInContext': 3, 'context': ' I is testng grammar tool using python. It do...', 'offset': 3, 'errorLength': 2, 'category': 'GRAMMAR', 'ruleIssueType': 'grammar', 'sentence': 'I is testng grammar tool using python.'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['testing'], 'offsetInContext': 6, 'context': ' I is testng grammar tool using python. It does not ...', 'offset': 6, 'errorLength': 6, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'I is testng grammar tool using python.'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['cost', 'costs', 'Costa'], 'offsetInContext': 43, 'context': '... grammar tool using python. It does not costt anythng. ', 'offset': 52, 'errorLength': 5, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'It does not costt anythng.'}), Match({'ruleId': 'MORFOLOGIK_RULE_EN_US', 'message': 'Possible spelling mistake found.', 'replacements': ['anything'], 'offsetInContext': 43, 'context': '...ar tool using python. It does not costt anythng. ', 'offset': 58, 'errorLength': 7, 'category': 'TYPOS', 'ruleIssueType': 'misspelling', 'sentence': 'It does not costt anythng.'})]

To see the number of errors, use the code below.

len(output_data)
# 4

To see the detailed description of the first issue in the text, use the command below.

output_data[0]
Match({'ruleId': 'PERS_PRONOUN_AGREEMENT', 'message': 'Did you mean “am” or “will be”?', 'replacements': ['am', 'will be'], 'offsetInContext': 3, 'context': ' I is testng grammar tool using python. It do...', 'offset': 3, 'errorLength': 2, 'category': 'GRAMMAR', 'ruleIssueType': 'grammar', 'sentence': 'I is testng grammar tool using python.'})

To see the replacements suggested by the tool for the first issue, use the command below.

output_data[0].replacements
# ['am', 'will be']
Simplify Output of LanguageTool

To simplify the output, extract only grammatical errors and their suggested corrections from the output.

def check_text(input_text):
    
    # Initialize LanguageTool instance  
    tool = language_tool_python.LanguageTool('en-US')  

    # Check for language errors in the input text  
    matches = tool.check(input_text)  

    # Initialize lists to store detected mistakes and their corrections  
    mistakes = []  
    corrections = []  
    start_positions = []  
    end_positions = []  

    # Iterate through the detected language errors  
    for rule in matches:  
        if len(rule.replacements) > 0:  
            start_positions.append(rule.offset)  
            end_positions.append(rule.errorLength + rule.offset)  
            mistakes.append(input_text[rule.offset : rule.errorLength + rule.offset])  
            corrections.append(rule.replacements[0])   

    return list(zip(mistakes,corrections))

check_text(mytext)
Output
[('is', 'am'),
 ('testng', 'testing'),
 ('costt', 'cost'),
 ('anythng', 'anything')]
Supported Languages in LanguageTool

In the program above, we used American English (en-US) as a language to detect grammar and spelling errors in the text. You can use other variants of English as follows.

  • en-CA: Canadian English
  • en-AU: Australian English
  • en-NZ: New Zealand English
  • en-GB: British English
  • en-ZA: South African English

The library supports many languages other than English as shown below. To detect the language automatically, you can use auto language code.

ar, ast-ES, be-BY, br-FR, ca-ES, ca-ES-valencia, da-DK, de, de-AT, de-CH, de-DE, el-GR, en, en-AU, en-CA, en-GB, en-NZ, en-US, en-ZA, eo, es, es-AR, fa, fr, ga-IE, gl-ES, it, ja-JP, km-KH, nl, nl-BE, pl-PL, pt, pt-AO, pt-BR, pt-MZ, pt-PT, ro-RO, ru-RU, sk-SK, sl-SI, sv, ta-IN, tl-PH, uk-UA, zh-CN

2. Gramformer

Gramformer is an open source python library based on some of the top notch researches in grammar correction. It has several models which can detect, highlight and correct grammar errors.

Installation

Make sure to install libraries such as torch and spacy along with Gramformer.


pip install -U git+https://github.com/PrithivirajDamodaran/Gramformer.git
pip install torch
pip install spacy
python -m spacy download en

To correct grammar automatically in the text, run the program below.


from gramformer import Gramformer
import torch

def set_seed(seed):
  torch.manual_seed(seed)
  if torch.cuda.is_available():
    torch.cuda.manual_seed_all(seed)

set_seed(1212)
gf = Gramformer(models = 1, use_gpu=False)
mytext = """I is testng grammar tool using python. It does not costt anythng."""
gf.correct(mytext, max_candidates=1)
Output
{'I am testing grammar tool using python. It does not cost anything.'}

To highlight grammar issues in the text along with its solutions, run gf.correct first and pass the output of it with the input text to gf.highlight method.


correction = gf.correct(mytext, max_candidates=1)
for corrected_sentence in correction:
    print(gf.highlight(mytext, corrected_sentence))
Output
 
I <c type='VERB:SVA' edit='am'>is</c> <c type='SPELL' edit='testing'>testng</c> grammar tool using python. It does not <c type='SPELL' edit='cost'>costt</c> <c type='NOUN' edit='anything.'>anythng.</c>

3. Ginger

Ginger Software allows you to check and correct grammar in Python via its API. It offers both free and paid versions. The free version has a limit of 2000 sentences per month and you need to subscribe before using it.

To get your API key, you need to visit the Ginger Software Page on the RapidAPI website.

Make sure the requests library is installed before using the program below. In the following code, don't forget to enter your API key in the API variable.


import requests
API = "Enter Your Ginger API Key"
url = "https://ginger4.p.rapidapi.com/correction"
querystring = {"lang":"US","generateRecommendations":"false","flagInfomralLanguage":"true"}
mytext = """I is testng grammar tool using python. It does not costt anythng."""
headers = {
	"content-type": "text/plain",
	"Content-Type": "text/plain",
	"Accept-Encoding": "identity",
	"X-RapidAPI-Key": API,
	"X-RapidAPI-Host": "ginger4.p.rapidapi.com"
}

response = requests.post(url, data=mytext, headers=headers, params=querystring)
print(response.json())

4. pyaspeller

pyaspeller is a python library to correct spelling mistakes but is unable to correct grammar. You can install this library by using the command pip install pyaspeller.


from pyaspeller import YandexSpeller
def error_correct_pyspeller(sample_text):
    speller = YandexSpeller()
    fixed = speller.spelled(sample_text)
    return fixed

mytext = """I is testng grammar tool using python. It does not costt anythng."""
output_text = error_correct_pyspeller(mytext)
print(output_text)
Output
I is testing grammar tool using python. It does not cost anything.
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "4 Ways to Correct Grammar with Python"
Next → ← Prev