-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy_filenames.py
480 lines (419 loc) · 15.8 KB
/
py_filenames.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# Generates filenames based on image and object descriptions
# Note that file extentions are added separately
#
#
import codecs
import os
import helpers
from helpers import output
# limitations on namelength
# shorten if longer GOODLENGTH cut if longer than MAXLENGTH
GOODLENGTH = 100
MAXLENGTH = 128
PHOTO_FILE = u'photo_multimedia_etc.csv'
OBJDATEN_FILE = u'objDaten_etc.csv'
CSV_FOLDER = u'data'
LOG_SUBFOLDER = u'logs'
MAPPING_FOLDER = u'mappings'
LIST_CAT = u'Category:Livrustkammaren och Skoklosters slott med Stiftelsen Hallwylska museet/mapping'
def run(folder=None, mapping=None, outfolder=None,
filename_photo=None, filename_obj_daten=None):
"""
Generates filenames from photo and object descriptions
:param folder: the folder containing csv files
:param mapping: the folder containing mapping files
:param filename_photo: the photo data csv file
:param filename_obj_daten: the objDaten csv file
:param outfolder: if provided output is not put in default folder
:return: None
"""
# set defaults unless overridden
folder = folder or CSV_FOLDER
mapping = mapping or MAPPING_FOLDER
filename_photo = filename_photo or PHOTO_FILE
filename_obj_daten = filename_obj_daten or OBJDATEN_FILE
outfolder = outfolder or folder
# create target folders if they don't exist
target_folders = (mapping, os.path.join(outfolder, LOG_SUBFOLDER))
for t in target_folders:
if not os.path.isdir(t):
os.mkdir(t)
# make descriptions
photo_file = os.path.join(folder, filename_photo)
obj_daten_file = os.path.join(folder, filename_obj_daten)
log_file = os.path.join(outfolder, LOG_SUBFOLDER, u'filenames.log')
descriptions, photo = makeDescriptions(photo_file, obj_daten_file,
log_file)
# output Commons
mapping_file = os.path.join(mapping, u'Filenames.txt')
commonsOutput(descriptions, mapping_file)
# make Filenames
filenames_file = os.path.join(outfolder, u'filenames.csv')
makeFilenames(descriptions, photo, filenames_file)
def makeDescriptions(photoFile, objDatenFile, logFile):
"""
Given the photo and objDaten data this uses the two generate descriptions.
Also returns photo for later use
:param photoFile: path to photo data file
:param multiFile: path to multimedia data file
:param logFile: path to logfile
:return: dict, dict
"""
# setup
flog = codecs.open(logFile, 'w', 'utf-8') # logfile
# load input files
photoHeader = 'PhoId|MulId|PhoObjId|PhoBeschreibungM|PhoAufnahmeortS|' \
'PhoSwdS|AdrVorNameS|AdrNameS|PhoSystematikS|MulPfadS|' \
'MulDateiS|MulExtentS|PstId|same_PhoId|same_object'
photo = helpers.csvFileToDict(photoFile, 'PhoId', photoHeader,
lists=('PhoObjId', ))
objDatenHeader = 'ObjId|ObjKueId|AufId|AufAufgabeS|ObjTitelOriginalS|' \
'ObjTitelWeitereM|ObjInventarNrS|ObjInventarNrSortiertS|' \
'ObjReferenzNrS|ObjDatierungS|ObjJahrVonL|ObjJahrBisL|' \
'ObjSystematikS|ObjFeld01M|ObjFeld02M|ObjFeld03M|ObjFeld06M|' \
'ObjReserve01M|ausId|related|ergId|role:roleCmt:kueId|' \
'mulId|massId'
objDaten = helpers.csvFileToDict(objDatenFile, 'ObjId', objDatenHeader)
# start process
skipLog = [] # no photoDescr, no objectDescr
manyLog = [] # no photoDescr, many objects
noHopeLog = [] # no photoDescr, no objects
descriptions = {}
uniques = set([]) # unique filenames
for k, v in photo.iteritems():
phoId = v['PhoId']
objIds = v['PhoObjId']
museum = v['PhoSwdS']
if not museum:
museum = u'LSH'
phoBes = getDescFromPhoBes(v['PhoBeschreibungM'])
if not phoBes: # try to get description from object
if len(objIds) == 1 and objIds[0]:
# exactly one object
phoBes = getDescFromObj(objDaten[objIds[0]])
if not phoBes:
# failed to make a description from the object
skipLog.append(phoId)
elif len(objIds) > 1:
# multiple objects
manyLog.append(phoId)
else:
# no objects
noHopeLog.append(phoId)
if phoBes:
filename = u'%s - %s - %s' % (phoBes, museum, phoId)
descriptions[phoId] = {'descr': phoBes,
'filename': filename}
uniques.add(filename)
# check uniqueness
if len(uniques) != len(descriptions):
output(u'Descriptions are not unique!!!!: %d were duplicate' %
(len(uniques) - len(descriptions)))
# output logs
if skipLog:
flog.write('* No-objectDescr and No-photoDescr (phoIds)\n')
flog.write('%s\n' % '\n'.join(skipLog))
if manyLog:
flog.write('* Many objects and No-photoDescr (phoIds)\n')
flog.write('%s\n' % '\n'.join(manyLog))
if noHopeLog:
flog.write('* No-objects and No-photoDescr (phoIds)\n')
flog.write('%s\n' % '\n'.join(noHopeLog))
# wrap up
output(u'Processed %d images out of which %d has some type of problem. '
u'See log (%s) for more info.' %
(len(photo), len(photo) - len(descriptions), logFile))
flog.close()
return descriptions, photo
def getDescFromPhoBes(text):
"""
Given a text, filter and chop it to get a suitable description
:param text: text to process
:return: str
"""
# setup
badStrings = (u'LRK.', u'LRK ', u'LRk ', u'HWY ', u'Hwy S', u'ENR ',
u'Enr ', u'enr ', u'inv. nr. ', u'SKO ', u'LXIV:',
u'unr ', u'XLII:', u'XXX')
badchar = u'-., '
# remove any badstrings/ids
runAgain = True
while(runAgain):
runAgain = False
for b in badStrings:
if b in text:
runAgain = True
pos = text.find(b)
# find where to stop chopping
separators = (',', ' och', ' &', '. ')
sep = None
pos2 = -1
for separator in separators:
posEnd = text.find(separator, pos + len(b))
if posEnd > 0 and (posEnd < pos2 or pos2 < 0):
sep = separator.rstrip()
pos2 = posEnd
# chop
if pos2 > 0:
# See if there is a list of ids
while(True):
pos3 = text.find(',', pos2 + len(sep))
if pos3 > 0:
bit = text[pos2 + len(sep):pos3]
if not bit.strip('0123456789-,. '):
pos2 = pos3 + len(',') - len(sep)
continue
else:
bit = text[pos2 + len(sep):]
if not bit.strip('0123456789-,. '):
pos2 = pos2 + len(bit)
break
text = u'%s%s %s' % (text[:pos].strip(badchar),
sep,
text[pos2 + len(sep):].strip(badchar))
else:
text = text[:pos]
# cleanup text
text = cleanup_routine(text)
return text
def getDescFromObj(obj):
"""
Constructs a suitable description based on an entry in ObjDaten
:param obj: the objDaten item
:return: str
"""
badStrings = [u'<!>', u'(?)', u'Biografi och genealogi', u'Geografi',
u'Konst- och kulturhistoria', u'Samhälls- & rättsvetenskap',
u'Genrebild', u'Historiebild', u'Djurbild', u'Landskapsbild']
badchar = u'-., ' # kanske även "
# There are two relevant fields.
# Which to use depends partly on the collection to which the obj belongs
orig = obj[u'ObjTitelOriginalS']
kort = obj[u'ObjTitelWeitereM']
# remove badStrings from both
for b in badStrings:
orig = orig.replace(b, '')
kort = kort.replace(b, '')
# strip badchar
orig = orig.strip(badchar)
kort = kort.strip(badchar)
# if only numbers (left)
if not orig.strip('0123456789,.- '):
orig = ''
if not kort.strip('0123456789,.- '):
kort = ''
# decision time, pick longer or rely on collection
descr = ''
if orig.lower() in kort.lower():
descr = kort
elif kort.lower() in orig.lower():
descr = orig
elif not kort and not orig:
descr = ''
else:
samling = obj[u'AufAufgabeS']
if samling in (u'Livrustkammaren', u'Skoklosters slott'):
descr = kort
elif samling in (u'Hallwylska museet'):
descr = '%s: %s' % (orig, kort)
else: # u'LRK dubletter', u'Skoklosters slotts boksamling'
descr = orig
# cleanup
descr = cleanup_routine(descr)
return descr
def cleanup_routine(text):
"""
Run the full cleanup routine on a string.
:param text: the text to clean
:return: str
"""
text = cleanName(text)
text = cleanString(text)
if not text.strip('0123456789,.- '):
# if no relevant info left
text = ''
else:
text = shortenString(text)
text = touchup(text)
return text
def cleanName(text):
"""
Removes unwanted strings from a text string
:param text: text to test
:return: str
"""
# simple strings to remove
easyBadStrings = (u'Fler inventarienr.', u'Fler inventarienr',
u'Flera inventarienr.', u'Flera inventarienr',
u'Fler inventareinr', u'(?)')
for b in easyBadStrings:
text = text.replace(b, '').strip()
return text
def cleanString(text):
"""
Removes characters which are forbidden/undesired in filenames from string
:param text: the text to test
:return: str
"""
# bad characters - extend as more are identified
# Note that ":" is complicated as it has several different interpretaions.
# Currently first replacing possesive case and sentence break then
# dealing with stand alone :
# maybe also ? and ' symbol
badChar = {u'\\': u'-', u'/': u'-', u'|': u'-', u'#': u'-',
u'[': u'(', u']': u')', u'{': u'(', u'}': u')',
u':s': u's', u': ': u', ',
u' ': u' ', u' ': u' ', u' ': u' ', # unusual whitespace
u'e´': u'é',
u'”': u' ', u'"': u' ', u'“': u' '}
for k, v in badChar.iteritems():
text = text.replace(k, v)
# replace any remaining colons
if u':' in text:
text = text.replace(u':', u'-')
# replace double space by single space
text = text.replace(' ', ' ')
return text.strip()
def shortenString(text):
"""
If a string is larger than GOODLENGTH then this tries to
find a sensibel shortening
:param text: text to test
:return: str
"""
badchar = u'-., ' # kanske även "
if u'<!>' in text:
text = text[:text.find(u'<!>')]
# is ok?
if len(text) < GOODLENGTH:
return text
# attempt fixing
# remove trailing brackets
if text.endswith(')'):
pos = text.rfind('(')
if pos > 0:
return shortenString(text[:pos].strip(badchar))
# split string at certain character
pos = text.rfind('.')
if pos < 0:
pos = text.rfind(' - ')
if pos < 0:
pos = text.rfind(';')
if pos < 0:
pos = text.rfind(',')
if pos < 0:
# try something else
if len(text) > MAXLENGTH:
text = u'%s...' % text[:MAXLENGTH - 3]
return text
return shortenString(text[:pos].strip(badchar))
def touchup(text):
"""
Tweaks a string by removing surrounding bracket or quotes as well as
some trailing punctuation
:param text: text to test
:return: str
"""
# If string starts and ends with bracket or quotes then remove
brackets = {u'(': ')', u'[': ']', u'{': '}', u'"': '"'}
for k, v in brackets.iteritems():
if text.startswith(k) and text.endswith(v):
if text[:-1].count(k) == 1:
# so as to not remove unmatching brackets.
# slice in check is due to quote-bracket
text = text[1:-1]
# Get rid of leading/trailing punctuation
text = text.strip(' .,;')
# Make sure first character is upper case
text = text[:1].upper() + text[1:]
return text
def commonsOutput(descriptions, mappingFile, allEntries=None):
"""
Given filedescriptions this outputs it correctly in a Commons export format
:param descriptions: dict of descriptions with phoId as key
:param mappingFile: the target file for output
:param allEntries: optional, a list phoIds giving the order in which
to output the entries. This allows for easier
diff comparison
:return: None
"""
# setup
fOut = codecs.open(mappingFile, 'w', 'utf-8')
chunkSize = 250
chunkStart = u"====%d-%d====\n" \
u"{| class=\"wikitable sortable\"\n|-\n! PhoId !! generated " \
u"<descr> !! improved <descr>\n"
rowFormat = u"|-\n| %s || %s || \n"
# write intro
fOut.write(
u'Final filename becomes: <descr> - <museum> - <photoId>.<ext>\n\n'
u'Attempts have been made to keep descriptions under %d characters '
u'with a hard limit at %d characters\n\n'
u'You are free to improve the descriptions by adding an alternativ '
u'in the last column.\n'
u'===phoId | description | new description===\n\n'
u'%s' % (GOODLENGTH, MAXLENGTH, chunkStart % (0, chunkSize)))
if allEntries is None:
allEntries = descriptions.keys()
counter = 0
for phoId in allEntries:
# Add regular breaks
counter += 1
if counter % chunkSize == 0:
fOut.write(u'|}\n\n' + chunkStart % (counter, counter + chunkSize))
# write row
descr = descriptions[phoId]['descr']
fOut.write(rowFormat % (phoId, insufficient(descr)))
# # write outro
fOut.write(u'|}')
fOut.write(u'\n\n[[%s]]' % LIST_CAT)
fOut.close()
output(u'Created %s' % mappingFile)
def insufficient(text):
"""
Checks if a string is in blacklist, if so wrap in span setting text
colour to red, otherwise return unchanged.
:param text: test string
:return: str
"""
badStrings = [u'Detalj', u'Helbild', u'Målning', u'Reprofoto',
u'Ritning', u'Bok']
if text in badStrings:
text = u'<span style="color:red">%s</span>' % text
return text
def makeFilenames(descriptions, photo, filenamesFile):
"""
Given filedescriptions this outputs it correctly as csv for later import
:param descriptions: dict of descriptions with phoId as key
:param photo: the photo data
:param filenamesFile: the target file for output
:return: None
"""
# setup
filenamesHeader = 'PhoId|MulId|MulPfadS|MulDateiS|filename|ext'
# make a dict to be able to reuse helpers.dictToCsvFile()
filenames = {}
for phoId, v in descriptions.iteritems():
filenames[phoId] = {
'PhoId': phoId,
'MulId': photo[phoId]['MulId'],
'MulPfadS': photo[phoId]['MulPfadS'],
'MulDateiS': photo[phoId]['MulDateiS'],
'filename': v['filename'],
'ext': ''
}
# output
helpers.dictToCsvFile(filenamesFile, filenames, filenamesHeader)
output(u'Created %s' % filenamesFile)
if __name__ == '__main__':
import sys
usage = u'Usage:\tpython py_filenames.py\n'
argv = sys.argv[1:]
if not argv:
run()
else:
print usage
# EoF