Do you often have to deal with declining usernames in Russian? When sending letters, when displaying pages and mentions, when generating advertisements? Most likely, you have to dodge and write all the messages in the nominative case -
Ivanov Petr Sergeevich .
It is not always convenient, not always beautiful, not always appropriate. The Russian language is endowed with a rich morphology, which somewhat complicates its automatic processing. Everyone knows that
anthroponyms , as nouns rely on, obey all the rules of word formation.
To solve this problem when using Ruby, there is
Petrovich , a convenient lightweight library for
automagical declination of Russian names, surnames and patronymic names.
')

The essence
The Petrovich algorithm is based on the manual derivation rule base. It will be very easy for an application programmer to work with the library: the input is a word in
canonical form , the output is the word of the requested kind and case.
Using
The basic syntax is simple enough to understand.
petrovich = Petrovich.new(:male) petrovich.lastname('', :dative)
As you can see, there are two parameters that describe the behavior of the analyzer: the floor is passed to the constructor of the
Petrovich
class, the case is passed to the second parameter of the declination method.
First, the gender of the name. Petrovich considers three options:
- male gender
:male
; - female,
:female
; - “Androgynous” gender
:androgynous
, in which the shape of the last name does not depend on the gender of the person. For example, Prokopenko .
Secondly, the required case. Valid cases that change the word are described below in the table.
Death case | Characterizing question | Designation |
---|
Genitive | Whom? What? | genitive |
Dative | To whom? What | dative |
Accusative | Whom? What? | accusative |
Instrumental | By whom? Than? | instrumental |
Prepositional | About whom? About what? | prepositional |
It is possible to mix the module
Petrovich::Extension
into any class. This is especially useful when using ActiveRecord and similar ORM.
class User < ActiveRecord::Base include Petrovich::Extension petrovich :firstname => :my_firstname, :middlename => :my_middlename, :lastname => :my_lastname, :gender => :my_gender def my_firstname '' end def my_middlename '' end def my_lastname '--' end
Using the
petrovich
method, methods representing the last name, first name and patronymic are indicated. In this example, it is indicated that the method
my_firstname
represents the name, the method
my_lastname
represents the last name, the method
my_middlename
represents the middle name. Nothing prevents the module from being mixed into a regular class; everything will work just as well. Methods with the cases described in the table will be added to the target class.
user = User.new user.my_firstname
Some features of Russian spelling are taken into account quite well.
Accuracy rating
For the sake of decency, an assessment was made of the accuracy of the declension of words. As a reference, a list of surnames and their forms was taken from the
AOT morphological dictionary . Accuracy was evaluated based on processing 88,314 examples using the formula

,
where
case is a case,
gender is a gender,
V case, gender is the number of correct inflection for the specified case and gender,
N case, gender is the total number of examples for the specified case and gender.
At the time of this writing, an average accuracy of 99.6614% is observed based on processing 88,314 examples. Details are given in the table below. This information may be useful when using Petrovich in combat.
Gender / Case | Genitive | Dative | Accusative | Instrumental | Prepositional |
---|
Male | 99.7137% | 99.7386% | 99.7635% | 97.9858% | 99.7261% |
---|
Female | 99.9102% | 99.9401% | 99.9701% | 99.4636% | 99.9401% |
---|
Requisites
The repository on GitHub is located at
petrovich / petrovich-ruby . The library is distributed under the MIT License. The authors of the library are
Andrei Kozlov (
Bonch ) and
Dmitry Ustalov (
dustalov ).
To install Petrovich and use it in your own applications, just run
gem install petrovich
or add the appropriate line in the
Gemfile .
Web interface
It's nice that
Andrei Sitnik (
Iskin ) from Evil Martians made a cool interface that allows you to play with Petrovich directly from the browser.

Currently, the interface is available at
http://petrovich.nlpub.ru/ .
Porting
It would be great if developers working with other programming languages ​​would have ported this library to their languages. We are happy to answer any questions about Petrovich. In general, you can start porting with our
rule base .
Thank you for reading to the end!
The illustrations with Petrovich belong to Andrei Bilzho, the author of comics about Petrovich .