Gingerit Python: How to Correct Grammatical Errors with Python

Gingerit Python: How to Correct Grammatical Errors with Python

Want to check if a sentence is grammatically correct with Python? Try Gingerit - a free Python grammar checker.

If you want to implement a Python grammar checker and don’t know where to start, you’re at the right place. Python Gingerit module allows you to check and correct grammar free of charge, even without registering for a free account.

The module is based on Ginger Software, a grammar checker that allows you to eliminate writing pain points if English isn’t your first language:

Image 1 - Ginger software homepage (image by author)

Image 1 - Ginger software homepage (image by author)

Today you’ll learn how to use it in Python, and what are some of the issues and limitations you need to be aware of.

Prefer watching over reading? We have you covered:


How to Install Python Gingerit

The package is available on PyPi, which makes installation a breeze. Just run the following command from the terminal:

pip install gingerit

You can now open up a Python notebook or a script and start using the module. The code snippet below should correct the grammatical errors in a short sentence:

from gingerit.gingerit import GingerIt

text = 'This sentance contains a cuple of gramatical mistakes.'

parser = GingerIt()
parser.parse(text)

But instead, it results in a Gingerit JSONDecodeError with a message of Expecting value: line 1 column 1 (char 0):

Image 2 - Gingerit JSONDecodeError (image by author)

Image 2 - Gingerit JSONDecodeError (image by author)

Let’s see how to fix this Gingerit JSON error next.

Python Gingerit JSONDecodeError - How to Solve It

In addition to PyPi, the package is also available on GitHub. This means we can download the gingerit.py file and see what’s causing the JSON error.

To fix the issue, we’ll first need to install an additional Python package - CloudScraper. It’s used to bypass Cloudflare’s anti-bot page implemented with the Requests module:

pip install cloudscraper

Once installed, change the way the session is initialized on line 16:

# session = requests.Session()
session = cloudscraper.create_scraper()

The contents of GingerIt class (with library imports) should now look as follows:

import requests
import cloudscraper

URL = "..."  # noqa
API_KEY = "..."


class GingerIt(object):
    def __init__(self):
        self.url = URL
        self.api_key = API_KEY
        self.api_version = "2.0"
        self.lang = "US"

    def parse(self, text, verify=True):
        session = cloudscraper.create_scraper()
        request = session.get(
            self.url,
            params={
                "lang": self.lang,
                "apiKey": self.api_key,
                "clientVersion": self.api_version,
                "text": text,
            },
            verify=verify,
        )
        data = request.json()
        return self._process_data(text, data)
        
...

That’s all we need to solve the Gingerit JSONDecodeError. From now on, we’ll use the GingerIt class instead of the previously installed library, so keep that in mind.

Gingerit Python Examples - How Accurate is the Grammar Checker?

Let’s run the same code snippet as before, but without the library import:

text = 'This sentance contains a cuple of gramatical mistakes.'

parser = GingerIt()
parser.parse(text)

There are no errors anymore. Instead, the following Python dictionary is returned:

Image 3 - Result returned from Gingerit (image by author)

Image 3 - Result returned from Gingerit (image by author)

The result dictionary contains the original text input, the corrected sentence, and a list of corrections. We can get a better grasp of the corrections by placing them in a Pandas DataFrame:

import pandas as pd
pd.set_option('display.max_colwidth', None)

result = parser.parse(text)

pd.DataFrame(result['corrections'])

Here are the results:

Image 4 - Gingerit corrections in a Pandas DataFrame (image by author)

Image 4 - Gingerit corrections in a Pandas DataFrame (image by author)

Looks like Gingerit caught every grammatical error, but will that always be the case? Let’s find out.

Want to further tweak the visuals of Pandas DataFrames? This article has you covered:

How to Style Pandas DataFrames Like a Pro

Onto a slightly more complex example now. This time, the text will be longer and will contain more sophisticated grammatical mistakes:

text = "This paragraf will contain some grammatical misstakes. \
Theyre her to see how well does the Ginger gramar checking softwre work when acesed from the Python API. \
Fingrs crosed everything work."

parser = GingerIt()
parser.parse(text)

Here’s the output:

Image 5 - Gingerit dictionary output (image by author)

Image 5 - Gingerit dictionary output (image by author)

It’s quite lengthy, so let’s style it to get better insights into the corrections:

pd.DataFrame(parser.parse(text)['corrections'])
Image 6 - Gingerit corrections as a Pandas DataFrame (image by author)

Image 6 - Gingerit corrections as a Pandas DataFrame (image by author)

