A morphological solution written completely in PHP.
- Installation
- Quick start
- Russian
- Declension
- First names
- Middle names
- Last names
- Nouns
- Pluralization
- Numeral creation
- Cases
- Declension
- English
- Pluralization
- Addition of new languages
- Download library through composer:
composer require wapmorgan/morphos
Adapters:
- Adapter for Laravel (Blade): wapmorgan/morphos-blade
- Adapter for Symfony (Twig): wapmorgan/morphos-twig
Decline russian names:
var_dump(morphos\Russian\nameCase('Иванов Петр', morphos\Cases::GENETIVE, morphos\NamesDeclension::MAN)); // Иванова Петра
var_dump(morphos\Russian\nameCase('Кулаков Святослав Матвеевич', morphos\Cases::GENETIVE, morphos\NamesDeclension::MAN)); // Кулакова Святослава Матвеевича
Pluralize russian nouns:
var_dump(morphos\Russian\Plurality::pluralize('дом', 10)); // домов
var_dump(morphos\Russian\Plurality::pluralize('гидродендрариум', 2)); // гидродендрариума
Generate russian cardinal numerals:
var_dump(morphos\Russian\CardinalNumeral::generate(567)); // пятьсот шестьдесят семь
Pluralize english nouns:
var_dump(morphos\English\Plurality::pluralize('house', 10)); // houses
Russian morphology:
morphos\
Russian\
nameCase()
detectGender()
Cases
FirstNamesDeclension
LastNamesDeclension
GeneralDeclension
Plurality
CardinalNumeral
To compound all declension functionality in one call there is nameCase
function:
string|array nameCase($name, $case = null, $gender = null)
Arguments:
$name
- full name asФамилия Имя
orФамилия Имя Отчество
.$case
- can benull
or one ofCases
constants. If constant, a string will be returned. If null, an array will be returned.$gender
-NamesDeclension::MAN
orNamesDeclension::WOMAN
ornull
.
All declension classes are similar and have three common methods:
boolean hasForms($word, $gender)
- Check if name is mutable.string getForm($word, $case, $gender)
- Declines name.array getForms($word, $gender)
- Declines name to all cases.string detectGender($word)
- Tries to detect gender for given name.
Case
is one constant of Cases
class constants (described below).
Declension of first names in russian language.
Create declension class object:
use morphos\NamesDeclension;
use morphos\Russian\Cases;
use morphos\Russian\FirstNamesDeclension;
$dec = new FirstNamesDeclension();
Check whether there are forms for this name and if they exist get it:
// for example, let it be Иван
$user_name = 'Иван';
// we want to get it's genetivus form
if ($dec->hasForms($user_name, $dec->detectGender($user_name))) {
$name = $dec->getForm($user_name, Cases::RODIT, $dec->detectGender($user_name));
} else { // immutable name
$name = $user_name;
}
If you need all forms, you can get all forms of a name:
var_dump($dec->getForms($user_name, $dec->detectGender($user_name)));
/* Will produce something like
array(6) {
["nominativus"]=>
string(8) "Иван"
["genetivus"]=>
string(10) "Ивана"
["dativus"]=>
string(10) "Ивану"
["accusative"]=>
string(10) "Ивана"
["ablativus"]=>
string(12) "Иваном"
["praepositionalis"]=>
string(15) "об Иване"
}
*/
Declension of middle names in russian language.
Create declension class object:
use morphos\NamesDeclension;
use morphos\Russian\Cases;
use morphos\Russian\MiddleNamesDeclension;
$dec = new MiddleNamesDeclension();
Check whether there are forms for this name and if they exist get it:
// for example, let it be Иван
$user_name = 'Сергеевич';
$name = $dec->getForm($user_name, Cases::RODIT, $dec->detectGender($user_name));
If you need all forms, you can get all forms of a name:
var_dump($dec->getForms($user_name, $dec->detectGender($user_name)));
/* Will produce something like
array(6) {
["nominativus"]=>
string(18) "Сергеевич"
["genetivus"]=>
string(20) "Сергеевича"
["dativus"]=>
string(20) "Сергеевичу"
["accusative"]=>
string(20) "Сергеевича"
["ablativus"]=>
string(22) "Сергеевичем"
["praepositionalis"]=>
string(23) "о Сергеевиче"
}
*/
Declension of last names in russian language.
Create declension class object:
use morphos\NamesDeclension;
use morphos\Russian\Cases;
use morphos\Russian\LastNamesDeclension;
$dec = new LastNamesDeclension();
Check whether there are forms for this name and if they exist get it:
$user_last_name = 'Иванов';
if ($dec->hasForms($user_last_name, $dec->detectGender($user_last_name))) {
$dativus_last_name = $dec->getForm($user_last_name, Cases::RODIT, $dec->detectGender($user_last_name));
} else { // immutable last name
$dativus_last_name = $user_last_name;
}
echo 'Мы хотим подарить товарищу '.$dativus_last_name.' небольшой презент.';
If you need all forms, you can get all forms of a name:
var_dump($dec->getForms($user_last_name, $dec->detectGender($user_last_name)));
/* Will produce something like
array(6) {
["nominativus"]=>
string(12) "Иванов"
["genetivus"]=>
string(14) "Иванова"
["dativus"]=>
string(14) "Иванову"
["accusative"]=>
string(14) "Иванова"
["ablativus"]=>
string(16) "Ивановым"
["praepositionalis"]=>
string(19) "об Иванове"
}
*/
Declension of nouns in russian language.
General declension class also have few similar methods but arguments are different:
boolean hasForms($word, bool $animateness = false)
- Check if noun is mutable.string getForm($word, $case, $animateness = false)
- Declines noun.array getForms($word, $animateness = false)
- Declines noun to all cases.
Create declension class object:
use morphos\Russian\Cases;
use morphos\Russian\GeneralDeclension;
$dec = new GeneralDeclension();
Check whether there are forms for this word (second arg is an animateness) and get them:
if ($dec->hasForms('поле', false)) {
$form = $dec->getForm('поле', Cases::RODIT, false);
}
Get all forms of a word at once:
var_dump($dec->getForms('линейка', false));
/* Will produce something like
array(6) {
["nominativus"]=>
string(14) "линейка"
["genetivus"]=>
string(14) "линейкы"
["dativus"]=>
string(14) "линейке"
["accusative"]=>
string(14) "линейку"
["ablativus"]=>
string(16) "линейкой"
["praepositionalis"]=>
string(17) "о линейке"
}
*/
Pluralization nouns in Russian.
This class have similar methods but not only:
string getForm($word, $case, $animateness = false)
- Pluralizes noun and declines.array getForms($word, $animateness = false)
- Pluralizes noun and declines to all cases.string #pluralize($word, $count, $animateness = false)
- Pluralizes noun to coincide with numeral.
Get plural form of a noun:
use morphos\Russian\Cases;
use morphos\Russian\Plurality;
$plu = new Plurality();
$word = 'дом';
$plural = $plu->getForm($word, Cases::IMENIT);
echo 'Множественное число для '.$word.' - '.$plural;
Pluralize word and get all forms:
var_dump($plu->getForms('поле', false));
/* Result will be like
array(6) {
["nominativus"]=>
string(8) "поля"
["genetivus"]=>
string(10) "полей"
["dativus"]=>
string(10) "полям"
["accusative"]=>
string(8) "поля"
["ablativus"]=>
string(12) "полями"
["praepositionalis"]=>
string(10) "полях"
}
*/
Pluralize noun to use it with numeral:
use morphos\Russian\Plurality;
$word = 'дом';
$count = 10;
echo $count.' '.Plurality::pluralize($word, $count, false);
// result: 10 домов
All number creation classes are similar and have two common methods:
string getForm($number, $case, $gender = NumeralCreation::MALE)
- Get one form of a number.array getForms($number, $gender = NumeralCreation::MALE)
- Get all forms of a number.string #generate($number, $gender = NumeralCreation::MALE)
- Generates a cardinal numeral for a number.
$gender
is one of morphos\NumeralCreation
constants: MALE
or FEMALE
or NEUTER
.
Creation of cardinal numerals in russian language.
Create declension class object:
use morphos\NumeralCreation;
use morphos\Russian\CardinalNumeral;
use morphos\Russian\Cases;
$cardinal = new CardinalNumeral();
Get text representation of a number:
$number = 4351;
$numeral = $cardinal->getForm($number, Cases::IMENIT); // четыре тысячи триста пятьдесят один
$numeral = $cardinal->getForm($number, Cases::IMENIT, NumeralCreation::FEMALE); // четыре тысячи триста пятьдесят одна
If you need all forms, you can get all forms of a name:
var_dump($cardinal->getForms($number));
/* Will produce something like
array(6) {
["nominativus"]=>
string(66) "четыре тысячи триста пятьдесят один"
["genetivus"]=>
string(74) "четырех тысяч трехсот пятидесяти одного"
["dativus"]=>
string(80) "четырем тысячам тремстам пятидесяти одному"
["accusative"]=>
string(66) "четыре тысячи триста пятьдесят один"
["ablativus"]=>
string(90) "четырьмя тысячами тремястами пятьюдесятью одним"
["praepositionalis"]=>
string(81) "о четырех тысячах трехстах пятидесяти одном"
}
*/
Generate numeral of a number:
use morphos\Russian\CardinalNumeral;
$number = 4351;
echo CardinalNumeral::generate($number);
// result: четыре тысячи триста пятьдесят один
Cases in russian language:
morphos\Russian\Cases::IMENIT
- nominative casemorphos\Russian\Cases::RODIT
- genetive casemorphos\Russian\Cases::DAT
- dative casemorphos\Russian\Cases::VINIT
- accusative casemorphos\Russian\Cases::TVORIT
- ablative casemorphos\Russian\Cases::PREDLOJ
- prepositional case
English morphology:
morphos\
English\
Plurality
Pluralization a word in English:
use morphos\English\Plurality;
$plu = new Plurality();
$word = 'foot';
$count = 10;
echo $count.' '.$plu->pluralize($word, $count);
// result: 10 feet
See CONTRIBUTING.md for this.