-
Notifications
You must be signed in to change notification settings - Fork 1
/
cleaners.py
195 lines (166 loc) · 6.38 KB
/
cleaners.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import logging
import re
from typing import Union
from nkcr_exceptions import BadItemException
name_to_nkcr: dict = {}
language_dict: dict = {}
cachedData = {}
log = logging.getLogger(__name__)
def clean_last_comma(string: str) -> str:
if string.endswith(','):
return string[:-1]
return string
def clean_qid(string: str) -> str:
string = string.replace(')', '').replace('(', '')
first_letter = string[0]
if first_letter.upper() != 'Q':
raise BadItemException(string)
regex = r"Q[0-9]+"
match = re.search(regex, string, re.IGNORECASE)
if not match:
raise BadItemException(string)
return string
def prepare_orcid_from_nkcr(orcid: str) -> str:
# https://pythonexamples.org/python-split-string-into-specific-length-chunks/
orcid = orcid.replace(' ', '')
regex = u"^(\d{4}-){3}\d{3}(\d|X)$"
match = re.search(regex, orcid, re.IGNORECASE)
if not match:
return ''
else:
return orcid
def prepare_occupation_from_nkcr(occupation_string: str) -> Union[str, list]:
nkcr_to_qid = name_to_nkcr
occupations = []
# log_with_date_time(occupation_string)
if type(occupation_string) == str:
if occupation_string.strip() == '':
return occupations
splitted_occupations = occupation_string.strip().split('|')
try:
# occupations = [nkcr_to_qid[occupation] for occupation in splitted_occupations]
occupations = []
for occupation in splitted_occupations:
if occupation in nkcr_to_qid:
occupations.append(nkcr_to_qid[occupation])
else:
log.warning('not found occupation: ' + occupation)
except KeyError as e:
log.warning('not found occupation: ' + str(e))
# for occupation in splitted_occupations:
# try:
# occupation_qid = nkcr_to_qid[occupation]
# occupations.append(clean_qid(occupation_qid))
# except KeyError as e:
# print(e)
# pass
return occupations
def prepare_language_from_nkcr(language_string: str) -> Union[str, list]:
nkcr_to_qid = language_dict
languages = []
# log_with_date_time(occupation_string)
if type(language_string) == str:
if language_string.strip() == '':
return languages
splitted_languages = language_string.strip().split('$')
try:
# languages = [nkcr_to_qid[language] for language in splitted_languages]
languages = []
for language in splitted_languages:
if language in nkcr_to_qid:
languages.append(nkcr_to_qid[language])
else:
log.warning('not found language: ' + language)
except KeyError as e:
log.warning('not found language: ' + str(e))
# for occupation in splitted_occupations:
# try:
# occupation_qid = nkcr_to_qid[occupation]
# occupations.append(clean_qid(occupation_qid))
# except KeyError as e:
# print(e)
# pass
return languages
def prepare_places_from_nkcr(place_string: str) -> Union[str, list]:
nkcr_to_qid = name_to_nkcr
places: list = []
# log_with_date_time(occupation_string)
if type(place_string) == str:
if place_string.strip() == '':
return places
if ('|' in place_string and '$' not in place_string):
splitted_places = place_string.strip().split('|')
else:
splitted_places = place_string.strip().split('$')
regex = r"(.*?),\W*(.*)"
subst = "\\1 (\\2)"
corrected_splitted_places: list = []
for place in splitted_places:
if (place.strip().find('(') != -1):
corrected_splitted_places.append(place)
else:
result = re.sub(regex, subst, place.strip(), 0, re.MULTILINE)
if result:
corrected_splitted_places.append(result)
else:
corrected_splitted_places.append(place)
try:
# places = [nkcr_to_qid[corrected_place] for corrected_place in corrected_splitted_places]
places = []
for corrected_place in corrected_splitted_places:
if corrected_place in nkcr_to_qid:
places.append(nkcr_to_qid[corrected_place])
else:
log.warning('not found place: ' + corrected_place)
except KeyError as e:
log.warning('not found place: ' + str(e))
# for occupation in splitted_occupations:
# try:
# occupation_qid = nkcr_to_qid[occupation]
# occupations.append(clean_qid(occupation_qid))
# except KeyError as e:
# print(e)
# pass
return places
def prepare_isni_from_nkcr(isni: str) -> str:
# https://pythonexamples.org/python-split-string-into-specific-length-chunks/
isni = isni.replace(' ', '')
regex = u"^(\d{16})$"
match = re.search(regex, isni, re.IGNORECASE)
if not match:
return ''
else:
n = 4
str_chunks = [isni[i:i + n] for i in range(0, len(isni), n)]
return ''.join(str_chunks)
def prepare_column_of_content(column: str, row) -> Union[str, Union[str, list]]:
column_to_method_dictionary = {
'0247a-isni': prepare_isni_from_nkcr,
'0247a-orcid': prepare_orcid_from_nkcr,
'374a': prepare_occupation_from_nkcr,
'372a': prepare_occupation_from_nkcr,
'370a': prepare_places_from_nkcr,
'370b': prepare_places_from_nkcr,
'370f': prepare_places_from_nkcr,
'377a': prepare_language_from_nkcr,
}
return column_to_method_dictionary[column](row[column])
def resolve_exist_claims(column: str, wd_data: dict) -> Union[str, list]:
claims = []
if column == '0247a-isni':
claims = wd_data['isni']
if column == '0247a-orcid':
claims = wd_data['orcid']
if column == '374a':
claims = wd_data['occup']
if column == '372a':
claims = wd_data['field']
if column == '370a':
claims = wd_data['birth']
if column == '370b':
claims = wd_data['death']
if column == '370f':
claims = wd_data['work']
if column == '377a':
claims = wd_data['language']
return claims