Installation Add the package via Composer:
composer require nqxcode/lucene-stemmer-en-ru
Basic Usage Require the autoloader and instantiate the stemmer:
require 'vendor/autoload.php';
use Nqxcode\LuceneStemmer\Stemmer;
$stemmer = new Stemmer();
First Use Case Stemming English or Russian words:
$englishWord = $stemmer->stem('running'); // Output: 'run'
$russianWord = $stemmer->stem('бегущий'); // Output: 'бег'
Integration with Laravel
Register the service provider in config/app.php:
'providers' => [
// ...
Nqxcode\LuceneStemmer\LuceneStemmerServiceProvider::class,
],
Bind the stemmer in config/services.php (if needed) or use the facade:
use Nqxcode\LuceneStemmer\Facades\Stemmer;
$stemmedWord = Stemmer::stem('example');
Search Optimization Use the stemmer in Laravel Scout or custom search logic:
$searchTerm = Stemmer::stem($request->input('q'));
$results = Model::search($searchTerm)->get();
Text Processing Pipeline Chain stemming with other text transformations:
$processedText = strtolower($text);
$processedText = Stemmer::stem($processedText);
Database Indexing Store stemmed versions of text in a separate column for faster searches:
$stemmed = Stemmer::stem($post->content);
$post->stemmed_content = $stemmed;
$post->save();
Laravel Scout Customizer
Extend ScoutEngine to use stemming in search queries:
public function scopeSearchUsingStemmer($query, $search)
{
$stemmedSearch = Stemmer::stem($search);
return $query->where('stemmed_column', 'like', "%{$stemmedSearch}%");
}
Form Request Validation Validate and stem input before processing:
public function rules()
{
return [
'query' => 'required|string|max:255',
];
}
public function passedValidation()
{
$this->merge([
'stemmed_query' => Stemmer::stem($this->query),
]);
}
Middleware for Global Stemming Apply stemming to all incoming search queries via middleware:
public function handle($request, Closure $next)
{
if ($request->is('search*')) {
$request->merge(['q' => Stemmer::stem($request->q)]);
}
return $next($request);
}
Deprecated Dependencies The package relies on ZendSearch/Lucene, which is outdated. Ensure compatibility with your Laravel version (pre-Laravel 5.5 recommended due to PHP 5.6+ requirements).
Performance Overhead Stemming adds processing time. Cache results for frequent queries:
$stemmed = Cache::remember("stem_{$word}", now()->addHours(1), function() use ($word) {
return Stemmer::stem($word);
});
Language Detection The stemmer assumes input language. Explicitly handle mixed-language text:
if (Text::contains($text, ['а', 'б', 'в'])) {
// Russian logic
} else {
// English logic
}
Edge Cases
stem('') to avoid errors.$cleaned = preg_replace('/[^a-zа-я]/u', '', $text);
$stemmed = Stemmer::stem($cleaned);
running → run, бегущий → бег).\Log::debug('Stemmed: ' . Stemmer::stem($input));
Custom Stemming Rules Extend the stemmer by subclassing and overriding methods:
class CustomStemmer extends Stemmer {
protected function customRule($word) {
// Add logic here
}
}
Integration with Laravel Search Create a custom search trait:
trait UsesStemmedSearch {
public function scopeStemmedSearch($query, $term) {
return $query->where('stemmed_column', Stemmer::stem($term));
}
}
Testing Mock the stemmer in tests to avoid dependency on external libraries:
$this->app->instance(Stemmer::class, Mockery::mock(Stemmer::class));
How can I help you explore Laravel packages today?