A morphological solution written completely in PHP.
- Installation
- Quick start
- Russian
- Personal names
- Geographical names
- Nouns
- Numerals
- Cardinal
- Ordinal
- Cases
- English
- Nouns
- Numerals
- Cardinal
- Contibuting
- 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\name('Иванов Петр', 'родительный', morphos\Gender::MALE)); // Иванова Петра
-
Pluralize russian nouns:
var_dump(morphos\Russian\pluralize('дом', 10)); // домов
-
Generate russian cardinal numerals:
var_dump(morphos\Russian\CardinalNumeral::getCase(567, 'именительный')); // пятьсот шестьдесят семь
-
Generate russian ordinal numerals:
var_dump(morphos\Russian\OrdinalNumeral::getCase(961, 'именительный')); // девятьсот шестьдесят первый
-
Pluralize english nouns:
var_dump(morphos\English\pluralize('house')); // houses
-
Generate english cardinal numerals:
var_dump(morphos\English\CardinalNumeral::generate(567)); // five hundred sixty-seven
Russian morphology shortcuts:
morphos\Russian\name($fullname, $case, $gender = AUTO)
morphos\Russian\pluralize($noun, $count = 2, $animateness = false)
Russian class hierarchy:
morphos\
Russian\
CardinalNumeral
Cases
GeneralDeclension
FirstNamesDeclension
LastNamesDeclension
MiddleNamesDeclension
OrdinalNumeral
Plurality
To compound all declension functionality in one call there is name
function:
string|array name($name, $case = null, $gender = null)
Arguments:
$name
- full name asФамилия Имя
orФамилия Имя Отчество
.$case
- can benull
, one ofCases
constants, a string (described below). If not null, a string will be returned. If null, an array will be returned.$gender
- one ofGender
constants, a string ("m"
for male name,"f"
for female name),null
for autodetection.
There is three declension classes for partial declension:
FirstNamesDeclension
- declension of first names in russian languageMiddleNamesDeclension
- declension of middle names in russian languageLastNamesDeclension
- declension of last names in russian language
All declension classes are similar and have four common methods:
boolean isMutable($word, $gender = null)
- Check if name is mutable.string getCase($word, $case, $gender = null)
- Declines name.$case
is one constant ofmorphos\Cases
ormorphos\Russian\Cases
class constants (described below).array getCases($word, $gender = null)
- Declines name to all cases.string detectGender($word)
- Tries to detect gender for given name.
$gender
is morphos\Gender::MALE
(or "m"
) or morphos\Gender::FEMALE
(or "f"
) or null
for autodetection. Note that detection of middle name and last name can make right decision, but first names sometimes can not be used to determine gender of it's owner. Especially if name is not native Russian name. So better to specify gender if you want to declinate first names.
Cases in russian language:
- nominative case -
morphos\Russian\Cases::IMENIT
orименительный
- genitive case -
morphos\Russian\Cases::RODIT
orродительный
- dative case -
morphos\Russian\Cases::DAT
orдательный
- accusative case -
morphos\Russian\Cases::VINIT
orвинительный
- ablative case -
morphos\Russian\Cases::TVORIT
orтворительный
- prepositional case -
morphos\Russian\Cases::PREDLOJ
orпредложный
Examples:
FirstNamesDeclension
use morphos\Russian\FirstNamesDeclension;
// Get any form of a name:
// for example, let it be Иван
$user_name = 'Иван';
$name = FirstNamesDeclension::getCase($user_name, 'родительный'); // Ивана
// If you need all forms, you can get all forms of a name:
var_dump(FirstNamesDeclension::getCases($user_name));
/* Will produce something like
array(6) {
["nominative"]=>
string(8) "Иван"
["genitive"]=>
string(10) "Ивана"
["dative"]=>
string(10) "Ивану"
["accusative"]=>
string(10) "Ивана"
["ablative"]=>
string(12) "Иваном"
["prepositional"]=>
string(15) "об Иване"
}
*/
MiddleNamesDeclension
use morphos\Russian\MiddleNamesDeclension;
// Get any form of a name:
// for example, let it be Сергеевич
$user_name = 'Сергеевич';
$name = MiddleNamesDeclension::getCase($user_name, 'родительный'); // Сергеевича
// If you need all forms, you can get all forms of a name:
var_dump(MiddleNamesDeclension::getCases($user_name));
/* Will produce something like
array(6) {
["nominative"]=>
string(18) "Сергеевич"
["genitive"]=>
string(20) "Сергеевича"
["dative"]=>
string(20) "Сергеевичу"
["accusative"]=>
string(20) "Сергеевича"
["ablative"]=>
string(22) "Сергеевичем"
["prepositional"]=>
string(23) "о Сергеевиче"
}
*/
LastNamesDeclension
use morphos\Russian\LastNamesDeclension;
// Get any case of a last name
$user_last_name = 'Иванов';
$dative_last_name = LastNamesDeclension::getCase($user_last_name, 'дательный'); // Иванову
echo 'Мы хотим подарить товарищу '.$dative_last_name.' небольшой презент.';
// If you need all forms, you can get all forms of a name:
var_dump(LastNamesDeclension::getCases($user_last_name));
/* Will produce something like
array(6) {
["nominative"]=>
string(12) "Иванов"
["genitive"]=>
string(14) "Иванова"
["dative"]=>
string(14) "Иванову"
["accusative"]=>
string(14) "Иванова"
["ablative"]=>
string(16) "Ивановым"
["prepositional"]=>
string(19) "об Иванове"
}
*/
You can decline geographical names like cities, countries, streets and so on. It has similar methods:
boolean isMutable($name)
- Check if name is mutable.string getCase($name, $case)
- Declines name.$case
is one constant ofmorphos\Cases
ormorphos\Russian\Cases
class constants.array getCases($word null)
- Declines name to all cases.
Example:
use morphos\Russian\GeographicalNamesDeclension;
echo 'Пора бы поехать в '.GeographicalNamesDeclension::getCase('Москва', 'винительный'); // Москву
// If you need all forms, you can get all forms of a name:
var_dump(GeographicalNamesDeclension::getCases('Саратов'));
/*
array(6) {
["nominative"]=>
string(14) "Саратов"
["genitive"]=>
string(16) "Саратова"
["dative"]=>
string(16) "Саратову"
["accusative"]=>
string(14) "Саратов"
["ablative"]=>
string(18) "Саратовом"
["prepositional"]=>
string(19) "о Саратове"
}
*/
Nouns declension class also have few similar methods but arguments are different:
boolean isMutable($word, bool $animateness = false)
- Check if noun is mutable.string getCase($word, $case, $animateness = false)
- Declines noun.array getCases($word, $animateness = false)
- Declines noun to all cases.
Usage:
use morphos\Russian\GeneralDeclension;
// Following code will return original word if it's immutable:
$form = GeneralDeclension::getCase('поле', 'родительный'); // поля
// Get all forms of a word at once:
var_dump(GeneralDeclension::getCases('линейка'));
/* Will produce something like
array(6) {
["nominative"]=>
string(14) "линейка"
["genitive"]=>
string(14) "линейки"
["dative"]=>
string(14) "линейке"
["accusative"]=>
string(14) "линейку"
["ablative"]=>
string(16) "линейкой"
["prepositional"]=>
string(17) "о линейке"
}
*/
Plurality
class have similar methods but not only:
string getCase($word, $case, $animateness = false)
- Pluralizes noun and declines.array getCases($word, $animateness = false)
- Pluralizes noun and declines to all cases.string pluralize($word, $count, $animateness = false)
- Pluralizes noun to coincide with numeral.
Usage:
use morphos\Russian\Plurality;
$word = 'дом';
echo 'Множественное число для '.$word.' - '.Plurality::getCase($word, 'именительный'); // дома
// Pluralize word and get all forms:
var_dump(Plurality::getCases('поле'));
/* Result will be like
array(6) {
["nominative"]=>
string(8) "поля"
["genitive"]=>
string(10) "полей"
["dative"]=>
string(10) "полям"
["accusative"]=>
string(8) "поля"
["ablative"]=>
string(12) "полями"
["prepositional"]=>
string(13) "о полях"
}
*/
// Also you can use shortcut
// Pluralize noun to use it with numeral
$count = 10;
echo $count.' '.morphos\Russian\pluralize('дом', $count, false); // result: 10 домов
All number creation classes are similar and have two common methods:
string getCase($number, $case, $gender = Gender::MALE)
- Get one case of a numeral for a number.array getCases($number, $gender = Gender::MALE)
- Get all cases of a numeral for a number.
$gender
is morphos\Gender::MALE
(or "m"
) or morphos\Gender::FEMALE
(or "f"
) or morphos\Gender::NEUTER
(or "n"
).
Creation of cardinal numerals (количественные числительные) in russian language.
Example:
use morphos\Gender;
use morphos\Russian\CardinalNumeral;
// Get text representation of a number:
$number = 4351;
$numeral = CardinalNumeral::getCase($number, 'именительный'); // четыре тысячи триста пятьдесят один
$numeral = CardinalNumeral::getCase($number, 'именительный', Gender::FEMALE); // четыре тысячи триста пятьдесят одна
If you need all forms, you can get all cases of a numeral:
var_dump(CardinalNumeral::getCases($number));
/* Will produce something like
array(6) {
["nominative"]=>
string(66) "четыре тысячи триста пятьдесят один"
["genitive"]=>
string(74) "четырех тысяч трехсот пятидесяти одного"
["dative"]=>
string(80) "четырем тысячам тремстам пятидесяти одному"
["accusative"]=>
string(66) "четыре тысячи триста пятьдесят один"
["ablative"]=>
string(90) "четырьмя тысячами тремястами пятьюдесятью одним"
["prepositional"]=>
string(81) "о четырех тысячах трехстах пятидесяти одном"
}
*/
Creation of ordinal numerals (порядковые числительные) in russian language.
Example:
use morphos\Gender;
use morphos\Russian\OrdinalNumeral;
// Get text representation of a number:
$number = 67945;
$numeral = OrdinalNumeral::getCase($number, 'именительный'); // шестьдесят семь тысяч девятьсот сорок пятый
$numeral = OrdinalNumeral::getCase($number, 'именительный', Gender::FEMALE); // шестьдесят семь тысяч девятьсот сорок пятая
If you need all forms, you can get all cases of a numeral:
var_dump(OrdinalNumeral::getCases($number));
/* Will produce something like
array(6) {
["nominative"]=>
string(81) "шестьдесят семь тысяч девятьсот сорок пятый"
["genitive"]=>
string(83) "шестьдесят семь тысяч девятьсот сорок пятого"
["dative"]=>
string(83) "шестьдесят семь тысяч девятьсот сорок пятому"
["accusative"]=>
string(81) "шестьдесят семь тысяч девятьсот сорок пятый"
["ablative"]=>
string(81) "шестьдесят семь тысяч девятьсот сорок пятым"
["prepositional"]=>
string(84) "о шестьдесят семь тысяч девятьсот сорок пятом"
}
*/
English morphology shortcuts:
morphos\English\pluralize($word, $count = 2)
morphos\English\CardinalNumeral::generate($number)
English class hierarchy:
morphos\
English\
CardinalNumeral
Plurality
Pluralization a word in English:
use morphos\English\Plurality;
echo '10 '.Plurality::pluralize('foot');
// result: 10 feet
All number creation classes are similar and have one common methods:
string generate($number)
- Generates a cardinal numeral for a number.
Creation of cardinal numerals in english language.
Create declension class object:
use morphos\English\CardinalNumeral;
Get text representation of a number:
$number = 4351;
$numeral = CardinalNumeral::generate($number); // four thousand, three hundred fifty-one
See CONTRIBUTING.md for this.