hunspell clib

Hunspell spell checker for PascalAI — the same engine used by Firefox, LibreOffice, Chrome, and macOS. Check spelling, generate correction suggestions, perform morphological analysis and stemming in 100+ languages.

Version1.0.0
Typeclib
CategoryText Processing
System reqlibhunspell-dev + dictionaries
CLib package — Ubuntu/Debian: sudo apt install libhunspell-dev
ppm install hunspell

Quick Start

Basic spell check

uses hunspell;

var spell := NewSpellerLang('en_US');

{ Check individual words }
WriteLn(BoolToStr(Check(spell, 'hello')));    { True }
WriteLn(BoolToStr(Check(spell, 'helo')));     { False }

{ Get suggestions }
var suggestions := Suggest(spell, 'helo', 5);
for var s in suggestions do
  WriteLn('  → ' + s);

FreeSpeller(spell);

Check a whole document

uses hunspell;

var spell := NewSpellerLang('en_US');

{ Add domain-specific words }
AddWord(spell, 'PascalAI');
AddWord(spell, 'HEIF');

var result := CheckText(spell, myDocument);
WriteLn('Words: '  + IntToStr(result.WordCount));
WriteLn('Errors: ' + IntToStr(result.ErrorCount));

for var w in result.Words do
  if not w.IsCorrect then
  begin
    WriteLn('Misspelled: ' + w.Word);
    if Length(w.Suggestions) > 0 then
      WriteLn('  Suggest: ' + w.Suggestions[0]);
  end;

FreeSpeller(spell);

Lifecycle API

FunctionSignatureDescription
NewSpellerNewSpeller(affPath, dicPath: string): TSpellerCreate a speller from explicit .aff and .dic file paths. Use this when your dictionary files are in a non-standard location.
NewSpellerLangNewSpellerLang(lang: string): TSpellerCreate a speller using a language code (e.g. 'en_US'). Looks up the dictionary in standard system paths (/usr/share/hunspell/).
FreeSpellerFreeSpeller(spell: TSpeller)Release all memory held by the speller instance. Always call when done.
AddDictionaryAddDictionary(spell: TSpeller; dicPath: string)Load an additional .dic file into an existing speller (e.g., a technical domain dictionary on top of the base language).

Spell Check API

Function / FieldType / SignatureDescription
CheckCheck(spell: TSpeller; word: string): BooleanReturn True if the word is correctly spelled according to the loaded dictionaries.
CheckTextCheckText(spell: TSpeller; text: string): TCheckResultTokenize text and check every word. Returns a TCheckResult with per-word detail.
TCheckResult.WordCountIntegerTotal number of words found in the input text.
TCheckResult.ErrorCountIntegerNumber of words that failed spell check.
TCheckResult.Wordsarray of TWordResultPer-word results. Iterate to find misspellings and their suggestions.
TWordResult.WordstringThe original word token.
TWordResult.IsCorrectBooleanTrue if the word passed spell check.
TWordResult.Suggestionsarray of stringUp to 5 correction suggestions for misspelled words.
TWordResult.OffsetIntegerByte offset of the word in the original input string.

Suggestions API

FunctionSignatureDescription
SuggestSuggest(spell: TSpeller; word: string; maxCount: Integer): array of stringReturn up to maxCount correction suggestions for the given word, ordered by likelihood.
SuggestScoredSuggestScored(spell: TSpeller; word: string; maxCount: Integer): array of TScoredWordReturn suggestions with an edit-distance score. TScoredWord has fields Word (string) and Score (Integer, lower is closer).

Custom Words API

Session vs persistent: AddWord and RemoveWord affect the in-memory speller instance only. Changes are not written back to the .dic file. For persistent custom dictionaries, maintain a separate .dic file and load it with AddDictionary.
FunctionSignatureDescription
AddWordAddWord(spell: TSpeller; word: string)Add a word to the speller's in-session dictionary. Subsequent Check calls will accept it as correct.
AddWordWithExampleAddWordWithExample(spell: TSpeller; word, example: string)Add a word with a morphological example that helps Hunspell derive compound and inflected forms.
RemoveWordRemoveWord(spell: TSpeller; word: string)Remove a word from the session dictionary (useful for temporarily blacklisting a term).

Morphology API

FunctionSignatureDescription
StemStem(spell: TSpeller; word: string): array of stringReturn the base stem(s) of the word. "running" → ["run"]. Useful for search indexing and text normalization.
AnalyzeAnalyze(spell: TSpeller; word: string): array of stringReturn morphological analysis tags for the word (e.g. part of speech, tense, number). Format depends on the dictionary's .aff file.
GenerateGenerate(spell: TSpeller; word, example: string): array of stringGenerate inflected or derived forms of word by analogy with example. Useful for NLP data augmentation.

Utility API

FunctionSignatureDescription
FindErrorsFindErrors(spell: TSpeller; text: string): array of TWordResultShorthand for CheckText that returns only the misspelled words, skipping correctly spelled ones.
AutoCorrectAutoCorrect(spell: TSpeller; text: string): stringReplace each misspelled word with the top suggestion (if one exists). Returns the corrected text.
ListAvailableLangsListAvailableLangs(): array of stringScan system Hunspell directories and return a list of installed language codes (e.g. ["en_US", "fr_FR", "de_DE"]).

Dictionary Installation

Install language dictionaries via apt before calling NewSpellerLang. Dictionary packages install .aff and .dic files to /usr/share/hunspell/.

en_US
sudo apt install hunspell-en-us
es (Spanish)
sudo apt install hunspell-es
fr (French)
sudo apt install hunspell-fr
de (German)
sudo apt install hunspell-de-de
pt_BR (Portuguese)
sudo apt install hunspell-pt-br
ru (Russian)
sudo apt install hunspell-ru
it (Italian)
sudo apt install hunspell-it
nl (Dutch)
sudo apt install hunspell-nl
pl (Polish)
sudo apt install hunspell-pl
Tip: apt search hunspell- lists all available language packs. Over 100 languages are available in standard Debian/Ubuntu repositories.

Key Concepts

.aff / .dic file pairs

Every Hunspell dictionary consists of two files: a .dic file (the word list) and a .aff file (affix rules). The affix rules describe how stems are combined with prefixes and suffixes to form valid words. Both files must be present for the speller to work. NewSpellerLang('en_US') locates en_US.aff and en_US.dic automatically.

Session vs persistent dictionary

AddWord inserts a word only for the lifetime of the TSpeller instance. To persist custom words across runs, maintain your own supplementary .dic file (one word per line, first line = word count) and load it with AddDictionary on startup.

Morphological analysis for NLP

Analyze returns part-of-speech tags and grammatical attributes encoded in the dictionary's flag system. The exact output format varies by language pack. This is useful for building POS taggers, grammar checkers, or linguistic feature extractors without a separate NLP library.

Stemming for search indexing

Stem reduces inflected forms to their dictionary base. Indexing stems instead of raw words means a query for "running" also matches documents containing "run", "runs", and "ran". Hunspell stemming is language-aware and more accurate than generic algorithmic stemmers (Porter, Snowball) for highly inflected languages.