Morphos - a morphological solution written completely in PHP.
Supported languages:
- English - pluralization.
- Russian - pluralization and declension.
- Download library through composer:
composer require "wapmorgan/morphos"
-
Create declension class object:
$dec = new morphos\RussianNamesDeclension();
-
Check whether there are forms for this name:
var_dump($dec->hasForms('Иван', morphos\RussianNamesDeclension::MAN)); // true
-
Get all forms of a name:
var_dump($dec->getForms('Иван', morphos\RussianNamesDeclension::MAN)); // array[] {...}
-
Get one form of a name:
var_dump($dec->getForm('Иван', morphos\RussianCases::RODIT_2, morphos\RussianNamesDeclension::MAN)); // Ивана
-
All forms for this name:
array(6) { ["nominativus"]=> string(8) "Иван" ["genetivus"]=> string(10) "Ивана" ["dativus"]=> string(10) "Ивану" ["accusative"]=> string(10) "Ивана" ["ablativus"]=> string(12) "Иваном" ["praepositionalis"]=> string(15) "об Иване" }
-
Create declension class object:
$dec = new morphos\RussianGeneralDeclension();
-
Check whether there are forms for this word (second arg is an animateness):
var_dump($dec->hasForms('поле', false));
-
Get all forms of a word:
var_dump($dec->getForms('поле', false)); // array[] {...}
-
Get one form of a word:
var_dump($dec->getForm('поле', false, morphos\RussianCases::RODIT_2)); // поля
-
All forms for this word:
array(6) { ["nominativus"]=> string(8) "поле" ["genetivus"]=> string(8) "поля" ["dativus"]=> string(8) "полю" ["accusative"]=> string(8) "поле" ["ablativus"]=> string(10) "полем" ["praepositionalis"]=> string(8) "поле" }
-
Get all forms of a plural word:
var_dump($dec->pluralizeAllDeclensions('поле', false)); // array[] {...}
-
All forms for this plural word:
array(6) { ["nominativus"]=> string(8) "поля" ["genetivus"]=> string(10) "полей" ["dativus"]=> string(10) "полям" ["accusative"]=> string(8) "поля" ["ablativus"]=> string(12) "полями" ["praepositionalis"]=> string(10) "полях" }
Declension of general words is very complicated and may fail.
-
Cases in russian language:
- morphos\RussianCases::IMENIT_1
- morphos\RussianCases::RODIT_2
- morphos\ 77C5 RussianCases::DAT_3
- morphos\RussianCases::VINIT_4
- morphos\RussianCases::TVORIT_5
- morphos\RussianCases::PRODLOJ_6
-
Pluralization a word in Russian:
$plu = new morphos\RussianPlurality(); $word = 'дом'; $count = 10; echo sprintf("%d %s", $count, $plu->pluralize($word, $count, false)); // last argument - animateness
prints
10 домов
-
Pluralization a word in English:
$plu = new morphos\EnglishPlurality(); $word = 'foot'; $count = 10; echo sprintf("%d %s", $count, $plu->pluralize($word, $count));
prints
10 feet
Morphos are open for additions and improvements.
Addition a new language is simple: create the class inheriting one of basic classes and realize abstract methods from it.
Here is a list of basic classes:
Class for names declension.
-
Checks, whether there are rules for this name.
abstract public function hasForms($name, $gender);
-
Generates all forms of a name.
abstract public function getForms($name, $gender);
-
Generates one form of a name.
abstract public function getForm($name, $form, $gender);
-
Checks, whether there are rules for this word.
public function hasForms($word, $animate = false);
-
Generates all forms of a word.
public function getForms($word, $animate = false);
-
Generates one form of a word.
public function getForm($word, $form, $animate = false);
For simple access to functions of string processing there are some functions in morphos
namespace:
set_encoding()
- Sets encoding for using in morphos/* functions.length()
- Calculates count of characters in string.slice()
- Slices string like python.lower()
- Lower case.upper()
- Upper case.name()
- Name case. (ex: Thomas Lewis)