Skip to content

Instantly share code, notes, and snippets.

@oasic
Last active August 31, 2021 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oasic/2c53ab7adf3d0d67b24d18a9a0e3bbf0 to your computer and use it in GitHub Desktop.
Save oasic/2c53ab7adf3d0d67b24d18a9a0e3bbf0 to your computer and use it in GitHub Desktop.
Automatic Grammar and Spelling Correction using GrammarBot
# tutorial here: https://www.grammarbot.io/grammar-autocorrection
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.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment