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.
sudo apt install libhunspell-devuses 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);
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);
| Function | Signature | Description |
|---|---|---|
| NewSpeller | NewSpeller(affPath, dicPath: string): TSpeller | Create a speller from explicit .aff and .dic file paths. Use this when your dictionary files are in a non-standard location. |
| NewSpellerLang | NewSpellerLang(lang: string): TSpeller | Create a speller using a language code (e.g. 'en_US'). Looks up the dictionary in standard system paths (/usr/share/hunspell/). |
| FreeSpeller | FreeSpeller(spell: TSpeller) | Release all memory held by the speller instance. Always call when done. |
| AddDictionary | AddDictionary(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). |
| Function / Field | Type / Signature | Description |
|---|---|---|
| Check | Check(spell: TSpeller; word: string): Boolean | Return True if the word is correctly spelled according to the loaded dictionaries. |
| CheckText | CheckText(spell: TSpeller; text: string): TCheckResult | Tokenize text and check every word. Returns a TCheckResult with per-word detail. |
| TCheckResult.WordCount | Integer | Total number of words found in the input text. |
| TCheckResult.ErrorCount | Integer | Number of words that failed spell check. |
| TCheckResult.Words | array of TWordResult | Per-word results. Iterate to find misspellings and their suggestions. |
| TWordResult.Word | string | The original word token. |
| TWordResult.IsCorrect | Boolean | True if the word passed spell check. |
| TWordResult.Suggestions | array of string | Up to 5 correction suggestions for misspelled words. |
| TWordResult.Offset | Integer | Byte offset of the word in the original input string. |
| Function | Signature | Description |
|---|---|---|
| Suggest | Suggest(spell: TSpeller; word: string; maxCount: Integer): array of string | Return up to maxCount correction suggestions for the given word, ordered by likelihood. |
| SuggestScored | SuggestScored(spell: TSpeller; word: string; maxCount: Integer): array of TScoredWord | Return suggestions with an edit-distance score. TScoredWord has fields Word (string) and Score (Integer, lower is closer). |
| Function | Signature | Description |
|---|---|---|
| AddWord | AddWord(spell: TSpeller; word: string) | Add a word to the speller's in-session dictionary. Subsequent Check calls will accept it as correct. |
| AddWordWithExample | AddWordWithExample(spell: TSpeller; word, example: string) | Add a word with a morphological example that helps Hunspell derive compound and inflected forms. |
| RemoveWord | RemoveWord(spell: TSpeller; word: string) | Remove a word from the session dictionary (useful for temporarily blacklisting a term). |
| Function | Signature | Description |
|---|---|---|
| Stem | Stem(spell: TSpeller; word: string): array of string | Return the base stem(s) of the word. "running" → ["run"]. Useful for search indexing and text normalization. |
| Analyze | Analyze(spell: TSpeller; word: string): array of string | Return morphological analysis tags for the word (e.g. part of speech, tense, number). Format depends on the dictionary's .aff file. |
| Generate | Generate(spell: TSpeller; word, example: string): array of string | Generate inflected or derived forms of word by analogy with example. Useful for NLP data augmentation. |
| Function | Signature | Description |
|---|---|---|
| FindErrors | FindErrors(spell: TSpeller; text: string): array of TWordResult | Shorthand for CheckText that returns only the misspelled words, skipping correctly spelled ones. |
| AutoCorrect | AutoCorrect(spell: TSpeller; text: string): string | Replace each misspelled word with the top suggestion (if one exists). Returns the corrected text. |
| ListAvailableLangs | ListAvailableLangs(): array of string | Scan system Hunspell directories and return a list of installed language codes (e.g. ["en_US", "fr_FR", "de_DE"]). |
Install language dictionaries via apt before calling NewSpellerLang. Dictionary packages install .aff and .dic files to /usr/share/hunspell/.
apt search hunspell- lists all available language packs. Over 100 languages are available in standard Debian/Ubuntu repositories.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.
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.
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.
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.