Sonnet
Sonnet, in sonic-frameworks-spell-check (find_package(KF6Sonnet); KF6::SonnetCore and KF6::SonnetUi), solves spell checking for text editors. Its spell checkers are plugins: Hunspell and Aspell on SonicDE, with plugins for HSpell, Voikko and the Windows and macOS system spell checkers in the source as well. It also detects the language of a text automatically, combining several methods.
Spell checking in your QTextEdit
Adding Sonnet to a QTextEdit is easy:
QTextEdit *textEdit = new QTextEdit;
textEdit->setText(
QString::fromLatin1("This is a sample buffer. Whih this thingg will "
"be checkin for misstakes. Whih, Enviroment, govermant. Whih."));
Sonnet::SpellCheckDecorator *installer = new Sonnet::SpellCheckDecorator(textEdit);
installer->highlighter()->setCurrentLanguage(QStringLiteral("en_US"));
Sonnet::SpellCheckDecorator can be extended to check differently formatted text, such as emails:
class MailSpellCheckDecorator : public Sonnet::SpellCheckDecorator
{
public:
explicit MailSpellCheckDecorator(QTextEdit *edit)
: Sonnet::SpellCheckDecorator(edit)
{
}
protected:
bool isSpellCheckingEnabledForBlock(const QString &blockText) const override
{
qDebug() << blockText;
return !blockText.startsWith(QLatin1Char('>'));
}
};
Use MailSpellCheckDecorator exactly like SpellCheckDecorator; it also skips the quoted parts of an email (lines starting with >).
Language detection
Sonnet can tell about 70 languages apart in a string. Its method is based on Languid, a Perl script by Maciej Ceglowski, and works in two steps: first it checks which writing systems the text uses, then, for the languages using those writing systems, it compares an n-gram frequency model of each language with one of the text. The most similar language model wins. If no language is found, the result is an empty string.
A simple example with GuessLanguage:
GuessLanguage languageGuesser;
QString lang = languageGuesser.identify("Mon super texte");
identify() returns the language's ISO 639-1 code (a three-letter code only where no two-letter code exists).
Widgets
Sonnet also has widgets for configuring it in Qt applications. DictionaryComboBox, for example, is a combo box for choosing the dictionary. Connect it to the highlighter of a decorator to switch the spell check language:
Sonnet::DictionaryComboBox *comboBox = new Sonnet::DictionaryComboBox;
QObject::connect(comboBox, &Sonnet::DictionaryComboBox::dictionaryChanged,
installer->highlighter(), &Sonnet::Highlighter::setCurrentLanguage);
Sonnet::Dialog checks a whole text interactively:
void TestDialog::check(const QString &buffer)
{
Sonnet::Dialog *dlg = new Sonnet::Dialog(new BackgroundChecker(this), nullptr);
connect(dlg, &Dialog::spellCheckDone, this, &TestDialog::doneChecking);
dlg->setBuffer(buffer);
dlg->show();
}
ConfigDialog is a fuller settings dialog: the words to ignore, skipping run-together words, and turning automatic language detection on or off.
The examples come from sonic-frameworks-spell-check/examples (LGPL-2.1-or-later).