pytrovich: a Python port of an inflector for Russian anthroponyms developed by petrovich team: https://github.com/petrovich
Project description
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.
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_genderflag (defaultTrue) marks the supplied gender as an explicit fact. Passknown_gender=Falsewhen the gender came out ofPetrovichGenderDetectorto 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytrovich-0.2.0.tar.gz.
File metadata
- Download URL: pytrovich-0.2.0.tar.gz
- Upload date:
- Size: 49.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4501b8b467127b6bbe2583a56631fdc7cdd7dbac15058319321c147e94614ee0
|
|
| MD5 |
b2f786dfdca3e2462fc6060fb5c97b3f
|
|
| BLAKE2b-256 |
05058230874b9c95ba68586757721508d51844399ee357fe7da0655c6088cf86
|
File details
Details for the file pytrovich-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pytrovich-0.2.0-py3-none-any.whl
- Upload date:
- Size: 31.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb5fbdaed2a33738896d70156ffde5f74ff5a60375f47ced697b8ae76507ed6a
|
|
| MD5 |
a7ce32ade89a526402881084cf611a55
|
|
| BLAKE2b-256 |
3ca5f029c2f0f78555b6471a2103ee614e902e7a2e03b47437da7b782d8ce1b9
|