-
Notifications
You must be signed in to change notification settings - Fork 0
/
pnv_person.py
81 lines (69 loc) · 2.37 KB
/
pnv_person.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
import re
import sys
class PnvPerson:
categories = [
"prefix",
"givenName",
"patronym",
"givenNameSuffix",
"infixTitle",
"surnamePrefix",
"baseSurname",
"trailingPatronym",
"honorificSuffix",
"disambiguatingDescription"
]
combinations = [
"surname",
"literalName",
"firstName",
"infix",
"suffix"
]
def __init__(self):
self.naam = {}
def add(self,categorie,value):
if categorie in self.naam:
self.naam[categorie] += " {}".format(value)
else:
self.naam[categorie] = value
def get(self,categorie):
if categorie in self.combinations:
return self.combine(categorie)
if categorie not in self.categories:
try:
return self.naam[categorie]
except KeyError:
pass
return "unknown name part: {}".format(categorie)
def __get(self, categorie):
try:
return self.naam[categorie]
except KeyError:
return ""
def combine(self,categorie):
if(categorie == "literalName"):
result = ""
for cat in self.categories:
if cat in self.naam:
result = result + " {}".format(self.naam[cat])
return self.clean(result)
elif(categorie == "surname"):
return self.clean(f"{self.naam['surnamePrefix']} {self.naam['baseSurname']}")
elif(categorie == "firstName"):
return self.clean(f"{self.__get('givenName')} {self.__get('patronym')} {self.__get('givenNameSuffix')}")
elif(categorie == "infix"):
return self.clean(f"{self.__get('InfixTitle')} {self.__get('surnamePrefix')}")
elif(categorie == "suffix"):
return self.clean(f"{self.__get('trailingPatronym')} {self.__get('honorificSuffix')} {self.__get('disambiguatingDescription')}")
def clean(self,text):
pattern = re.compile(" +")
pattern_2 = re.compile("' ")
result = re.sub(pattern," ",text)
result = re.sub(pattern_2,"'",result)
return result.strip()
def to_h(self):
return self.naam
def to_s(self):
return self.get('literalName')