Files
ave-cms/help/en/helpers/RussianStemmer.md
2026-07-27 12:58:44 +03:00

1.2 KiB
Raw Permalink Blame History

RussianStemmer - stemming Russian words

App\Helpers\RussianStemmer - converts a Russian word to its stem (Porters algorithm) for fuzzy search: different forms of one word find each other. Used as instance.

use App\Helpers\RussianStemmer;

$stemmer = new RussianStemmer();

Methods

###stemWord($word) Return the stem of the word.

$stemmer->stemWord('кресла');    // 'кресл'
$stemmer->stemWord('креслом');   // 'кресл'
$stemmer->stemWord('кресел');    // 'кресел' → приводится к общей основе с прочими формами

###clearCache() Clear internal computed basis cache.


Recipe

Text indexing for search:

$stemmer = new RussianStemmer();
$stems = array();
foreach (preg_split('/\s+/u', mb_strtolower($text, 'UTF-8')) as $word) {
    $word = trim($word);
    if ($word !== '') { $stems[] = $stemmer->stemWord($word); }
}
// $stems сохраняем в поисковый индекс; запрос стеммим так же

So “buy chairs” will find a document with the word “chair”. To correct the layout