We sometimes get requests asking if there is a way to upload a list of words to ignore, so we wanted to demonstrate how easy it is to do this within your application. This article demonstrates how to use a custom spelling dictionary to filter out or ignore words flagged by Grammar Bot as incorrectly spelled.
This tutorial extends the previous one on flagging errors, so you will want to review that first here:
https://www.grammarbot.io/flag-grammar-spelling-errors
The changes to support the addition of filtering words by a dictionary are indicated by a comment with double angles («):
import requests
def flag_errors(text, custom_dictionary=None): # «
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_a1c27dp1af3afjsn67a3e57142d6",
'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
error_text = text[offset:(offset + length)] # «
if custom_dictionary and match["rule"]["id"] == "MORFOLOGIK_RULE_EN_US": # «
if error_text in custom_dictionary: # «
continue # «
# build new_text from cursor to current offset
new_text += text[cursor:offset]
# next add **word**
new_text += "**" + error_text + "**"
# 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
Test Run
So let’s give it a try! The first line below runs some text filled with errors through our function and there is no custom dictionary. Notice that every error has been flagged. But, in the second line, we add a custom dictionary to filter out “wurd” and “spel”. It really is that simple. :)
print(flag_errors("We be smart. How doo you spel that wurd?"))
print(flag_errors("We be smart. How doo you spel that wurd?", {"wurd": True, "spel": True}))
Output:
We **be** smart. How **doo** you **spel** that **wurd**?
We **be** smart. How **doo** you spel that wurd?
View or download this Gist: https://gist.github.com/oasic/31435774d7647b98368760a1803a540d