Repository navigation
09. Semantic
The semantic helpers live in ZeroLevel.Services.Semantic and its Implementation.Helpers namespace. They cover everything you typically need before feeding text into an index or a classifier: tokenization, stop-word removal, stemming, n-grams and string-distance metrics.
TextAnalizer splits free text into word tokens using Unicode + Cyrillic-aware regexes:
using ZeroLevel.Implementation.Semantic.Helpers;
foreach (var w in TextAnalizer.ExtractWords("Hello, мир! 123 abc-def."))
Console.WriteLine(w);
// Hello
// мир
// 123
// abc-defVariants:
| Method | Output |
|---|---|
ExtractWords(text) |
All word tokens. |
ExtractRuWords(text) |
Russian-only tokens (drops Latin). |
ExtractUniqueWords(text) |
Unique tokens (HashSet dedup). |
ExtractUniqueWordsWithoutStopWords(text) |
Unique, minus stop-words (StopWords). |
ExtractWordTokens(text) |
WordToken(Value, Index) — token + original position. |
The underlying regex is \b[\wА-Яа-я-'']+\b — note the - and apostrophe inside words (so "state-of-the-art" stays one token).
using ZeroLevel.Services.Semantic;
bool stop = StopWords.IsStopWord("and"); // true (English built-in list)English and Russian lists are bundled. Extend them by calling StopWords.Add(lang, words) at startup.
A Snowball-style stemmer interface (ILexProvider) plus the common Russian implementation:
using ZeroLevel.Services.Semantic;
ILexProvider lex = new LexProvider(new RussianStemmer());
foreach (var tok in lex.ExtractUniqueLexTokens("Пушкин писал стихи"))
{
// tok.Token — stemmed form ("пушк", "писа", "стих")
// tok.Words — original words that produced this stem
}For English / Bulgarian / German / etc., plug any IStemmer — the Iveonik.Stemmers NuGet package exposes stemmers for ~15 languages and is commonly used alongside ZeroLevel.
var grams = NGramsBuilder.BuildNGrams("hello", 3); // "hel", "ell", "llo"Useful for fuzzy matching short strings or as cheap features for a classifier.
using ZeroLevel.Services.Semantic;
int d = LevenshteinDistance.Compute("kitten", "sitting"); // 3
double j = JaroWinkler.Similarity("MARTHA", "MARHTA"); // 0.96...Provided metrics:
- Levenshtein (edit distance).
- Damerau-Levenshtein (with transpositions).
- Jaro / Jaro-Winkler.
- Hamming (equal-length strings).
- Dice / Sørensen on bigrams.
For substring / prefix lookups in large dictionaries, ZeroLevel.Services.Trees has:
-
Trie<TValue>— classical prefix tree. -
SuffixAutomaton— linear-time substring membership, useful for "all substrings of N strings" style queries.
using ZeroLevel.Services.Text;
string latin = Transliteration.Front("Пушкин"); // "Pushkin"Bidirectional Russian ↔ Latin, rule-based (GOST-like), predictable.
ZeroLevel.Services.Drawing.TextOnCurve renders a string along a parametric curve — handy for map labels, compass roses, decorative rendering. See the WPFExamples project for a live demo.
- Always lowercase after tokenization —
ExtractWordsis case-insensitive on word boundaries but preserves case in output. - Combine stemming + stop-words + n-grams as layered fields in Sleopok Engine — e.g.
title(raw) +title_stemmed+title_ngramswith decreasingBoost. - The stop-word list is process-global; don't mutate it in tests without rolling back.
Application basics
Data & serialization
Networking
Text & search