Using Node.js and LnaguageTool to correct texts

How to make a spell checker for our projects

Johan
Johan
5 min read

We will explain how to make a spell checker using the motor LibreOffice.

As you already know, LibreOffice is the free software alternative for Microsoft Office, this great text editor brings with it a powerful spell checker that not only looks if a word is misspelled, but also conjugations and many more complex skills.

Well, we are going to use it in our project so that our editors when they write do not commit so many spelling mistakes and the system will detect it and may warn them if they have not noticed and have made some simple or complex spelling mistake.

I'll show you how to do it with Node.js but it could really be done with any language you want, because practically all the processing is going to be done by the LibreOffice engine that is called LanguageTool, which is also free or therefore we can use in our projects as we please.

You can use your API or use your own server, I'll explain how to do it and mount it on your own server so you do not have to depend on the LanguageTool API that has some limitations in your free layer.

Installation of the text correction server

The first step we are going to take is the installation of the text correction server to be able to make the requests to this and to respond with the result.

Download the version of LanguageTool «Desktop» to use without connection (occupies> 170MB) and uncompress it.

wget https://languagetool.org/download/LanguageTool-stable.zip
unzip LanguageTool-stable.zip

Well we download it and unzip it as you see in the previous command, we will need to have JAVA installed on our server in order to run the program since it is written in this language. Nothing worrying is very easy to do, in the case of Ubuntu 18.04 and surely the rest we just have to execute the next command.

apt install default-jre

We installed the virtual machine, and we can run our own spelling corrections server, we will proceed to run the server.

java -cp languagetool-server.jar org.languagetool.server.HTTPServer --port 8081 --allow-origin '*';

Now you can test the spelling server by making a request to the following URL: curl --data "language=en&text=simple testo with eror" http://localhost: 8081/v2 /check

This request will return a response in the format JSON which is what we will use for our corrector.

{'software': {
 'name': 'LanguageTool',
 'version': '4.6-SNAPSHOT', 'buildDate ''2019-05-22 18: 47',
 'apiVersion': 1,
 'premium': false,
 'premiumHint': 'You might be missing errors only the Premium version can find. Contact us at support & lt; at & gt; languagetoolplus.com.',
 'status': '
   },
 'warnings': {
 'incompleteResults': false
   },
 'language': {
 'name': 'English (US)',
 'code': 'en-US',
 'detectedLanguage': {
 'name': 'English (US)',
 'code': 'en-US',
 'confidence': 0.6561856
     }
   },
 'matches': [{
 'message': 'This sentence does not start with an uppercase letter',
 'shortMessage':',
 'replacements': [{
 'value': 'My '
     }],
 'offset': 0,
 'length': 2,
 'context': {
 'text': 'my text',
 'offset': 0, 'length': 2
     },
 'sentence': 'my text',
 'type': {'typeName': 'Other '},
 'rule': {
 'id': 'UPPERCASE_SENTENCE_START',
 'description': 'Checks that a sentence starts with an uppercase letter',
 'issueType': 'typographical',
 'category': {
 'id': 'CASING',
 'name': 'Capitalization '
       }
     },
 'ignoreForIncompleteSentence': true,
 'contextForSureMatch': - 1
     }]
   }

Well as we see almost everything we do this this great software that is also free, a marvel, hey!

Now we are going to integrate this answer in our website / service that we have, in this case written with Node.

import request from 'request'; // if we do not have the package installed we do it;)

const result = new Promise ((resolve, reject) = => {
 request ({
   method: 'GET',
   url: 'http://localhost:8081/v2/check',
   qs: {
     disabledRules: 'WHITESPACE_RULE,FRENCH_WHITESPACE',
     allowIncompleteResults: true,
     enableHiddenRules: true,
     enabledOnly: false,
     language: post.locale,
     text: 'Text to check'
   },
   json: true
 },
 (error, response, body) = => {
   if (error) {
    reject (error);
   } else {
    resolve (body);
   }
  });
});

Well it's that simple, as you can see it can be done in any language easily because all the work is done by the LanguageTool, although this example is quite simple and it's up to you to exploit it and complicate it more to do exactly what you want, the idea is to make or show the writer these errors so you can solve them, as you see also LanguageTool returns a text in the language you are dealing with the advice and the possibilities of replacement as well as suggestions for depending on the error that comes up.

In WebMediums we are using this system so that our writers do not mess without realizing it, something that will facilitate the work and the fluidity without having to be so aware of the errors and delegate this task to the software that will do it instantly.

This is how we have implemented it:

How to make a spell checker for our projects – Free Code – WebMediums

Responses