Andras Timar
Beyond the spell checker
Collabora Online and Collabora Office now check sentences, not only words. A spell checker looks at one word at a time and asks whether it is in the dictionary; it is silent when every word is right but the sentence is wrong, as in “Il sont parti” or “the the”. A sentence checker reads the sentence, and sometimes the paragraph, and points out agreement errors, duplicated words, wrong punctuation and matters of style, with a suggestion where it has one. Both underline as you type, and the context menu on an underlined passage says what the checker found and offers the correction.
This post covers the two kinds of sentence checker that ship. Lightproof runs inside the engine and needs no server. The remote checkers, LanguageTool, the Duden Korrekturserver and Grammalecte, send the text to a service you run yourself or subscribe to. The last two sections show how to set each up in Collabora Online and in Collabora Office.
Lightproof, the built-in sentence checker
Lightproof is a rule-based sentence checker. Each rule is a regular expression over the paragraph, a replacement, a message, and an optional condition that may ask the spelling dictionary or the morphology about the words it matched. László Németh wrote it as a Python framework, and for years the rule packages for English, Hungarian, Brazilian Portuguese and Russian shipped inside the dictionary extensions as Python code, run through the office suite’s Python bridge.
Why it had to be rewritten
The Python version could not run in our products. Collabora Online and Collabora Office do not ship the Python loader by default; it sits behind an opt-in build argument, so the rule data was installed and could never be loaded. Python macros remain an option for those who build with them, but grammar checking could not depend on them.
The rewrite also took a Python interpreter and its global lock off the grammar checking thread, and it exposed a latent deadlock in the old design: the English unit conversion went through Calc’s function service, which takes the main thread’s lock, while proofreading runs on another thread that the main thread may be waiting for.
How the C++ engine works
The rules were not translated by hand. At build time a compiler, lpcompile.py, reads the Python rule tables and writes a binary rule file per language: the patterns translated to ICU’s regular expression syntax, the conditions lowered through Python’s own ast module to bytecode, the option list with its defaults, and the licence notice. The runtime is three pieces: a reader that memory-maps the rule file, so every kit checking the same language shares one copy; a small stack machine that runs the conditions; and the proofreader that Writer talks to. The host functions the rules call, spell(), morph(), stem(), generate(), suggest(), affix() and measurement(), are answered by the engine’s own spelling and morphology services and a native unit table, with no Calc and no Python involved.
Translating regular expressions from Python to ICU is not free. ICU gives “[“, “&”, “{” and a leading “:” meanings inside a character class that Python does not, and a pattern that keeps one is rejected at run time, silently taking its rule with it. The compiler escapes them and fails the build if one survives; fourteen patterns across the four packages needed this.
Brazilian Portuguese is by far the largest package, with 11,875 rules, and running every pattern over every paragraph was not affordable. Each rule now carries the most selective literal its pattern must contain; a paragraph is indexed once into a bit per three-character sequence, and a rule whose literal is absent is dismissed before its pattern is even built. On a debug build that took one Portuguese paragraph from 87 ms to 35 ms. English, with about seventy rules, costs under a fifth of a millisecond either way.
The upstream Lightproof repository is gone, so the rule sources and the generator that turns them into rule tables were recovered from a Software Heritage archive and brought into the tree, with the fixes needed to run on a current Python. That made the rules maintainable again, and it surfaced 172 Brazilian Portuguese rules that had never fired for anyone, in Python or otherwise, because they named undefined word lists or were not valid code; they have been repaired. A tool builds a demonstration document, a chapter per language and a row per rule with an example and the expected suggestion, 613 examples in all, so a native speaker can check the rules by hand.
Languages
Lightproof ships rules for English in all its variants, Hungarian, Brazilian Portuguese and Russian. The rule files live in share/lightproof and are installed whether or not the bundled dictionaries are, so builds configured without them, such as the flatpak and the snap, still check sentences. The checker is found through its component registration and answers which languages it serves from the rule files it finds; adding a language is adding a rule file.
Options
Each package has its own switches, one per rule category, and they are the user’s to set in the Options dialog, under a panel named after the language. English has 17: possible mistakes, capitalization, word duplication, parentheses, word spacing, em and en dashes, quotation marks, the multiplication and minus signs, apostrophes, ellipses, thousand separators, and conversion to or from metric units. Hungarian has 28, among them compound words, comma usage, consistency of money amounts, ligatures and thin spaces. Brazilian Portuguese has 26, including pleonasms, gerundisms, cacophonous sounds, paronyms and the confusions of “mal” with “mau” and “há” with “a”. Russian has 11, from comma usage and abbreviations to typography and quotation marks.
The option titles appear in the package’s own language and are not translated: an option names a notion of that language’s grammar, and whoever has a reason to set it is reading that language already. Only the panel titles, the names of the languages, are translated.
The values live in the Linguistic configuration under GrammarChecking/SentenceChecking and arrive in three tiers: the schema defaults, an administrator’s preset, and the per-user settings the dialog writes. A change takes effect at once. The checker rereads its flags and asks the open documents to check again, with no reload. In the context menu on an underlined passage the message row carries a Lightproof icon, so you can see which checker spoke.
Remote sentence checkers
Three checkers run on a server and get the text over HTTP: LanguageTool, the Duden Korrekturserver and Grammalecte. They share one implementation in the engine, the HTTP round trip, the JSON parsing, a cache of recent answers and the proofreader scaffolding, and each adds only its request, its response parser and its language list. A paragraph goes to the server in one request. A server that does not answer is left alone for thirty seconds and logged once, so a service that is down costs no more than one failed request per half minute.
|
Checker |
What it is |
Licence |
Languages |
Commercial options |
Account |
|
LanguageTool |
Grammar, style and spelling checker in Java |
LGPL 2.1 |
English, German, French, Spanish, Portuguese, Dutch, Russian, Ukrainian and others, about thirty in all |
LanguageTool Premium, hosted at languagetool.org, with rules the open-source version lacks |
Username and API key for Premium; none for a self-hosted server |
|
Duden Korrekturserver |
German spelling, grammar and style checker on the Duden dictionaries and rules |
Proprietary, from EPC |
German in its German, Austrian and Swiss standards; English, French, Italian and Spanish spelling where the server has dictionaries for them |
Licensed server, on premises or hosted; terms from EPC |
API token from the server’s accounts page |
|
Grammalecte |
French grammar checker in Python |
GPL 3 |
French |
None; free software, run it yourself |
None |
The text of the document leaves the machine, which shapes how the checkers are configured. In Collabora Online one process checks the document for everyone who has it open, so the server, account and key of a checker are the administrator’s, set in coolwsd.xml; a user may change them only where the administrator allows it, and never for a checker the administrator switched off, since that switch keeps the text in house. What a checker looks for, the Duden style checks and the Grammalecte rule families, is always the user’s own choice. Collabora Office has no administrator, and its Options dialog shows everything.
A language that more than one enabled checker serves goes to the first in the order Grammalecte, Duden, LanguageTool: French to Grammalecte, German to Duden, the rest to LanguageTool. The checkers that spell check as well, LanguageTool and the Duden Korrekturserver, take their languages over from the local spell checker once their server has answered, so a misspelling is not underlined twice. Until it answers, and if it never does, the local spell checker keeps the language. Each checker has its own icon on the message row of the context menu.
LanguageTool
LanguageTool is the best-known open-source grammar checker, started by Daniel Naber in 2003 and developed since by LanguageTooler GmbH and a community of rule authors. It checks grammar, style and spelling in more than thirty languages and runs as a Java server with an HTTP API. The open-source server is under the LGPL 2.1. The company sells LanguageTool Premium, a hosted service with additional rules and higher limits, reached through the same API with an account name and an API key.
LanguageTool stopped publishing a binary release archive after version 6.6, so Collabora builds the collabora/languagetool image on Docker Hub from the release tag of the source. It is free to use, but it is the open-source LanguageTool and it is unsupported.
The settings are enabled, base_url, user_name, api_key, ssl_verification and allow_user_settings. The base URL ends in /v2, the version of the API, and the checker appends /check and /languages; for Premium it is https://api.languagetoolplus.com/v2 with the account’s name and key. A configuration that drove the Duden Korrekturserver through this block, with rest_protocol set to duden, still works and logs a deprecation: its values go to the Duden checker.
Duden Korrekturserver
The Duden Korrekturserver is the server edition of the Duden proofreading technology, developed and sold by EPC Consulting und Software GmbH. It checks German spelling, grammar and style against the Duden dictionaries, in the three national standards, and lets you choose the orthography it enforces: the Duden recommendation, conservative, progressive, tolerant, or the press agencies’ rules. A server with dictionaries installed also spell checks English, French, Italian and Spanish. It is proprietary software, licensed per server for on-premises or hosted use; terms come from EPC.
The server settings are enabled, base_url, api_key, ssl_verification, spellchecking_level, orthography_standard, property_sets, dictionaries and allow_user_settings. The base URL is the server’s API root, ending in /api, and the token comes from the server’s user accounts page. The checking level runs from 0, none, through single words and spelling to 3, spelling and grammar. Correction profiles and supplementary dictionaries are chosen by name; the dialog lists the ones the server publishes.
The user chooses how much the server explains, a short message, a long one, or a long one with an example, and the eight style checks, which the server runs for German: foreign words with a German equivalent, archaic words, regional words, colloquial and vulgar words, filler words, offensive wording, gender-inclusive wording and overly complex sentences. The server’s explanation of each error code appears on the message row, in the language of the user interface.
Grammalecte
Grammalecte is a grammar checker for French by Olivier R., published under the GPL 3 as free software, with no paid edition. It is rule-based and written in Python, and it has an HTTP server, which Collabora Online talks to. A recipe under docker/grammalecte in the source tree builds an image from the Grammalecte release archive, checking its hash, and runs the server as an unprivileged user; nothing of Grammalecte is in our repository.
The settings are enabled, base_url, ssl_verification and allow_user_settings. The base URL is the server’s root, and the checker appends /gc_text/fr. The server has no authentication and no TLS of its own, so it belongs on an internal network.
What the checker looks for is the user’s, in 35 rule families grouped the way the server groups them: typography, nouns and adjectives, verbs, style, and miscellany. The defaults are those of Grammalecte’s own writing profile. Grammalecte counts in code points from the start of each line, and the checker maps its answers back to positions in the paragraph, so an emoji before an error does not shift the underline. Spelling is left to the local dictionary: Grammalecte reports misspellings but offers no suggestions for them.
Setting up Collabora Online
Lightproof needs no setup: it ships in the engine and checks English, Hungarian, Brazilian Portuguese and Russian text as soon as a document opens. Each remote checker has a block of its own in coolwsd.xml, off by default, and is switched on by setting enabled to true and base_url to the server:
<languagetool>
<enabled>true</enabled>
<base_url>http://languagetool:8010/v2</base_url>
<user_name></user_name>
<api_key></api_key>
</languagetool>
<duden>
<enabled>true</enabled>
<base_url>https://duden.example.com/api</base_url>
<api_key>the token from the server's accounts page</api_key>
<spellchecking_level>3</spellchecking_level>
<orthography_standard>duden</orthography_standard>
</duden>
<grammalecte>
<enabled>true</enabled>
<base_url>http://grammalecte:8080</base_url>
</grammalecte>Every key can also be given on the command line as an override, for instance –o:grammalecte.enabled=true, which is how the docker image takes them through extra_params. The API keys are redacted from the configuration the server logs.
Two recipes in the source tree run a checker next to Collabora Online. docker/languagetool has a compose file that starts the collabora/languagetool image beside collabora/code on an internal network; the server wants about half a gigabyte of memory, more with the optional n-gram data that finds confused words, and both are set through environment variables described in its README. docker/grammalecte builds the Grammalecte image and starts it the same way. In both, the checker’s network is internal, because neither service has authentication of its own.
allow_user_settings, off by default in every block, lets users change the server and account of a checker in their own settings. It is off because the document is checked by one process for everyone who has it open, so a user who points a checker at a server of their choosing sends everyone’s text there. It has no effect while the checker is off: a checker the administrator switched off is not offered to users at all.
allowed_languages, the list of languages whose writing aids are loaded at start, does not come into this. It limits the local aids, the dictionaries, hyphenation, thesaurus and Lightproof, and a remote checker checks every language it is configured for whether or not that list names it.
Nothing waits for a server. The checkers are configured when the server starts, but their servers are first asked when a document loads, on a thread of their own, and a checker that spell checks takes its languages over from the local dictionary only once its server has answered. A server that is slow or down costs no startup time and no document load; it is asked again at the next document load or settings change, after half a minute.
Users find their part in the Options dialog. The sentence checker section has a panel per Lightproof language with its switches. The remote checkers section shows, for each checker the administrator switched on, what the user may choose: the Duden style checks and the explanation level, the Grammalecte rule families, and, where allow_user_settings is on, the server, account and key as well.
Setting up Collabora Office
Collabora Office, the desktop application, has the same checkers and the same dialog, with nobody in between: there is no server configuration, and every setting is the user’s. Lightproof is on from the first start. The remote checkers start off, and nothing leaves the machine until one is switched on.
In the Options dialog the remote checkers section shows a panel per checker. Switching a checker on opens its settings: the server URL, for LanguageTool the username and API key of a Premium account, for the Duden Korrekturserver the API token, and whether to verify the server’s certificate. The Duden panel adds the checking level, the German spelling standard, how much the server explains, the correction profiles and supplementary dictionaries the server offers, and the eight style checks. The Grammalecte panel lists its 35 rule families by group, in French, with Grammalecte’s own writing defaults.
A checker takes effect when it is switched on; there is nothing to restart. The engine starts asking it about the languages it serves, and a checker that spell checks, LanguageTool or Duden, takes those languages over from the bundled dictionary once its server has answered, and gives them back when it is switched off again. A profile that drove the Duden Korrekturserver through the LanguageTool settings, which was the only way before, has its server moved to the Duden checker on the first start.
In the document the checkers look alike: an underline under the passage, and on right-click a context menu whose first row is the checker’s message with the checker’s own icon, Lightproof’s and LanguageTool’s letters over a wave, Duden’s D over its red rule or Grammalecte’s nested diamonds, followed by the suggestions, Ignore, and Ignore All for that rule.
Try it
The quickest way to see a remote checker at work is docker compose up in docker/grammalecte or docker/languagetool of the Collabora Online source tree, then a Writer document in French or English with a mistake in it. Lightproof needs nothing at all: open a document in any of its four languages and type “the the”.
The code is in the engine under lingucomponent: the Lightproof checker and its compiler in spellcheck/lightproof, with a README that describes the rule language, how to change a rule and how to add a language; the shared remote checker code in lingutil, and the three checkers beside it. Rule fixes, new Lightproof languages and reports of what the checkers get wrong are welcome in the usual places.
Lightproof is available in Collabora Online 26.04.5 and the external sentence checkers are available in Collabora Online 26.04.6.