Skip to content

09. Semantic

ogoun edited this page Apr 23, 2026 · 1 revision

Semantic & Text

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.

Tokenization

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-def

Variants:

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).

Stop-words

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.

Stemming

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.

N-grams

var grams = NGramsBuilder.BuildNGrams("hello", 3);   // "hel", "ell", "llo"

Useful for fuzzy matching short strings or as cheap features for a classifier.

String distance

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.

Trie & suffix structures

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.

Transliteration

using ZeroLevel.Services.Text;

string latin = Transliteration.Front("Пушкин");   // "Pushkin"

Bidirectional Russian ↔ Latin, rule-based (GOST-like), predictable.

Text on curve

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.

Tips

  • Always lowercase after tokenization — ExtractWords is 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_ngrams with decreasing Boost.
  • The stop-word list is process-global; don't mutate it in tests without rolling back.

Clone this wiki locally