So a while ago, I was struggling with making bulk AI content cheaply and making it at least human readable. I coded up A LOT of different basic grammar checking tools, spell checking, did some NLTK stuff that just made everything incredibly hard, and in general it was just a mess.
That is, until I discovered LanguageTool, a free & open source grammar checker. It's basically like Grammarly, but free.
But I didn't want just Grammarly, I wanted to automate Grammarly. That's where I discovered that you can host your own LanguageTool server locally, and find errors in your grammar using an API. It's super easy to use once you figure out how to set the server up. Sometimes it gets stuck on itself if you try to send it a whole article at once, but for the most part, it works like a charm. Here's some Python code that I wrote to fix spelling in text:
That is, until I discovered LanguageTool, a free & open source grammar checker. It's basically like Grammarly, but free.
But I didn't want just Grammarly, I wanted to automate Grammarly. That's where I discovered that you can host your own LanguageTool server locally, and find errors in your grammar using an API. It's super easy to use once you figure out how to set the server up. Sometimes it gets stuck on itself if you try to send it a whole article at once, but for the most part, it works like a charm. Here's some Python code that I wrote to fix spelling in text:
import requests
import json
import threading
thread_local = threading.local()
def get_session():
if not hasattr(thread_local, "session"):
thread_local.session = requests.Session()
return thread_local.session
url = 'http://localhost:8081/v2/check'
def check_spelling(text):
if not text: return {}
data = {'language': 'en-US', 'text': text}
SESSION = get_session()
response = SESSION.post(url, data=data)
if not response: return text
return response.json()['matches']
def fix_spelling(text, matchStart=0):
if text == '': return ''
matches = check_spelling(text)
matches.sort(key=lambda x: x['offset'])
if len(matches) > matchStart:
offset = matches[matchStart]['offset']
if len(matches[matchStart]['replacements']) > 0:
replacements = matches[matchStart]['replacements'][0]['value']
length = matches[matchStart]['length']
text = text[ffset] + replacements + text[offset + length:]
else:
matchStart += 1
text = fix_spelling(text, matchStart)
return text
I don't know how much use this will be to anyone out there, but when I was using it, it worked great! I have since moved onto producing higher quality AI content, but I didn't want this code to go to waste, so here y'all go! This will definitely help those out there that are using spun content or cheap AI content that has a lot of spelling errors in it.if __name__ == '__main__':
print(fix_spelling("this is a simpel test with an mispelled errorz."))