Automated Correction of Grammar and Spelling

This article demonstrates how to use GrammarBot to automatically correct text.  We receive a lot of mail indicating this is a common use case, so we wanted to provide a tutorial on how to accomplish this.  With that said, we caution this approach and recommend that a "human in the loop" approach be used in most scenarios instead, because the precision of errors and the suggested corrections are not always at the level of precision required.  But you be the judge. Let's dive in!

 

Calling the API

You can use one of the ready-made open-source grammar bot clients on the quickstart page, but it may actually be less work to just use your own.  In Python, we can use the popular requests library which makes it easy to call the GrammarBot API and get the JSON response in just a few lines of code without any additional dependencies.  

 

import requests

 

text = "We be smart. They be smart too."

  r = requests.post("https://grammarbot.p.rapidapi.com/check",

   data = {'text': text, 'language': 'en-US'},

   headers={

   'x-rapidapi-host': "grammarbot.p.rapidapi.com",

   'x-rapidapi-key': "your_rapid_api_key_2a1c27dp1af3afjsn67a3e57142d6",

   'content-type': "application/x-www-form-urlencoded"

  })

j = r.json()


 

Automatically Correcting the Input

 

If there are errors detected in the text, the JSON object returned in our request above will contain an array of "matches" which we can iterate over.  Each match contains the following fields (and others as well):

 

  • offset - the start index in the original text for the potential error

  • length - the length in characters of the potential error

  • replacements - the list of possible corrections


 

 View the JSON output to see what data is at your disposal (see example screenshot).


 

At a high level, what we want to do is map our input text into a new output text in which any matches have been replaced by the first item in the replacements list.  The auto_correct_text function below does exactly this (in Python):


 

import requests

 

def auto_correct_text(text):

  r = requests.post("https://grammarbot.p.rapidapi.com/check",

   data = {'text': text, 'language': 'en-US'},

   headers={

   'x-rapidapi-host': "grammarbot.p.rapidapi.com",

   'x-rapidapi-key': "your_rapid_api_key_2a1c27dp1af3afjsn67a3e57142d6",

   'content-type': "application/x-www-form-urlencoded"

  })

 j = r.json()

 new_text = ''

 cursor = 0

 for match in j["matches"]:

     offset = match["offset"]

     length = match["length"]

     if cursor > offset:

         continue

     # build new_text from cursor to current offset

     new_text += text[cursor:offset]

     # next add first replacement

     repls = match["replacements"]

     if repls and len(repls) > 0:

         new_text += repls[0]["value"]

     # update cursor

     cursor = offset + length

    

 # if cursor < text length, then add remaining text to new_text

 if cursor < len(text):

     new_text += text[cursor:]

 

 return new_text

 

auto_correct_text("We be smart. They be smart too.")

# output: 'We are smart. They are smart too."
 

View or download this Gist: https://gist.github.com/oasic/2c53ab7adf3d0d67b24d18a9a0e3bbf0

© 2020 by GrammarBot