Morphos - a morphological decision written completely in PHP.
Supported languages:
- English - pluralization.
- Russian - pluralization and declension.
- Download library through composer:
composer require "wapmorgan/morphos"
-
Declension of personal names in russian language:
$array = array('Иван', 'Игорь', 'Андрей', 'Фома', 'Никита', 'Илья'); $dec = new morphos\RussianNamesDeclension(); foreach ($array as $name) { if ($dec->hasForms($name)) { // Get all forms of a name $forms = $dec->getForms($name, morphos\RussianNamesDeclension::MAN); // you can use 'm' или 'w' instead of class constant // Get genetive form of a name $form = $dec->getForms($name, morphos\RussianCases::RODIT_3, 'm'); } }
-
Declension of general words in russian language:
$word = 'поле'; $dec = new morphos\RussianGeneralDeclension(); if ($dec->hasForms($word)) {// this methods always returns true at this moment # Singular declension // Get all forms of a word $forms = $dec->getForms($word, false); // second argument is an animateness // Get genetive form of a word $form = $dec->getForm($word, false, morphos\RussianCases::RODIT_3); # Plural declension // Get all forms of a word $forms = $dec->pluralizeAllDeclensions($word, false); // There's no method for getting one form }
Declension of general words is very complicated.
-
Cases in russian language:
- morphos\RussianCases::IMENIT_1
- morphos\RussianCases::RODIT_2
- morphos\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));
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)