top of page

Tutorial: Flag Grammar and Spelling Errors

This article demonstrates how to use GrammarBot to identify flagged errors in an input text, by wrapping errors in double asterisks.  The input text "You is amazing" would be output as:

   You **is** amazing

If the output text is loaded into a Mardown viewer, this has the effect of bolding the flagged errors making them easy to review.  Let's get started.


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 ="",

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


'x-rapidapi-host': "",

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

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


j = r.json()


Marking Up 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


 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 are surrounded with two asterisks.  The flag_errors function below does exactly this (in Python):


def flag_errors(text):

  r ="",

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


   'x-rapidapi-host': "",

   '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:


    # build new_text from cursor to current offset

    new_text += text[cursor:offset]

    # next add **word**

    new_text += "**" + text[offset:(offset + length)] + "**"

    # 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


# let's try it!

flag_errors("We be smart. They be smart too. How doo you spel that wurd?")

# output: 'We **be** smart. They **be** smart too. How **doo** you **spel** that **wurd**?'

View or download this Gist:

bottom of page