lexf
In full-text search, a user searching for cut expects to hit cutting, searching for 数据库 expects to hit 數據庫, and searching for 1000.5 expects to hit 1,000.50.
lexf expands each token after segmentation into several search forms: first the normalized base form, then the stem and variants.
Indexing and querying use the same set of expansion rules, so both ends align naturally and recall works across inflections, traditional/simplified variants, and written formats.
All rules are based on algorithms and built-in tables; no dictionaries or models are loaded, there are no external dependencies, and no unsafe is used.
It is designed to work with segmentation results from mcut's CutMode::Search, and the output can be written directly into a PostgreSQL-compatible full-text index.
Usage
//! Expand search terms into normalized forms and stems
use ;
use ;
Output:
Die -> ["die"]
Häuser -> ["häuser", "hauser", "haeuser", "haus"]
und -> ["und"]
der -> ["der"]
Wald -> ["wald"]
Expansion rules
All tokens first undergo general normalization: lowercase, full-width characters converted to half-width, decimal digits in each script converted to ASCII digits, curly quotes, various hyphens and spaces unified, and combining diacritics removed.
They are then processed according to the scripts that appear in the token:
| Script | Normalization | Stemming |
|---|---|---|
| Latin | Remove diacritics (café → cafe); German digraphs (häuser → haeuser); merge two-part hyphenated words (e-mail → email) |
English uses Snowball Porter2 plus lemmatization (studies → study); German, French, Spanish, Portuguese, Italian, Dutch, Swedish, and Turkish use simplified Snowball rules |
| Han | Traditional to simplified (OpenCC table) | None |
| Japanese kana | Half-width katakana to full-width, katakana to hiragana | Remove polite and inflectional endings (ます, ました, etc.) |
| Korean | None | Remove particles and endings (를, 에서, etc.) |
| Cyrillic | ё → е |
Snowball Russian |
| Arabic | Remove vowel marks, unify variants such as alef and taa marbuta | Light10 prefix/suffix stripping |
| Greek | Remove accents, ς → σ |
Common inflectional suffixes |
Numbers and alphanumeric mixed tokens are handled separately:
- Pure numbers: remove thousands separators (only when every group after a comma has three digits;
3,14is unaffected), and remove trailing zeros in decimals. - Alphanumeric mixed tokens (
gpt-4o,v1.2.0): normalization only, no stemming.
Hyphenated Latin words are not stemmed.
Interface
expand_each
Expand one token, calling emit once for each form produced.
The output order is fixed: normalized base form first, then normalized variants, and finally the stem.
Output is deduplicated, empty forms are not emitted, and at most cfg.max_form forms are output.
Leading and trailing whitespace is ignored, and whitespace-only tokens emit no forms.
expand
Convenience version of expand_each that collects the results into a Vec<String>.
Each call allocates new buffers; use expand_each for batch processing.
LexConfig
lang: language hint, defaultLangHint::Auto. For Han, kana, Korean, etc., the script itself determines how they are processed; this field mainly affects which language's stemming rules are chosen for Latin words.max_form: maximum number of forms output per token, defaultDEFAULT_MAX_FORM(6). When 0, nothing is output.mode: expansion depth, defaultMode::NormAndStem.
When lang is Auto, Latin words are judged per token based on their own diacritics; if there are no diacritics, they are treated as English.
When lang is set to a non-Latin language such as Chinese or Japanese, Latin words within it are treated as English.
Mode
Norm: normalization only. Used for prefix queries (such as'cutin':*) and exact phrases, to avoid stemming changing the prefix.NormAndStem: normalization plus stemming, used for indexing and ordinary queries.
Scratch
Reusable working buffers.
Pass the same Scratch repeatedly in a loop so that no heap memory is allocated while processing each token.
LangHint
Language hint.
LangHint::from_code accepts ISO codes or PostgreSQL full-text search configuration names, case-insensitively, such as "de" and "German"; returns None when unrecognized.
detect_lang
Detect the primary language of a piece of text; suitable for detecting once per document and then filling the result into LexConfig::lang.
Non-Latin scripts are counted by the Unicode block each character belongs to, and the most frequent one is chosen. When the proportion of kana is high enough, Han characters in the same segment are counted as Japanese.
Latin script is scored using language-specific diacritics and the function words unique to each language in the Snowball stopword lists.
Returns LangHint::Other when the text contains no letters from supported scripts; returns LangHint::En when Latin script cannot be distinguished.