-
Notifications
You must be signed in to change notification settings - Fork 0
/
beehive-annotation-parser.py
565 lines (503 loc) · 21.9 KB
/
beehive-annotation-parser.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 29 13:19:13 2019
@author: davidnelson
This script will link up the index to the Alvearium of Pastorius's
"Bee-Hive" manuscript. You will need a cleaned-up copy of the most recent data
export. The end of the script will break the index and Alvearium into two
separate files for further manipulation.
"""
import pandas as pd
import re
import csv
import beehive
import os
# =============================================================================
# The first part of the script will create links between the Alvearium and the
# Octavo Index. At the end, it provides clean files of both sections for
# further use.
# =============================================================================
a = re.compile(r'alpha_\d+')
idx = re.compile(r'index_\d+')
num = re.compile(r'\d+')
with open('data/beehive-data-for-wax.csv', 'r') as unlinked:
df = pd.read_csv(unlinked)
df.fillna('', inplace=True)
# get pids from toc
with open('data/master_toc.csv', 'r') as f:
toc = csv.DictReader(f, delimiter=',')
pages = {}
for row in toc:
if row['volume'] == '2':
if beehive.find_numbers(row['first_entry']) is True:
page_range = list(range(int(row['first_entry']),
int(row['last_entry']) + 1))
pages.update({row['pid']: page_range})
toc_link = "<a href='/digital-beehive/toc/"
# create dummy column for numerical entry matching
df['num_match'] = ''
for row in df.index:
if df.loc[row, 'pid'].startswith('num'):
entry = str(df.loc[row, 'entry'])
topic = df.loc[row, 'topic']
num_match = f'{entry} [{topic}]'
df.loc[row, 'num_match'] = num_match
for row in df.index:
head = str(df.loc[row, 'head'])
if head != '':
entry = str(df.loc[row, 'entry'])
entry_list = []
if '|' in entry:
entries = entry.split('|')
for i in entries:
if i == 'a':
# Since the index is added manually to each alphabetical
# entry, we don't need to control for case.
index_match = df[df['index'] == head]
try:
annotation = beehive.alpha_annotator(index_match, 'a')
entry_list.append(annotation)
except IndexError:
print(f'{head} has a problem with "a."')
entry_list.append(i)
# numerical section annotated up to 1000 for now, so we
# can try to link these. Adjust number at next datapull
elif beehive.find_numbers(i) is True:
num_entry = int(re.search(r'\d+', i).group())
if '[PAGE_MISSING' in i:
entry_list.append(i)
elif num_entry <= 1000:
match = df[df['num_match'] == i]
try:
annotation = beehive.num_annotator(
match, i)
entry_list.append(annotation)
except IndexError:
print(f'{head} has a numerical problem.')
entry_list.append(i)
else:
for pid, contents in pages.items():
if num_entry in contents:
annotation = f"{toc_link}{pid}/'>{i}</a>"
entry_list.append(annotation)
else:
continue
else:
entry_list.append(i)
print(head)
elif entry == 'a':
index_match = df[df['index'] == head]
try:
annotation = beehive.alpha_annotator(index_match, 'a')
entry_list.append(annotation)
except IndexError:
print(f'{head} has a problem with "a."')
entry_list.append(entry)
elif beehive.find_numbers(entry) is True:
num_entry = int(re.search(r'\d+', entry).group())
if '[PAGE_MISSING' in entry:
entry_list.append(entry)
elif num_entry <= 1000:
match = df[df['num_match'] == entry]
try:
annotation = beehive.num_annotator(match, entry)
entry_list.append(annotation)
except IndexError:
print(f'{head} has a numerical problem.')
entry_list.append(entry)
else:
for pid, contents in pages.items():
if num_entry in contents:
annotation = f"{toc_link}{pid}/'>{entry}</a>"
entry_list.append(annotation)
else:
continue
else:
entry_list.append(entry)
new_entry = '|'.join(entry_list)
df.loc[row, 'entry'] = new_entry
print('Links to Alvearium done.')
# write links to index
for row in df.index:
index = str(df.loc[row, 'index'])
if index != '':
index_list = []
if '|' in index:
indices = index.split('|')
for i in indices:
index_match = df[df['head'] == i]
try:
annotation = beehive.index_annotator(index_match, i)
index_list.append(annotation)
except IndexError:
print(f"{df.loc[row, 'entry']} {df.loc[row, 'topic']} "
"has an index problem.")
index_list.append(i)
# handle missing entries separately to avoid excessive errors
elif index == '[NOT_IN_INDEX]':
index_list.append(index)
else:
index_match = df[df['head'] == index]
try:
annotation = beehive.index_annotator(index_match, index)
index_list.append(annotation)
except IndexError:
print(
(f"{df.loc[row, 'entry']} [{df.loc[row, 'topic']}] has "
'an index problem.')
)
index_list.append(index)
new_index = '|'.join(index_list)
df.loc[row, 'index'] = new_index
df = df.drop(['num_match'], axis=1)
# write issues from issue trackers
df['issue'] = ''
# load issues
alpha_issues = beehive.load_issues('data/alpha-issues.csv')
num_issues = beehive.load_issues('data/num-issues.csv')
index_issues = beehive.load_issues('data/index-issues.csv')
for i in alpha_issues.keys():
match = df.index[df['item'] == i].tolist()
df.loc[match, 'issue'] = alpha_issues[i]
for i in num_issues.keys():
match = df.index[df['item'] == i].tolist()
df.loc[match, 'issue'] = num_issues[i]
for i in index_issues.keys():
match = df.index[df['item'] == i].tolist()
df.loc[match, 'issue'] = index_issues[i]
linked_csv = df.to_csv('beehive-data-linked.csv', index=False)
print('Links to index done.')
beehive.write_csv(
'beehive-data-linked.csv', 'alvearium.csv', ('alpha', 'num'), 'pid')
print('Alvearium separated')
beehive.write_csv(
'beehive-data-linked.csv', 'beehive-index.csv', 'index', 'pid')
print('Index separted')
os.remove('beehive-data-linked.csv')
# =============================================================================
# The next part of the script creates cross-references between entries
# in the Alvearium. For numerical entries that have not yet been annotated,
# the script creates a link to the corresponding Wax ToC page. Note that this
# feature of the code will become obsolete when the entire Numerical Section
# has been annotated.
# =============================================================================
# load corrections
corrections = beehive.load_corrections('data/alpha-corrections.csv')
# create links to cross references
with open('alvearium.csv', 'r') as f:
df = pd.read_csv(f)
df.fillna('', inplace=True)
df['num_match'] = ''
for row in df.index:
if df.loc[row, 'pid'].startswith('num'):
entry = str(df.loc[row, 'entry'])
topic = df.loc[row, 'topic']
num_match = f'{entry} [{topic}]'
df.loc[row, 'num_match'] = num_match
for row in df.index:
xref = df.loc[row, 'xref']
xref_list = []
if '|' in xref:
xrefs = xref.split('|')
for i in xrefs:
if i in corrections: # we shouldn't get problems here, so no check
xref_match = df[df['item'] == corrections[i]]
annotation = beehive.alpha_annotator(xref_match, i)
xref_list.append(annotation)
elif beehive.find_numbers(i) is True:
xref_num = int(re.search(r'\d+', i).group())
if '[PAGE_MISSING' in i:
xref_list.append(i)
elif xref_num <= 1000:
try:
xref_match = df[df['num_match'] == i]
annotation = beehive.num_annotator(xref_match, i)
xref_list.append(annotation)
except IndexError:
print(f'Cross-reference {i} in entry',
f"{df.loc[row, 'entry']} has a problem.")
xref_list.append(i)
else:
for pid, contents in pages.items():
if xref_num in contents:
a = f"{toc_link}{pid}/'>{i}</a>"
xref_list.append(a)
else:
continue
elif i == '[WORD_MISSING]': # tag used for bith xrefs and entries
xref_list.append(i)
else:
xref_match = df[df['entry'].str.lower() == i.lower()]
try:
annotation = beehive.alpha_annotator(xref_match, i)
xref_list.append(annotation)
except IndexError:
print(f'Cross-reference {i} in entry',
f"{df.loc[row, 'entry']} has a problem.")
xref_list.append(i)
else:
if xref in corrections:
xref_match = df[df['item'] == corrections[xref]]
annotation = beehive.alpha_annotator(xref_match, xref)
xref_list.append(annotation)
elif xref == '':
xref_list.append(xref)
elif beehive.find_numbers(xref) is True:
xref_num = int(re.search(r'\d+', xref).group())
if '[PAGE_MISSING' in xref:
xref_list.append(xref)
elif xref_num <= 1000:
try:
xref_match = df[df['num_match'] == xref]
annotation = beehive.num_annotator(xref_match, xref)
xref_list.append(annotation)
except IndexError:
print(f'Cross-reference {xref} in entry',
f"{df.loc[row, 'entry']} has a problem.")
xref_list.append(xref)
else:
for pid, contents in pages.items():
if xref_num in contents:
a = f"{toc_link}{pid}/'>{xref}</a>"
xref_list.append(a)
else:
continue
elif xref == '[WORD_MISSING]':
xref_list.append(xref)
else:
xref_match = df[df['entry'].str.lower() == xref.lower()]
try:
annotation = beehive.alpha_annotator(xref_match, xref)
xref_list.append(annotation)
except IndexError:
print(f'Cross-reference {xref} in entry',
f"{df.loc[row, 'entry']} has a problem.")
xref_list.append(xref)
new_xref = '|'.join(xref_list)
df.loc[row, 'xref'] = new_xref
print('Alvearium cross-references created.')
# Create metadata that alerts user when a numerical entry shares an entry
# number with another entry
df['also_in_entry'] = ''
for row in df.index:
entry = df.loc[row, 'entry']
topic = df.loc[row, 'topic']
also_in_list = []
if beehive.find_numbers(entry) is True:
entry_match = df[df['entry'] == entry]
if len(entry_match) > 1:
for item in entry_match.index:
if df.iloc[item]['topic'] != topic:
also_entry = beehive.num_annotator(
df.iloc[item], df.iloc[item]['topic'])
also_in_list.append(also_entry)
also_in = '|'.join(also_in_list)
df.loc[row, 'also_in_entry'] = also_in
print('Also in data created.')
# =============================================================================
# Now we handle page links. You'll need the data file with Pastorius's
# pagination. Note that this file is different from the pages in the Table of
# Contents and must be updated to reflect the desired links.
# =============================================================================
with open('data/pastorius-pages.csv', 'r') as f:
toc = csv.DictReader(f, delimiter=',')
p_pages = {}
for row in toc:
p_pages.update({row['pastorius_page_numbers']: row['pid']})
for row in df.index:
if df.loc[row, 'page'] != '':
page = df.loc[row, 'page']
p_list = []
if '|' in page:
paginae = page.split('|')
for i in paginae:
pnumber = re.search(r'p.\d+', i).group().strip('p.')
if pnumber in p_pages:
n = p_pages[pnumber]
annotation = f"{toc_link}{n}/'>{i}</a>"
p_list.append(annotation)
else:
print(f"{pnumber} for {df.loc[row, 'entry']} missing.")
p_list.append(i)
else:
pnumber = re.search(r'p.\d+', page).group().strip('p.')
if pnumber in p_pages:
n = p_pages[pnumber]
annotation = f"{toc_link}{n}/'>{page}</a>"
p_list.append(annotation)
else:
print(f"{pnumber} for {df.loc[row, 'entry']} missing.")
p_list.append(page)
new_page = '|'.join(p_list)
df.loc[row, 'page'] = new_page
df = df.drop(['num_match'], axis=1)
new_csv = df.to_csv('data/alvearium-linked.csv', index=False)
print('Page references created for the Alvearium.')
os.remove('alvearium.csv')
with open('beehive-index.csv', 'r') as f:
df = pd.read_csv(f)
df.fillna('', inplace=True)
for row in df.index:
if df.loc[row, 'page'] != '':
page = df.loc[row, 'page']
p_list = []
if '|' in page:
paginae = page.split('|')
for i in paginae:
pnumber = re.search(r'p.\d+', i).group().strip('p.')
if pnumber in p_pages:
n = p_pages[pnumber]
annotation = f"{toc_link}{n}/'>{i}</a>"
p_list.append(annotation)
else:
print(f"{pnumber} for {df.loc[row, 'head']} missing.")
p_list.append(i)
else:
pnumber = re.search(r'p.\d+', page).group().strip('p.')
if pnumber in p_pages:
n = p_pages[pnumber]
annotation = f"{toc_link}{n}/'>{page}</a>"
p_list.append(annotation)
else:
print(f"{pnumber} for {df.loc[row, 'head']} missing.")
p_list.append(page)
new_page = '|'.join(p_list)
df.loc[row, 'page'] = new_page
print('Index page references created.')
# =============================================================================
# Now we handle see and add references in the index. Both of these can be
# problematic, so you will need the corresponding files for corrections.
# =============================================================================
corrections = beehive.load_corrections('data/index-see-corrections.csv')
for row in df.index:
if df.loc[row, 'see'] != '':
see = df.loc[row, 'see']
see_list = []
if '|' in see:
vide = see.split('|')
for i in vide:
if i in corrections:
see_match = df[df['item'] == corrections[i]]
annotation = beehive.index_annotator(see_match, i)
see_list.append(annotation)
else:
see_match = df[df['head'].str.lower() == i.lower()]
try:
annotation = beehive.index_annotator(see_match, i)
see_list.append(annotation)
except IndexError:
print(f'See reference {i} for header',
f"{df.loc[row, 'head']} has a problem.")
see_list.append(i)
else:
if see in corrections:
see_match = df[df['item'] == corrections[see]]
annotation = beehive.index_annotator(see_match, see)
see_list.append(annotation)
else:
see_match = df[df['head'].str.lower() == see.lower()]
try:
annotation = beehive.index_annotator(see_match, see)
see_list.append(annotation)
except IndexError:
print(f'See reference {see} for header',
f"{df.loc[row, 'head']} has a problem.")
see_list.append(see)
new_see = '|'.join(see_list)
df.loc[row, 'see'] = new_see
print('See annotations created.')
# make add annotations
corrections = beehive.load_corrections('data/index-add-corrections.csv')
for row in df.index:
if df.loc[row, 'add'] != '':
add = df.loc[row, 'add']
add_list = []
if '|' in add:
adde = add.split('|')
for i in adde:
if i in corrections:
add_match = df[df['item'] == corrections[i]]
annotation = beehive.index_annotator(add_match, i)
add_list.append(annotation)
else:
add_match = df[df['head'].str.lower() == i.lower()]
try:
annotation = beehive.index_annotator(add_match, i)
add_list.append(annotation)
except IndexError:
print(f'Add reference {i} for header',
f"{df.loc[row, 'head']} has a problem.")
add_list.append(i)
elif add in corrections:
add_match = df[df['item'] == corrections[add]]
annotation = beehive.index_annotator(add_match, add)
add_list.append(annotation)
else:
add_match = df[df['head'].str.lower() == add.lower()]
try:
annotation = beehive.index_annotator(add_match, add)
add_list.append(add)
except IndexError:
print(f'Add reference {add} for header',
f"{df.loc[row, 'head']} has a problem.")
add_list.append(add)
new_add = '|'.join(add_list)
df.loc[row, 'add'] = new_add
print('Add annotations created.')
# create insertion links
ins = re.compile(r'\[:\d+.\]')
crochets = {} # get pids from insertions
for row in df.index:
if df.loc[row, 'insertion'] != '':
insertion = df.loc[row, 'insertion']
pid = df.loc[row, 'pid']
beehive.add_or_append(crochets, insertion, pid)
for row in df.index:
ins_match = ins.match(str(df.loc[row, 'head']))
key_list = []
xref_list = []
if ins_match is not None:
head = df.loc[row, 'head']
if head == '[:70.]':
print('Crochet [:70.] needs updated protocol!')
else:
key_list = crochets.get(head)
for i in key_list:
xref = df.loc[df['pid'] == i]['head'].to_list()
xref = xref[0]
link = f"<a href='/digital-beehive/index5/{i}/'>{xref}</a>"
xref_list.append(link)
annotation = '|'.join(xref_list)
df.loc[row, 'insertion_xref'] = annotation
print('Index crochets linked.')
new_csv = df.to_csv('data/beehive-index-linked.csv', index=False)
print('Index file created.')
os.remove('beehive-index.csv')
print('Creating individual files for the Alvearium...')
beehive.write_csv('data/alvearium-linked.csv', 'data/alpha1.csv',
('A', 'B', 'C', 'D'), 'first_letter')
beehive.write_csv('data/alvearium-linked.csv', 'data/alpha2.csv',
('E', 'F', 'G', 'H'), 'first_letter')
beehive.write_csv('data/alvearium-linked.csv', 'data/alpha3.csv',
('I', 'K', 'L', 'M', 'N'), 'first_letter')
beehive.write_csv('data/alvearium-linked.csv', 'data/alpha4.csv',
('O', 'P', 'Q', 'R', 'S'), 'first_letter')
beehive.write_csv('data/alvearium-linked.csv', 'data/alpha5.csv',
('T', 'U', 'W', 'X', 'Y', 'Z'), 'first_letter')
beehive.write_num_csv('data/alvearium-linked.csv', 'data/num1.csv', 1, 250)
beehive.write_num_csv('data/alvearium-linked.csv', 'data/num2.csv', 251, 500)
beehive.write_num_csv('data/alvearium-linked.csv', 'data/num3.csv', 501, 725)
beehive.write_num_csv('data/alvearium-linked.csv', 'data/num4.csv', 866, 1000)
print('Creating individual files for the index...')
beehive.write_csv('data/beehive-index-linked.csv', 'data/index1.csv',
('A', 'B', 'C', 'D'), 'first_letter')
beehive.write_csv('data/beehive-index-linked.csv', 'data/index2.csv',
('E', 'F', 'G', 'H'), 'first_letter')
beehive.write_csv('data/beehive-index-linked.csv', 'data/index3.csv',
('I', 'K', 'L', 'M', 'N'), 'first_letter')
beehive.write_csv('data/beehive-index-linked.csv', 'data/index4.csv',
('O', 'P', 'Q', 'R', 'S'), 'first_letter')
beehive.write_csv('data/beehive-index-linked.csv', 'data/index5.csv',
('T', 'U', 'W', 'X', 'Y', 'Z', 'i'), 'first_letter')
print('Done.')