Grammar checking for TeXmacs!

Check the LanguageTool plugin for TeXmacs at tm-forge. It can spot spelling, grammar, typography and style issues in many languages (autodetected).

The FOSS LanguageTool is certainly not as good as more recent AI-based text-proofing engines, but it’s already useful.

I have only tested the plugin on Linux, and it may need minor adjustments on other OSes.

6 Likes

Hey, this looks amazing.
I’ve been using LanguageTool for quite a while now (through the website), and I think that the tool is quite good.
I’ll give it a try and report back when I have some free time.

Thank you. This is amazing :slight_smile:

Dear @pjoyez,

thanks for this great tool. I’ve played around with it a bit and it works out of the box under macOS 14.7.6.

What I’ve noticed though, is that the plugin autodetects the language only once. If the base language for a document is set to, let’s say, German, then the entire document will be checked for spelling and grammar mistakes in German.

The plugin does not autodetect changes to the language environment made in a multilingual document. If you mark parts of the text in an English document as German, these part will not be checked for German mistakes.

TeXmacs spell utility allows for such changes within a document, and acts well behaved. So, I wonder if these issues are a feature of the plugin (that may be fixed) or a limitation of the (free) server at languagetool.org.

Cheers, Tilda

I did some testing on these two TeXmacs files:


and

LT-test_en

Both files contain numerous spelling mistakes in both the German and English parts of the text. Note that the base language of the first TeXmacs document is German, while the second uses US English.

  • Running TeXmacs’ spell checker on both files reveals some — though not all — of the errors. As expected, changes in language environments are correctly detected, and the spell checker generally suggests appropriate corrections for misspellings in either language.

  • I also copied and pasted both texts into LanguageTool via a browser. LanguageTool successfully detected spelling and grammar issues in both languages and, in fact, identified significantly more mistakes than the TeXmacs spell checker. In short, LanguageTool correctly autodetects language changes and handles multilingual documents well.

  • As mentioned in my previous post, this unfortunately does not apply to the LanguageTool plugin as it currently stands: it sticks to the selected base language of the document.

  • I also noticed that the plugin doesn’t start at the beginning of the file or selected text but instead works its way from the end of the file to the top. Why is that, @pjoyez? Could this be why it fails to detect the language codes (e.g., <german|neuer Text> and <english|some text>) and thus consistently fails to switch the language environment?

Anyway, thanks again for making this plugin available.

Cheers, Tilda

From Wikipedia, the free encyclopedia :

LanguageTool is a free and open-source grammar, style, and spell checker, and all its features are available for download. The LanguageTool website connects to a proprietary sister project called LanguageTool Premium (formerly LanguageTool Plus), which provides improved error detection for English and German, as well as easier revision of longer texts, following the open-core model.

Also, even on the in-browser checking on their website you’d get different results depending on whether you are using the free or paid service.

Regarding multilanguage texts, specifically:

-The default free API the plugin connects to assumes a single language for the whole submitted text, based on the first few words of the text. With the locally installed FOSS server, I get the same result. Your testing on the website reveals they use a different engine there, indeed.

-Anyway, the immediate workaround is to just check separately the parts of your text in a given language. If you don’t want to perform such subdivision manually, you can perhaps try their premium plan, and point the plugin to the corresponding server (some small changes would be required in the code for identifying with the server, though). Alternatively, one could modify the plugin to invoke LT repeatedly for each language used in the TeXmacs document…

LT checks the whole submitted text at once; it’s reviewing the issues found that proceeds backwards from the end. If you don’t like it, just jump to the first issue, pressing the “home” key or using the button in the widget.

Dear @pjoyez,

Thanks for the quick response and for the two workarounds regarding multilingual checking and ensuring the scan starts at the beginning of the text.

To enable LanguageTool Premium, I modified line 21 in init-languagetool.scm as follows:

(define-public LTserver "https://api.languagetoolplus.com")

As you noted, this alone isn’t sufficient — authentication is still missing.

What’s the recommended way to provide the username and API key to the plugin? I came across the following settings for vscode-ltex:

“ltex.languageToolHttpServerUri”: “https://api.languagetoolplus.com”,
“ltex.languageToolOrg.username”: “email”,
“ltex.languageToolOrg.apiKey”: “your_key”

Is there an equivalent way to set these credentials in init-languagetool.scm?

Any help would be greatly appreciated.
Cheers, Tilda

Yes, you need to modify the curl command line so that it incorporates your username and apiKey, with the appropriate syntax. For finding the required syntax, you can experiment with the form at https://languagetool.org/http-api/swagger-ui/#!/default/post_check and examine the command line it issues. After that, adjust the scheme code so that it issues properly the command with username and apiKey.

As you suggested, @pjoyez, I used the Swagger page and received a rather long curl command that included my username and API key. I ran that command in a zsh shell, and it appeared to work as expected.

Next, I tried to extract and integrate some relevant parts of that command into languagetool.scm. However, I’m not entirely sure I did it correctly:

(begin 
  (let* ((enc (uri-encode jsonstring))
           (tmpurl (url-temp))
           (data (string-append "data=" enc "&language=auto&preferredVariants=" (language-to-LTlocale spell-language) "&enabledOnly=false&motherTongue=" (language-to-LTlocale (get-output-language)) ))
           (void (string-save data tmpurl)) 
           (cmdl (list "curl"
                       "POST"
                       (string-append LTserver "/v2/check")              
                       "-H accept: application/json"
                       "-H Content-Type: application/x-www-form-urlencoded"
  				   "-H username=myemailaddress@provider.sth"
  				   "-H apiKey=xxx-XXXXXXXXXXXX"
  				   "-H language=auto"
  				   "-H motherTongue=de-DE"
  				   "-H preferredVariants=de-DE,en-US"
  				   "-H enabledOnly=false"
  				   "-H level=picky"
                       "--data-binary" (string-append "@" (url->system tmpurl) )
                             )))

The plugin still seems able to call LanguageTool, but I’m unsure whether it’s actually using the Premium version. I also don’t know whether I’ve inserted my additions in the right place or in the correct format.

Any suggestions or hints would be very much appreciated!

Cheers, Tilda

Thank you for this wonderful tool!

Is it possible that the plugin clashes with the traditional spell-checking tool? I have the spellchecker running regularly (see Spellcheck as-you-type) and it seems to change the suggestions of the languagetool plugin. For example, I tried with “You sincerely” and languagetool correctly suggest “Yours sincerely”, but when I accept the change, in the document I get “UK sincerely”.

In a clean TeXmacs profile without regular spellchecking the languagetool plugin works correctly.

Funny mixup! I had not anticipated that the plugin and the spellchecker could be running at the same time, and there seems to be some collision, indeed. I’ll check that and other possible interference. Thanks for bringing my attention to that.

3 Likes