JFLib

spell#

spell checking: Hunspell dictionaries, the system's checker, a personal word list -- crate root lib/spell/lib.rs

spell -- spell checking: Hunspell dictionaries read by the library itself, the system's
checker (Windows ISpellChecker), a personal word list

  Dictionary::from_bytes(aff, dic) / load(aff_path, dic_path)   a Hunspell dictionary
       (.aff + .dic, any SET encoding of aff.rs); add_dic(bytes) a further word list;
       check(word), suggest(word), add_word(word); wordchars(), language(), words(),
       unsupported() (the .aff keywords it ignored)
  find(lang, SRC_*) -> Source        where the checker for a language tag lives
  languages(SRC_*) -> tags           the languages there are checkers for
  Speller::open(lang, SRC_*) / from_source(&source) / with_dictionary(dict, lang)
       the checker the UI uses: check(word), check_token(text, &token), cached(word),
       suggest(word), add(word) / remove(word) (the personal word list, kept in a file:
       set_personal_file), ignore(word) (this session), misspelled(text) -> byte ranges,
       language(), backend(), revision() (changes when the word lists change);
       suggester() -> Suggester: suggest(word, max) on another thread (Hunspell only)
  tokens(text, wordchars) -> Token   the words of a text worth checking (token.rs)
  personal_path(lang)                "config://spell/<language>.dic"
  Hyphenator::from_dic(bytes) / from_tex(text) / load(path), hyphenate(word)   hyphenation
       (Liang's patterns: libhyphen's hyph_xx_YY.dic, TeX's \patterns; hyphen.rs);
       find_hyphen(lang, SRC_*) / hyphen_languages(SRC_*): the dictionaries of the sources

Sources (SRC_*), tried in this order for every language:
  SRC_SYSTEM  Windows: ISpellChecker (plat::spell) for the language, then the Hunspell
              folders of DICPATH; macOS: NSSpellChecker (plat::spell), then
              ~/Library/Spelling and /Library/Spelling; iOS: UITextChecker; Linux: DICPATH, $XDG_DATA_HOME/hunspell
              (~/.local/share/hunspell), and hunspell, myspell, myspell/dicts below every
              XDG_DATA_DIRS folder (/usr/local/share, /usr/share); Web / Android: none
  SRC_USER    config://hunspell -- dictionaries the user put there
  SRC_APP     app://hunspell -- dictionaries the app ships (apps/NAME/assets/hunspell)
A dictionary file NAME.aff (with NAME.dic beside it) serves a tag when NAME, read without
case and with '-' as '_', is the tag ("de_DE"), a variant of it ("de_DE_frami"), the bare
language ("de"), or the language in another region ("de_AT"; the main region first:
"de_DE", "en_US").  Headless runs and tests use SRC_APP only, so their results never
depend on the machine.

What the Hunspell reader supports and ignores: aff.rs (format), check.rs (affix and compound
rules), suggest.rs (suggestions).  The checks follow Hunspell 1.7 (compared on de_DE_frami,
en_US, en_GB: docs/ROADMAP.md 4), with one difference on purpose: an UPPER word with a break
point ("CD-ROM", "E-MAIL-ADRESSE") is correct when its pieces are (Hunspell splits the Title
case form it made and rejects "Cd-rom").  Not supported: COMPOUNDSYLLABLE, COMPOUNDROOT,
COMPOUNDMORESUFFIXES, COMPLEXPREFIXES (two prefixes), a prefix together with two suffixes,
morphology (AM, the fields of the .dic), PHONE suggestions, OCONV, the replacements of
CHECKCOMPOUNDPATTERN (simplified compounds), the Turkish / Azeri casing of LANG; no clock
limits (Hunspell's results depend on the machine's speed, these never do).  The work is
bounded by counts instead, also for a crafted dictionary (config://hunspell is the user's to
fill): a check tries at most 6 million rules and patterns of the .aff (check.rs, CHECK_WORK),
a search for suggestions 64 million, its n-gram step 32 million (suggest.rs); the word list
keeps every lookup short, also for words whose hashes collide (dic.rs), and the .aff loads
in one pass over its rules (aff.rs).  Real dictionaries use a sixth of that or less (20 of
them measured, docs/ROADMAP.md section 13, U11); a crafted one takes at most about 0.15 s for
a check and 1.6 s for a suggestion instead of seconds and minutes.

Personal word list: one word per line (a first line with a number is skipped), UTF-8.  A
word added in lower case is also accepted Capitalised and in UPPER case; "Paris" only so.
Words ignored for the session work the same way and are never written.  The system's
checker never learns them (Windows' own word list stays untouched).

spell crate root#

  • struct Dictionary a Hunspell dictionary: the affix rules and the word list

impl Dictionary

  • fn from_bytes(aff: &[u8], dic: &[u8]) -> Dictionary a dictionary from the bytes of its .aff and .dic files
  • fn load(aff_path: &str, dic_path: &str) -> Option<Dictionary> a dictionary from two files (any vfs name: app://, config://, a path)
  • fn add_dic(&mut self, dic: &[u8]) a further word list in the dictionary's encoding and flags
  • fn add_word(&mut self, line: &str) a word as if the .dic had it ("word" or "word/flags")
  • fn language(&self) -> &str the language: the LANG of the .aff, or what the loader set ("de-DE")
  • fn set_language(&mut self, tag: &str) set the language tag (find() names it after the file)
  • fn wordchars(&self) -> &[char] WORDCHARS: characters that belong to words besides the letters
  • fn words(&self) -> usize the number of entries in the word list
  • fn unsupported(&self) -> &[String] .aff keywords this reader ignored
  • fn check(&self, word: &str) -> bool the word is correct (case variants, a trailing abbreviation dot, numbers, break points)
  • fn suggest(&self, word: &str) -> Vec<String> corrections for a misspelled word, best first (at most 8)

spell/hyphen.rs#

Source: lib/spell/hyphen.rs

hyphen -- hyphenation: Liang's algorithm on TeX patterns and on the dictionaries of
libhyphen (LibreOffice / OpenOffice "hyph_xx_YY.dic")

  Hyphenator::from_dic(bytes) -> Option   a libhyphen dictionary: the character set on the
       first line (UTF-8, ISO8859-1 .. -16, KOI8-R / -U, microsoft-cp1251 ...: aff.rs),
       LEFTHYPHENMIN / RIGHTHYPHENMIN / COMPOUNDLEFTHYPHENMIN / COMPOUNDRIGHTHYPHENMIN n,
       NOHYPHEN a,b,... (no break next to these), NEXTLEVEL (the patterns before it find
       the parts of a compound word, the ones after it hyphenate each part), one pattern per
       line, '%' / '#' comments
  Hyphenator::from_tex(text) -> Option    the \patterns{...} and \hyphenation{...} blocks of
       a TeX pattern file (hyph-utf8's hyph-*.tex; UTF-8, '%' comments)
  load(path) -> Option                    a .dic file, or .tex (by its \patterns)
  add_patterns(text)                      more patterns, whitespace-separated ("hy3ph")
  add_exceptions(text)                    words with every break shown ("ta-ble"): exact
                                          results for these words
  set_min(left, right)                    characters a break leaves at least on the left /
                                          right of the word (default and minimum: the
                                          dictionary's values, else 2 and 2)
  hyphenate(word) -> Vec<usize>           where word may break (byte offsets of the first
                                          character after each break, ascending)
  hyphenated(word, sep) -> String         word with sep at every break (tests, tools)
  find_hyphen(lang, SRC_*) -> Option<(path, tag)>, hyphen_languages(SRC_*)
                                          the dictionaries of a source (see below)

Liang: every pattern is letters with a digit (0..9) between them; for each place between
two letters of ".word." the largest digit of the matching patterns wins and odd allows a
break.  The word is lower-cased first (character by character) and digits in it count
as word boundaries ('.').  TeX patterns: every matching pattern counts.  .dic files are
made for libhyphen and read as it reads them: its automaton applies at each character only
the pattern that is its state there (the longest suffix of the text read so far that begins
some pattern; de_DE's first level is a list of words that relies on it), and it works in two
levels: the first level's odd places split a word into parts, every part goes through the
first level again (it may show parts the whole word hid), a part without parts is hyphenated
by the second level with COMPOUNDLEFT / RIGHTHYPHENMIN at its inner ends; never a break
after the last character.  A dictionary without NEXTLEVEL gets libhyphen's default first
level (parts at '-', '\'', U+2013, U+2019, which NOHYPHEN keeps from breaking themselves).
Then no break leaves fewer than LEFTHYPHENMIN / RIGHTHYPHENMIN characters at the ends, and
NOHYPHEN removes the breaks next to its strings.  The results equal libhyphen 2.8's on the 21
dictionaries of the freedesktop SDK (spelltest --hyphenate; 214,000 words, 0 differences).
Not supported: libhyphen's non-standard hyphenation ("c1k/k=k,1,1": Zucker -> Zuk-ker,
mainly hu_HU) -- such a pattern keeps the places around its replaced letters from breaking
(odd digits there count one more), so no break ever needs a changed spelling.

Sources (spell::SRC_*), like the Hunspell dictionaries (speller.rs): SRC_SYSTEM: DICPATH,
on Linux $XDG_DATA_HOME/hyphen and hyphen, hunspell, myspell, myspell/dicts below every
XDG_DATA_DIRS folder (/usr/share/hyphen: Debian's hyphen-* packages), on Windows also the
dictionary extensions of LibreOffice (%ProgramFiles%\LibreOffice\share\extensions\dict-*);
SRC_USER: config://hyphen; SRC_APP: app://hyphen.  A file hyph_<tag>.dic serves a language
tag like a Hunspell file NAME.aff: the tag ("de_DE"), the bare language, another region.
  • struct Hyphenator hyphenation patterns of one language (see the module header)

impl Hyphenator

  • fn new() -> Hyphenator no patterns (no breaks) and libhyphen's default first level

  • fn from_dic(bytes: &[u8]) -> Option<Hyphenator> a libhyphen dictionary (hyph_xx_YY.dic); None when it has no pattern

  • fn from_tex(text: &str) -> Option<Hyphenator> the \patterns{...} and \hyphenation{...} of a TeX file; None when it has no pattern

  • fn load(path: &str) -> Option<Hyphenator> a .dic file (or a TeX file: .tex)

  • fn add_patterns(&mut self, text: &str) more patterns (whitespace-separated, "hy3ph"), added to the hyphenating level

  • fn add_exceptions(&mut self, text: &str) exceptions: words with every break as '-' ("ta-ble", "project"); they replace the patterns for exactly these words (any case)

  • fn set_min(&mut self, left: usize, right: usize) characters a break leaves at least at the start / end of a word (the dictionary's LEFTHYPHENMIN / RIGHTHYPHENMIN stay the minimum)

  • fn min(&self) -> (usize, usize) (LEFTHYPHENMIN, RIGHTHYPHENMIN) in effect

  • fn patterns(&self) -> (usize, usize) the patterns of both levels (tests, tools)

  • fn hyphenate(&self, word: &str) -> Vec<usize> where word may break: the byte offset of the first character after each break

  • fn hyphenated(&self, word: &str, sep: &str) -> String word with sep at every break ("hy-phen-ation")

  • fn find_hyphen(lang: &str, sources: u32) -> Option<(String, String)> the hyphenation dictionary for a language tag: (path, the tag it serves); sources in the order SRC_SYSTEM, SRC_USER, SRC_APP (as spell::find)

  • fn hyphen_languages(sources: u32) -> Vec<String> the language tags there are hyphenation dictionaries for (sorted, each once)

spell/speller.rs#

Source: lib/spell/speller.rs

speller -- finding a language's checker and the Speller the UI uses (see lib.rs)
/// the system: Windows ISpellChecker, the Hunspell folders of Linux, DICPATH
SRC_SYSTEM: u32 = 1
/// config://hunspell
SRC_USER: u32 = 2
/// app://hunspell
SRC_APP: u32 = 4
/// every source
SRC_ALL: u32 = 7
  • enum Source where a language's checker lives

    • System(String) -- the system's checker (plat::spell) with this tag
    • Hunspell { aff: String, dic: String, lang: String, src: u32 } -- a Hunspell dictionary: the .aff and .dic names, the tag it serves, the SRC_* it came from
  • fn personal_path(lang: &str) -> String the personal word list of a language: "config://spell/<primary language>.dic"

  • fn find(lang: &str, sources: u32) -> Option<Source> where the checker for a language tag lives (sources tried in the order of lib.rs)

  • fn languages(sources: u32) -> Vec<String> the language tags there are checkers for (sorted, each once)

  • struct Speller a checker with its personal word list and a cache of results (see lib.rs)

impl Speller

  • fn with_dictionary(mut dict: Dictionary, lang: &str) -> Speller a checker for a Hunspell dictionary

  • fn from_source(s: &Source) -> Option<Speller> the checker of a source (Hunspell files are read now)

  • fn open(lang: &str, sources: u32) -> Option<Speller> the checker for a language tag (find + from_source)

  • fn language(&self) -> &str the language tag ("de-DE")

  • fn backend(&self) -> &'static str "hunspell" or "system"

  • fn dictionary(&self) -> Option<&Dictionary> the Hunspell dictionary (None for the system's checker)

  • fn wordchars(&self) -> &[char] WORDCHARS of the dictionary (for tokens())

  • fn revision(&self) -> u32 changes whenever the personal or the ignored words change (caches of results depend on it)

  • fn cached(&self, word: &str) -> Option<bool> the cached result for a word (None: not checked yet)

  • fn check(&mut self, word: &str) -> bool the word is correct (personal and ignored words, then the checker; cached)

  • fn check_token(&mut self, text: &str, t: &Token) -> bool a token of text is correct: the word, or the word with the dot that follows it (an abbreviation)

  • fn token_errors(&mut self, text: &str, t: &Token) -> Vec<(usize, usize)> the misspelled byte ranges of one token: none when it is correct. A hyphenated word marks only its wrong parts: from each part on the longest run of parts that is a word counts as right ("E-Mail" of "E-Mail-Adr"), wrong parts next to each other are one range ("E-Mial"); "Hasu-Tür" marks "Hasu"; right parts make a right word (a personal word inside)

  • fn misspelled(&mut self, text: &str) -> Vec<(usize, usize)> the byte ranges of the misspelled words of a text

  • fn suggest(&mut self, word: &str, max: usize) -> Vec<String> corrections, best first (at most max)

  • fn suggester(&self) -> Option<Suggester> suggest() for another thread: the Hunspell dictionary and the personal list as they are now (None for the system's checker, which stays on its thread)

  • fn add(&mut self, word: &str) -> bool add a word to the personal list (written to the file of set_personal_file) -> the file was written (or there is none)

  • fn remove(&mut self, word: &str) -> bool take a word off the personal list -> the file was written (or there is none)

  • fn ignore(&mut self, word: &str) accept the word for this session (not written anywhere)

  • fn personal(&self) -> Vec<String> the personal list

  • fn ignored(&self) -> Vec<String> the words ignored this session

  • fn set_personal_file(&mut self, path: Option<&str>) keep the personal list in a file (read now; None: in memory only)

  • struct Suggester a Speller's suggestions for a worker thread (Speller::suggester): the same results as Speller::suggest at the time it was made

impl Suggester

  • fn suggest(&self, word: &str, max: usize) -> Vec<String> corrections, best first (at most max)

spell/token.rs#

Source: lib/spell/token.rs

token -- the words of a text that are worth checking

A word is a run of letters (alphabetic characters and combining marks, plus the
dictionary's WORDCHARS); apostrophes and hyphens join letters ("don't", "E-Mail"), dots only
when WORDCHARS has them ("z.B").  Skipped: words with a digit ("3D", "MP3", "1st"), and every
blank-separated chunk that looks like a URL, an e-mail address or a path (contains "://",
'@' or '\\', starts with "www.", '/' or "~/", or a drive "C:").  A token records whether a
'.' follows it, so an abbreviation ("usw.") can be checked with its dot.
  • struct Token a word of a text: its byte range, and whether a '.' follows it

    • start: usize
    • end: usize
    • dot: bool
  • fn tokens(text: &str, wordchars: &[char]) -> Vec<Token> the words of text to check (see the module header); wordchars: the dictionary's WORDCHARS

Source: docs/API.md in the JFLib repository.