Overall, Gingerit did an excellent job. There’s only one objection - it failed to correct work into works in the last sentence. It’s not a major issue, just something to consider.

Limitations of Gingerit in Python - Can We Break It?

So, how far can you take Gingerit grammar Python API before running into errors and limitations? There’s only one way to find out.

The following code snippet tries to parse a 512-character-long string:

text = "This paragraf will be around 500 characters long. It's here to test the limits of the Ginger \
gramar softwar when accessed through the Python API. Do you think thre'll be any isues? Potentialy yes, \
but we'l have to test and see. And how cool is the fact you can check grammar and get detailed insigt into \
erors from Python, without registering for a free account or even buying a subscription? Its amazing, but \
there are limitations, of course. Lets see if we can break it by passing in this realy long parahraph."

parser = GingerIt()
pd.DataFrame(parser.parse(text)['corrections'])

Here are the results:

Image 7 - Gingerit on a 500+ character long string (image by author)

Image 7 - Gingerit on a 500+ character long string (image by author)

As you can see, there are no issues when trying to parse and correct a 500-character-long paragraph. But how far can we take it?

Let’s try 2000 characters:

long_text = "This paragraf will be around 2000 characters long. It's here to test the limits of the Ginger \
gramar softwar when accessed through the Python API. Do you think thre'll be any isues? Potentialy yes, \
but we'l have to test and see. And how cool is the fact you can check grammar and get detailed insigt into \
erors from Python, without registering for a free account or even buying a subscription? Its amazing, but \
there are limitations, of course. Lets see if we can break it by passing in this realy long parahraph. \
Second iteraion. It's here to test the limits of the Ginger \
gramar softwar when accessed through the Python API. Do you think thre'll be any isues? Potentialy yes, \
but we'l have to test and see. And how cool is the fact you can check grammar and get detailed insigt into \
erors from Python, without registering for a free account or even buying a subscription? Its amazing, but \
there are limitations, of course. Lets see if we can break it by passing in this realy long parahraph. \
Thrd iteration. It's here to test the limits of the Ginger \
gramar softwar when accessed through the Python API. Do you think thre'll be any isues? Potentialy yes, \
but we'l have to test and see. And how cool is the fact you can check grammar and get detailed insigt into \
erors from Python, without registering for a free account or even buying a subscription? Its amazing, but \
there are limitations, of course. Lets see if we can break it by passing in this realy long parahraph. \
Forth iteration. It's here to test the limits of the Ginger \
gramar softwar when accessed through the Python API. Do you think thre'll be any isues? Potentialy yes, \
but we'l have to test and see. And how cool is the fact you can check grammar and get detailed insigt into \
erors from Python, without registering for a free account or even buying a subscription? Its amazing, but \
there are limitations, of course. Lets see if we can break it by passing in this realy long parahraph."

parser = GingerIt()
result = parser.parse(long_text)
pd.DataFrame(result['corrections'])

This time we get an error:

Image 8 - Error when processing a too-long string (image by author)

Image 8 - Error when processing a too-long string (image by author)

It’s actually a well-known and discussed issue. The free API can’t handle more than 600 characters at once, so you’ll have to split your strings into smaller chunks. It’s not that big of an issue, but the error message should be more informative.

Gingerit Python FAQ

We’ll now go over some frequently asked questions regarding Python and Gingerit.

Is Gingerit free to use?

From Python and their website - yes. You can use Gingerit Python for free, with a limitation of 600 characters per API request. The only way to pass this limitation is to register for an account, which isn’t free nor comes with a free trial.

Gingerit isn’t working. What can I do?

There are many reasons why Gingerit Python might not work for you. We’ve gone over two reasons and solutions in this article:

  • JSONDecodeError - Use the modified Python script instead of the PyPi package.
  • Character limit - You can’t pass in a string longer than 600 characters when using Gingerit from Python.

You might also run into weekly rate limits, but this shouldn’t happen right away.

I get a JSONDecodeError when using Gingerit - How to solve it?

It’s a really simple fix - Just use the modified Python script instead of the PyPi package. We’ve gone through how to modify the script to bypass Cloudflare’s anti-bot page earlier in the article, so refer to that section.


Conclusion

And that’s how easy it is to implement a Python grammar checker for free. We went through the process of configuring Python Gingerit, and a couple of examples of different lengths and complexities.

There’s some manual work and tweaks involved in getting past errors, but these are well worth it if you need a free grammar checker with explanations.

What are your thoughts on Gingerit? Do you use it as a free way to check if a sentence is grammatically correct in Python? Do you use some other alternative? Please let me know in the comment section below.

Stay connected