Skip to main content

Pytrovich

pytrovich is a Python 3.9+ port of petrovich library which inflects Russian names to a given grammatical case. It supports first names, last names and middle names inflections. Since version 0.0.2, gender detection is also available.

It is STRONGLY RECOMMENDED to use pytrovich VERSION >=0.1.0

3–5× faster lookups via a suffix trie, 800× faster repeated construction via parsed-rules caching, deterministic gender output, correct handling of all-caps / Ё / indeclinable names that previously failed silently, and refreshed rules data covering 24 upstream fixes from 2020–2024. Upgrade because the old version returned wrong answers on a non-trivial slice of real Russian names (try make(LASTNAME, MALE, GEN, "ИВАНОВ") or make(FIRSTNAME, MALE, GEN, "Пётр") on both --- the new one is correct, the old one isn't), flapped between gender predictions across runs, and crashed on unknown names; the new one is faster, deterministic, properly typed (py.typed ships), and benchmarked at >99% accuracy across 270k gold-standard inflections.

petrovich-java was the main inspiration.

The alternative (earlier) port: Petrovich (@alexeyev was not aware of it at the time of porting petrovich to Python). The only meaningful difference we have found is that it does not support gender detection.

Python 3x PyPI version Downloads

Installation

Should be as simple as that

pip install pytrovich

Usage

Inflection

from pytrovich.enums import NamePart, Gender, Case
from pytrovich.maker import PetrovichDeclinationMaker

maker = PetrovichDeclinationMaker()
print(maker.make(NamePart.FIRSTNAME, Gender.MALE, Case.GENITIVE, "Иван"))  # Ивана
print(maker.make(NamePart.LASTNAME, Gender.MALE, Case.INSTRUMENTAL, "Иванов"))  # Ивановым
print(maker.make(NamePart.MIDDLENAME, Gender.FEMALE, Case.DATIVE, "Ивановна"))  # Ивановне

Inflection follows the canonical petrovich-ruby reference semantics, verified row-for-row against the petrovich-eval corpus:

  • hyphenated double names decline per component (Салтыков-Щедрин → dative Салтыкову-Щедрину);
  • exception rules match whole words only (the surname Ельцин keeps the regular -ин declension instead of the Цин exception);
  • the optional known_gender flag (default True) marks the supplied gender as an explicit fact. Pass known_gender=False when the gender came out of PetrovichGenderDetector to get the Ruby reference's looser detected-gender rule filter:
gender = detector.detect(lastname="Цой")  # Gender.MALE
maker.make(NamePart.LASTNAME, gender, Case.DATIVE, "Цой", known_gender=False)  # Цою

Gender detection

from pytrovich.detector import PetrovichGenderDetector

detector = PetrovichGenderDetector()
print(detector.detect(firstname="Иван"))  # Gender.MALE
print(detector.detect(firstname="Иван", middlename="Семёнович"))  # Gender.MALE
print(detector.detect(firstname="Арзу", middlename="Лутфияр кызы"))  # Gender.FEMALE

Custom rule file

You can replace default rules file with some custom one. Only JSON format is supported.

maker = PetrovichDeclinationMaker("/path/to/custom/rules.file.json")

E.g. if pytrovich fails on PetrovichDeclinationMaker creation, one may consider downloading rules.json directly from petrovich-rules repo as a fix (please create an issue if that actually happens).

How to cite

Not neccessary, but greatly appreciated, if you use this work.

@misc{Pytrovich,
  title     = {{petrovich/pytrovich: Python3 port of Petrovich, an inflector for Russian anthroponyms}},
  year      = {2020},
  url       = {https://github.com/petrovich/pytrovich},
  language  = {english},
}

More info

For more information on the project please refer to other petrovich repos.

License

This project is available under MIT license.

Metadata

Release files for pytrovich 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytrovich 0.2.1
File Size Uploaded Provenance Actions
pytrovich-0.2.1.tar.gz 49.7 kB Without provenance Details for pytrovich-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytrovich 0.2.1
File Interpreter ABI Platform Actions
pytrovich-0.2.1-py3-none-any.whl Python 3 none any Details for pytrovich-0.2.1-py3-none-any.whl

Total release size: 81.8 kB

Release files / pytrovich-0.2.1.tar.gz

Download URL pytrovich-0.2.1.tar.gz
Size 49.7 kB
Tags Source
SHA-256 checksum
How to use checksums
44f3a55fd76eef3ad91af73e6dbf4330dcdbfe98ba251200331acd48a270368f
BLAKE2b-256 checksum
How to use checksums
b4b499f6468975b1cdc28ba3718b39232661240dde117cd87fc03322ea7e7760
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.18

Release files / pytrovich-0.2.1-py3-none-any.whl

Download URL pytrovich-0.2.1-py3-none-any.whl
Size 32.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4644727b9ee21c235f0341ebaf9069af6ab052d8aee8d3b11eedf79e411f514c
BLAKE2b-256 checksum
How to use checksums
80ca5348732f96dac3595371cfd73a6f32f80337140652b53cfe795fc63db41c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.18

Security for pytrovich 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page