Free, Open-Source & Automatable Grammar Checking Tool (LanguageTools)

haxel

Junior Member
Joined
May 24, 2022
Messages
100
Reaction score
80
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:

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[:eek:ffset] + replacements + text[offset + length:]
else:
matchStart += 1
text = fix_spelling(text, matchStart)
return text
if __name__ == '__main__':
print(fix_spelling("this is a simpel test with an mispelled errorz."))
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.
 
Thank you, I was searching for something open source for grammar.
 
Hi @Axel can you instruct me how host or run(i guess I don't the term) the code in a local server? just give an overall idea, if it's okay with you
then I will research more, I love this Idea very much.
 
Hi @Axel can you instruct me how host or run(i guess I don't the term) the code in a local server? just give an overall idea, if it's okay with you
then I will research more, I love this Idea very much.
I run a .bat script to open the server, mainly because to run the server you have to open it from command prompt.
cd C
cd Projects\LanguageTools\LanguageTool-5.7-stable
java -cp languagetool-server.jar org.languagetool.server.HTTPServer --port 8081 --allow-origin
pause
That's the script I use. As you can see, the server is located in my Projects folder, and since I downloaded the script from Github, I have to navigate to the stable version of the script from within my projects folder. There's more than likely a better way to run the server that's more secure, but for my purposes, this works. You need to include pause at the end of the script otherwise the server just opens, then closes.

From there, you can access the server via requests, as you can see in the python script I shared above.
 
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features and essential functions on BlackHatWorld and other forums. These functions are unrelated to ads, such as internal links and images. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock