Installation:
composer require nqxcode/phpmorphy
Add to composer.json if not auto-loaded:
"autoload": {
"psr-4": {
"App\\": "app/",
"Nqxcode\\PhpMorphy\\": "vendor/nqxcode/phpmorphy/src/"
}
}
Run composer dump-autoload.
First Use Case: Initialize the morpher in a service or controller:
use Nqxcode\PhpMorphy\PhpMorphy;
$morpher = new PhpMorphy();
$morpher->loadDictionary('path/to/dictionary'); // e.g., 'ru_RU' for Russian
Basic Morphing:
$word = $morpher->inflect('бегу', 'nomn'); // Converts to nominative case
$plural = $morpher->inflect('дом', 'plur'); // Converts to plural
Case Conversion:
$morpher->inflect('собака', 'genm'); // 'собаки' (genitive)
$morpher->inflect('собаки', 'nomn'); // 'собака' (nominative)
Number Agreement:
$morpher->inflect('яблоко', 'plur'); // 'яблоки' (plural)
$morpher->inflect('яблоки', 'sing'); // 'яблоко' (singular)
Gender Handling:
$morpher->inflect('красивый', 'femn'); // 'красивая' (feminine)
Integration with Laravel:
// app/Providers/PhpMorphyServiceProvider.php
public function register()
{
$this->app->singleton('morpher', function () {
$morpher = new PhpMorphy();
$morpher->loadDictionary(config('morphy.dictionary'));
return $morpher;
});
}
// config/morphy.php
return [
'dictionary' => 'ru_RU',
];
Batch Processing:
$words = ['дом', 'собака', 'яблоко'];
$inflected = array_map(function ($word) use ($morpher) {
return $morpher->inflect($word, 'genm');
}, $words);
Validation Rules:
// app/Rules/ValidRussianWord.php
public function passes($attribute, $value)
{
$morpher = app('morpher');
return $morpher->checkWord($value); // Checks if word exists in dictionary
}
Dictionary Loading:
ru_RU for Russian). Ensure the path is correct:
$morpher->loadDictionary(base_path('dictionaries/ru_RU'));
Case Sensitivity:
$normalized = mb_strtolower($word, 'UTF-8');
$morpher->inflect($normalized, 'nomn');
Performance:
PhpMorphy instance in Laravel's container or a singleton:
$morpher = app('morpher'); // Reuse instance
Unsupported Languages:
Deprecated Methods:
PhpMorphy::getDictionary() if the package evolves. Use loadDictionary() for explicit control.Check Word Existence:
if (!$morpher->checkWord('несуществующее')) {
Log::warning('Word not found in dictionary: несуществующее');
}
Log Inflection Results:
$result = $morpher->inflect('бег', 'nomn');
Log::debug("Inflected 'бег' to '$result'");
Handle Exceptions: Wrap usage in try-catch for edge cases:
try {
$inflected = $morpher->inflect($word, $grammemes);
} catch (\Exception $e) {
Log::error("Morphy error: " . $e->getMessage());
return $word; // Fallback
}
Custom Grammemes: Extend the package by adding new grammeme rules (requires modifying the source or forking the repo).
Laravel Facade: Create a facade for cleaner syntax:
// app/Facades/PhpMorphy.php
public static function inflect($word, $grammemes)
{
return app('morpher')->inflect($word, $grammemes);
}
Usage:
PhpMorphy::inflect('дом', 'genm'); // 'дома'
Event Listeners: Trigger events for inflection results (e.g., log or analyze changes):
// app/Listeners/LogInflection.php
public function handle($event)
{
Log::info("Inflected {$event->original} to {$event->inflected}");
}
How can I help you explore Laravel packages today?