Repository navigation
Expand file tree
/
Copy pathcommon.py
More file actions
400 lines (309 loc) · 9.24 KB
/
Copy pathcommon.py
File metadata and controls
400 lines (309 loc) · 9.24 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""
A collection of commonly use data manipulation procedures.
"""
import json
import os
import codecs
from simplejson import JSONDecodeError
import sys
import requests
import glob
from os import walk
SLASH = '/'
UNDERSCORE = '_'
BLANK = ' '
JSON_EXT = '.json'
XML_EXT = '.xml'
# Mapped authors CSV file
AUTHOR_NAME_COL = 3
VIAF_ID_COL = 7
MUSICBRAINZ_ID_COL = 13
GND_HEADER = 'gnd'
AUTHOR_NAME_HEADER = 'name'
FREEBASE_HEADER = 'freebase'
# Viaf compositions CSV file
VIAF_COMPOSITIONS_CSV_AUTHOR_ID_COL = 0
VIAF_COMPOSITIONS_CSV_VIAF_WORK_ID_COL = 2
VIAF_COMPOSITIONS_CSV_COMPOSITION_TITLE_COL = 3
# JSON folders
WIKIDATA_AUTHOR_DATA_DIR = 'data/wikidata_author_data_dir'
RESULT = 'result'
AUTHOR_NAME = 'author name'
WORKS_JSON = 'works'
WORK_COUNT_JSON = 'work-count'
RECORDINGS_JSON = 'recordings'
ALIASES_JSON = 'aliases'
TITLE_JSON = 'title'
NAME_JSON = 'name'
ID_JSON = 'id'
RELATIONS_JSON = 'relations'
# Freebase
FREEBASE_PREFIX = '/m/'
# Neo4j
RELATION_AUTHOR_TO_COMPOSITION = 'has_composition'
COMPOSITION_AUTHOR_ID_HEADER = 'author id'
COMPOSITION_TITLE_HEADER = 'title'
AUTHOR_VIAF_ID_HEADER = 'viaf'
wikidata_author_fieldnames = [
'gnd'
, 'wikidata'
, 'onb'
, 'name'
, 'genre'
, 'occupation'
, 'freebase'
, AUTHOR_VIAF_ID_HEADER
, 'bnf'
, 'nkc'
, 'nta'
, 'imslp'
, 'dbpedia'
, 'music_brainz_artist_id'
]
freebase_wikidata_mapping = [
'Freebase ID'
, 'Wikidata URL'
, 'Label'
, 'GND'
]
viaf_compositions_fieldnames = [
COMPOSITION_AUTHOR_ID_HEADER
, AUTHOR_NAME
, 'work id'
, COMPOSITION_TITLE_HEADER
]
viaf_musicbrainz_compositions_mapping_fieldnames = [
'viaf id'
, 'musicbrainz id'
]
viaf_compositions_count_fieldnames = [
AUTHOR_NAME
, 'count'
]
musicbrainz_compositions_count_fieldnames = [
'id'
, AUTHOR_NAME
, 'count'
]
comprehensive_compositions_count_fieldnames = [
GND_HEADER
, AUTHOR_NAME_HEADER
, 'viaf compositions count'
, 'freebase compositions count'
, 'europeana references count by name'
, 'europeana references count by gnd'
, 'europeana references count by gnd and sameAs'
, 'europeana references count by name for all variations'
]
sameas_fieldnames = [
'onb id'
, 'author name'
, 'gnd url'
, 'sameAs'
]
musicbrainz_works_and_recordings_fieldnames = [
'id'
, 'author name'
, 'title'
, 'alternative names'
]
map_compositions_fieldnames = [
'title'
, 'author name'
, 'Wikidata ID'
, 'VIAF ID'
, 'MUSICBRAINZ ID'
]
map_band_fieldnames = [
'title'
, 'Wikidata ID'
, 'Internet Archive ID'
, 'Musicbrainz ID'
]
def progress(progress=0):
progress = round(progress * 100)
print("\n*** Progress: {0}% ***".format(progress))
# I/O handling
def read_records(inputfiles):
for filename in inputfiles:
with codecs.open(filename, 'r', 'utf-8') as in_file:
data = in_file.read()
yield (filename, data)
def ensure_directory(outputdir):
"""Make sure that output directory exists"""
if not os.path.exists(outputdir):
print("Creating directory", outputdir)
os.makedirs(outputdir)
def write_json_file(outputdir, filename, data):
ensure_directory(outputdir)
with codecs.open(outputdir + SLASH + filename, "w", 'utf-8') as out_file:
json.dump(data, out_file, sort_keys=True, indent=4,
ensure_ascii=False, encoding='utf-8')
def write_txt_file_from_list(outputdir, filename, itemlist):
ensure_directory(outputdir)
file = open(outputdir + SLASH + filename, "w")
file.write("\n".join(itemlist))
file.close()
def write_txt_file_from_list_of_lists(outputdir, filename, itemlist):
ensure_directory(outputdir)
with open(outputdir + "\\" + filename, 'w') as f:
for _list in itemlist:
#f.write(';'.join(_list))
for _string in _list:
f.write(str(_string).strip('[]'))
f.write(';')
f.write('\n')
def write_txt_file_from_string(outputdir, filename, data):
ensure_directory(outputdir)
file = open(outputdir + SLASH + filename, "w")
file.write(data)
file.close()
def write_xml_file(outputdir, filename, data):
ensure_directory(outputdir)
file = open(outputdir + SLASH + filename + XML_EXT, "w")
file.write(data)
file.close()
def extract_file_names_from_dir(dirpath):
file_list = []
for (dirpath, dirnames, filenames) in walk(dirpath):
file_list.extend(filenames)
break
return file_list
def read_json_file(inputfile):
with open(inputfile) as data_file:
data = json.load(data_file)
return data
# this method cleans up a temporary directory
# it is employed before new analysis starts
def cleanup_tmp_directories(folder):
for the_file in os.listdir(folder):
file_path = os.path.join(folder, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
except Exception, e:
print e
def toByteStr(text):
#print 'toByteStr:', text
res = ''
if text and text != 'NoneType':
res = text.encode('utf8', 'ignore')
return res
def validate_response_json(response):
return validate_json(response.content)
def validate_json(data):
json_data = None
try:
#print 'response.content', response.content
#print 'tmp response.content', response.content.replace('[]','"None":""')
tmp = validate_json_str(data)
json_data = json.loads(tmp)
except JSONDecodeError as jde:
print 'JSONDecodeError. Response author data:', data, jde
except UnboundLocalError as ule:
print 'UnboundLocalError. Response author data:', data, ule
except:
print 'Response json:', data
print 'Unexpected error:', sys.exc_info()[0]
print 'json_data:', json_data
return json_data
# This method validates JSON string and replaces empty keys []
def validate_json_str(data):
try:
tmp = data.replace('[]','"None":""')
json_data_str = tmp
except JSONDecodeError as jde:
print 'JSONDecodeError. Response author data:', data, jde
except:
print 'Response json:', data
print 'Unexpected error:', sys.exc_info()[0]
print 'json_data_str:', json_data_str
return json_data_str
def process_http_query(query):
r = requests.get(query)
if(r.status_code != 200):
print('Request error:', r.url)
return r
def is_stored_as_json_file(path):
response_json = None
inputfile = glob.glob(path)
if(inputfile):
print 'exists:', inputfile
#response_content = read_json_file(inputfile[0])
#response_json = json.loads(response_content)
response_json = read_json_file(inputfile[0])
return response_json
def find_longest_substring(string1, string2):
""" returns the longest common substring from the beginning of string1 and string2 """
def _iter():
for a, b in zip(string1, string2):
if a == b:
yield a
else:
return
return ''.join(_iter())
def longest_substring_finder(string1, string2):
answer = ""
len1, len2 = len(string1), len(string2)
#for i in range(len1):
# match = ""
# for j in range(len2):
# if (i + j < len1 and string1[i + j] == string2[j]):
# match += string2[j]
# else:
# if (len(match) > len(answer)): answer = match
# match = ""
match = ""
i = 0
for j in range(len2):
if (i + j < len1 and string1[i + j] == string2[j]):
match += string2[j]
# else:
# if (len(match) > len(answer)): answer = match
# match = ""
# return answer
return match
def find_longest_substring_from_list(list):
parent = ''
if len(list) > 0:
parent = list[0]
for substring in list:
# parent = longest_substring_finder(parent,substring)
parent = find_common_substring(parent,substring)
# parent = find_longest_substring(parent,substring)
return parent
def long_substr(data):
substr = ''
if len(data) > 1 and len(data[0]) > 0:
for i in range(len(data[0])):
for j in range(len(data[0])-i+1):
if j > len(substr) and all(data[0][i:i+j] in x for x in data):
substr = data[0][i:i+j]
return substr
def find_common_substring(str1,str2):
res = ''
if len(str1) > 0 and len(str2) > 0:
for i in range(len(str2)):
if (i < len(str1) and i < len(str2) and str1[i] == str2[i]):
res += str1[i]
else:
break
res = check_parent_at_least_a_word(str1, str2, res)
return res
def check_parent_at_least_a_word(substr, substr2, parent):
# check whether resulting string is at least one word
if len(parent) > 0:
if len(parent) < get_word_len(substr) or len(parent) < get_word_len(substr2):
parent = ''
return parent
def get_word_len(str):
res = 0
# check whether given string is a word and return word length
if str != '':
if ' ' in str:
res = str.index(" ")
else:
res = len(str)
return res
def convert_json_dict_to_string(json_dict):
return json.dumps(json_dict)