This bundle integrates the dompat/stemmer library into Symfony. It provides automatic service registration, Twig filters and easy configuration for language drivers.
|stem filter for your templates.DriverInterface to add your own drivers.Install the bundle via Composer:
composer require dompat/stemmer-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Dompat\StemmerBundle\DompatStemmerBundle::class => ['all' => true],
];
You can inject Dompat\Stemmer\Stemmer into your services or controllers:
use Dompat\Stemmer\Stemmer;
use Dompat\Stemmer\Enum\StemmerMode;
public function search(string $query, Stemmer $stemmer)
{
$stemmed = $stemmer->stem($query, 'cs', StemmerMode::AGGRESSIVE);
// ...
}
The bundle provides a stem filter:
{# Simple usage (uses LIGHT mode by default) #}
{{ 'working'|stem('en') }} {# output: work #}
{# With explicit mode #}
{{ 'declaration'|stem('en', 'aggressive') }} {# output: declar #}
Available modes: light (default), aggressive.
By default, the bundle registers all drivers found in dompat/stemmer. You can add custom mapping or override default drivers in config/packages/dompat_stemmer.yaml:
dompat_stemmer:
contexts:
sk: Dompat\Stemmer\Driver\CzechDriver # Use Czech rules for Slovak language
en: App\Stemmer\MyCustomEnglishDriver # Force your custom driver for English
To add a new language driver, implement Dompat\Stemmer\Contract\DriverInterface.
If you use autoconfiguration (default in Symfony), your driver will be automatically registered with the Stemmer manager.
Custom drivers have a higher priority by default. This means if you create your own EnglishDriver, it will automatically replace the original one from the library without any extra configuration.
If you want to use a specific driver for a locale (e.g., to switch back to the original one or map a different class), use the contexts configuration shown above.
namespace App\Stemmer;
use Dompat\Stemmer\Contract\DriverInterface;
use Dompat\Stemmer\Contract\StemmerModeInterface;
class FrenchDriver implements DriverInterface
{
public function getLocale(): string
{
return 'fr';
}
public function stem(string $word, StemmerModeInterface $mode): string
{
// ... your implementation
}
public function __toString(): string
{
return 'FrenchDriver';
}
}
This bundle is licensed under the MIT License.
How can I help you explore Laravel packages today?