Skip to content

Commit 25018b8

Browse files
committed
[ENH] A dirty minor update: functions from petrovich-java copied yet not fully adapted
1 parent e7da260 commit 25018b8

12 files changed

+1283
-1228
lines changed

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 Petrovich Developers
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1-
The MIT License (MIT)
2-
3-
Copyright (c)
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy of
6-
this software and associated documentation files (the "Software"), to deal in
7-
the Software without restriction, including without limitation the rights to
8-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9-
the Software, and to permit persons to whom the Software is furnished to do so,
10-
subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17-
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18-
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19-
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20-
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1+
![Petrovich](petrovich.png) pytrovich
2+
==========================================
3+
4+
__pytrovich__ is a library which inflects Russian names to given grammatical case. It supports first names, last names and middle names inflections.
5+
6+
__pytrovich__ is a Python implementation of Petrovich ruby gem (petrovich-ruby -> petrovich-java -> pytrovich).
7+
8+
## Usage
9+
10+
```python
11+
maker = PetrovichDeclinationMaker.getInstance();
12+
13+
maker.make(NamePart.FIRSTNAME, Gender.MALE, Case.GENITIVE, "Иван"); //Ивана
14+
maker.make(NamePart.LASTNAME, Gender.MALE, Case.INSTRUMENTAL, "Иванов"); //Ивановым
15+
maker.make(NamePart.MIDDLENAME, Gender.FEMALE, Case.DATIVE, "Ивановна"); //Ивановне
16+
```
17+
18+
### Custom rule file
19+
20+
You can replace default rules file with some custom one. Only JSON format supported by now.
21+
```python
22+
PetrovichDeclinationMaker maker = PetrovichDeclinationMaker.getInstance("/path/to/custom/rules.file.json");
23+
```
24+
25+
### Accuracy
26+
27+
You can read about accuracy statistics in [petrovich-ruby](https://github.com/petrovich/petrovich-ruby#Оценка-аккуратности) project front page
28+
29+
### License
30+
31+
This project is available under MIT license.

petrovich.png

1.72 KB
Loading

pytrovich/__main__.py

-23
This file was deleted.

pytrovich/deserialization.py

-37
This file was deleted.

pytrovich/meta.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
authors_string = ', '.join(authors)
1010
emails = ['[email protected]']
1111
license = 'MIT'
12-
copyright = '2017 ' + authors_string
13-
url = 'https://github.com/petrovich/pytrovich'
12+
copyright = '2020 ' + authors_string
13+
url = 'https://github.com/alexeyev/pytrovich'

pytrovich/models.py

+52-6
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,73 @@
33

44
class Rule(object):
55

6-
def __init__(self, gender, mods, test):
6+
def __init__(self, gender: str, mods: list, test: list):
7+
"""
8+
:param gender: grammatical gender
9+
:param mods: modifications rules list
10+
:param test: search rules list
11+
"""
712
self.gender = gender
813
self.mods = mods
914
self.test = test
1015

16+
def serialize(self):
17+
return {"gender": self.gender,
18+
"mods": self.mods,
19+
"test": self.test}
20+
21+
@staticmethod
22+
def parse(o: dict):
23+
return Rule(gender=o["gender"], mods=o["mods"], test=o["test"])
24+
1125

1226
class Name(object):
1327

14-
def __init__(self, exceptions, suffixes):
28+
def __init__(self, exceptions: list, suffixes: list):
1529
"""
16-
:param exceptions: list(Rule):
17-
:param suffixes: list(Rule)
30+
:param exceptions: list(Rule):
31+
:param suffixes: list(Rule)
1832
"""
1933
self.exceptions = exceptions
2034
self.suffixes = suffixes
2135

36+
def serialize(self):
37+
return {"exceptions": [e.serialize() for e in self.exceptions],
38+
"suffixes": [s.serialize() for s in self.suffixes]}
39+
40+
@staticmethod
41+
def parse(o: dict):
42+
return Name(exceptions=[Rule.parse(e) for e in o["exceptions"]],
43+
suffixes=[Rule.parse(s) for s in o["suffixes"]])
44+
2245

2346
class Root(object):
2447

2548
def __init__(self, firstname, lastname, middlename):
26-
2749
self.firstname = firstname
2850
self.lastname = lastname
29-
self. middlename = middlename
51+
self.middlename = middlename
52+
53+
def serialize(self):
54+
return {"firstname": self.firstname.serialize(),
55+
"lastname": self.lastname.serialize(),
56+
"middlename": self.middlename.serialize()}
57+
58+
@staticmethod
59+
def parse(a: dict):
60+
# a = json.loads(s)
61+
return Root(firstname=Name.parse(a["firstname"]),
62+
lastname=Name.parse(a["lastname"]),
63+
middlename=Name.parse(a["middlename"]))
64+
65+
66+
if __name__ == "__main__":
67+
import json
68+
69+
r = Rule("gender", ["a", "b", "c"], ["d", "e", "f"])
70+
71+
# print(jsonpickle.encode(r))
72+
# a = json.loads(jsonpickle.encode(r))
73+
# a.pop("py/object", None)
74+
# print(a)
75+
# print(jsonpickle.decode(json.dumps(a), classes=[Rule]))

0 commit comments

Comments
 (0)