Morphos

A morphological solution written completely in the PHP language. Supports Russian and English. Provides classes to decline personal names, decline and pluralize nouns and generate cardinal and ordinal numerals.

Morphos

A morphological solution written completely in PHP.

Composer package Latest Stable Version License Total Downloads Latest Unstable Version

Tests & Quality: Build Status Scrutinizer Code Quality

  1. Installation
  2. Quick start
  3. Russian
    1. Personal names
    2. Nouns
    3. Numerals
      1. Cardinal numerals
      2. Ordinal numerals
    4. Cases
  4. English
    1. Pluralization
    2. Numeral creation
      1. Cardinal numerals
  5. Addition of new languages

Installation

Adapters:

Quick Start

Russian

Russian morphology shortcuts:

Russian class hierarchy:

morphos\
        Russian\
                CardinalNumeral
                Cases
                GeneralDeclension
                FirstNamesDeclension
                LastNamesDeclension
                MiddleNamesDeclension
                OrdinalNumeral
                Plurality

Personal names

To compound all declension functionality in one call there is nameCase function:

string|array name($name, $case = null, $gender = null)

Arguments:

There is three declension classes:

All declension classes are similar and have four common methods:

$gender is NamesDeclension::MAN or NamesDeclension::WOMAN 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 just specify gender if you want to declinate first names.

Examples:

FirstNamesDeclension

$dec = new morphos\Russian\FirstNamesDeclension();

// Get any form of a name:
// for example, let it be Иван
$user_name = 'Иван';

$name = $dec->getCase($user_name, 'родительный'); // Ивана

// If you need all forms, you can get all forms of a name:
var_dump($dec->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

$dec = new morphos\Russian\MiddleNamesDeclension();

// Get any form of a name:
// for example, let it be Сергеевич
$user_name = 'Сергеевич';

$name = $dec->getCase($user_name, 'родительный'); // Сергеевича

// If you need all forms, you can get all forms of a name:
var_dump($dec->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

$dec = new morphos\Russian\LastNamesDeclension();

// Get any case of a last name
$user_last_name = 'Иванов';

$dative_last_name = $dec->getCase($user_last_name, 'дательный'); // Иванову

echo 'Мы хотим подарить товарищу '.$dative_last_name.' небольшой презент.';

// If you need all forms, you can get all forms of a name:
var_dump($dec->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) "об Иванове"
  }
*/

Nouns

Nouns declension

Nouns declension class also have few similar methods but arguments are different:

Usage:

use morphos\Russian\GeneralDeclension;

$dec = new GeneralDeclension();

// Following code will return original word if it's immutable:
$form = $dec->getCase('поле', 'родительный');

// Get all forms of a word at once:
var_dump($dec->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) "о линейке"
  }
*/

Nouns pluralization

Plurality class have similar methods but not only:

Usage:

use morphos\Russian\Plurality;

$plu = new Plurality();

$word = 'дом';
echo 'Множественное число для '.$word.' - '.$plu->getCase($word, 'именительный'); // дома

// Pluralize word and get all forms:
var_dump($plu->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 домов

Numeral creation

All number creation classes are similar and have three common methods:

$gender is one of morphos\NumeralCreation constants: MALE or FEMALE or NEUTER.

Cardinal numerals (CardinalNumeral)

Creation of cardinal numerals in russian language.

Create generation class object:

use morphos\NumeralCreation;
use morphos\Russian\CardinalNumeral;

$cardinal = new CardinalNumeral();

Get text representation of a number:

$number = 4351;

$numeral = $cardinal->getCase($number, 'именительный'); // четыре тысячи триста пятьдесят один
$numeral = $cardinal->getCase($number, 'именительный', NumeralCreation::FEMALE); // четыре тысячи триста пятьдесят одна

If you need all forms, you can get all cases of a numeral:

var_dump($cardinal->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) "о четырех тысячах трехстах пятидесяти одном"
  }
*/

Generate numeral of a number:

use morphos\Russian\CardinalNumeral;

echo CardinalNumeral::generate(4351);
// result: четыре тысячи триста пятьдесят один

Ordinal numerals (OrdinalNumeral)

Creation of ordinal numerals in russian language.

Create generation class object:

use morphos\NumeralCreation;
use morphos\Russian\OrdinalNumeral;

$ordinal = new OrdinalNumeral();

Get text representation of a number:

$number = 67945;

$numeral = $ordinal->getCase($number, 'именительный'); // шестьдесят семь тысяч девятьсот сорок пятый
$numeral = $ordinal->getCase($number, 'именительный', NumeralCreation::FEMALE); // шестьдесят семь тысяч девятьсот сорок пятая

If you need all forms, you can get all cases of a numeral:

var_dump($ordinal->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) "о шестьдесят семь тысяч девятьсот сорок пятом"
    }
*/

Generate numeral of a number:

use morphos\Russian\OrdinalNumeral;

echo CardinalNumeral::generate(67945);
// result: шестьдесят семь тысяч девятьсот сорок пятый

Cases (Cases)

Cases in russian language:

English

English morphology shortcuts:

English class hierarchy:

morphos\
        English\
                CardinalNumeral
                Plurality

Pluralization (Plurality)

Pluralization a word in English:

use morphos\English\Plurality;

echo '10 '.Plurality::pluralize('foot');
// result: 10 feet

Numeral creation

All number creation classes are similar and have one common methods:

Cardinal numbers (CardinalNumeral)

Creation of cardinal numerals in russian 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

Addition of new languages

See CONTRIBUTING.md for this.