-
Notifications
You must be signed in to change notification settings - Fork 2
/
distill.py
1931 lines (1438 loc) · 33.6 KB
/
distill.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import re
from itertools import chain
from lib import GT, LT, AMP
CORRECTION_ALLOWED = {
"01:p0004": {"author"},
"01:p0007": {"place", "title"},
"01:p0018": {"title"},
"01:p0056": {"title"},
"01:p0105": {"place", "author", "title"},
"01:p0106": {"author", "title"},
"01:p0121": {"author", "title"},
"01:p0129": {"author"},
"01:p0247": {"author"},
"01:p0302": {"place", "author", "title"},
"01:p0433": {"place", "author", "title"},
"01:p0482": {"place", "author", "title"},
"01:p0152": {"rawdate", "day", "month", "year", "title"},
"02:p0311": {"author", "title"},
"03:p0192": {"rawdate", "day", "title"},
"04:p0241": {"rawdate", "day", "title"},
"04:p0493": {"author"},
"04:p0496": {"month", "title"},
"06:p0065": {"author"},
"06:p0275": {"author"},
"06:p0402": {"author"},
"06:p0406": {"author", "title"},
"06:p0750": {"author"},
"06:p0897": {"author"},
"07:p0003": {"seq"},
"07:p0290": {"author"},
"07:p0353": {"author"},
"07:p0381": {"author"},
"07:p0396": {"author"},
"07:p0413": {"author"},
"07:p0456": {"author"},
"07:p0467": {"author"},
"07:p0479": {"author"},
"07:p0485": {"author"},
"07:p0517": {"author"},
"07:p0534": {"author"},
"07:p0537": {"author"},
"07:p0547": {"author"},
"07:p0552": {"author"},
"07:p0583": {"author"},
"07:p0596": {"author"},
"07:p0607": {"author"},
"07:p0610": {"author"},
"07:p0640": {"author"},
"07:p0651": {"author"},
"07:p0657": {"author"},
"07:p0660": {"seq", "author"},
"07:p0661": {"author"},
"07:p0684": {"author"},
"07:p0693": {"author"},
"07:p0706": {"author"},
"07:p0707": {"author"},
"07:p0710": {"author"},
"07:p0744": {"author"},
"07:p0745": {"author"},
"07:p0746": {"rawdate", "day", "author", "title"},
"07:p0754": {"author"},
"08:p0128": {"author"},
"08:p0224": {"author"},
"08:p0234": {"rawdate", "day", "title"},
"08:p0235": {"seq"},
"09:p0070": {"rawdate", "day", "month", "year"},
"09:p0344": {"seq", "rawdate", "day", "month", "year", "author", "title"},
"09:p0628": {"seq", "rawdate", "day", "month", "year", "author", "title"},
"10:p0087": {"author"},
"10:p0297": {"rawdate", "day", "month", "year"},
"10:p0399": {"rawdate", "day", "month", "year"},
"11:p0224": {"seq"},
"11:p0226": {"author"},
"12:p0001": {"author"},
"12:p0003": {"author"},
"12:p0083": {"author"},
"12:p0183": {"author"},
"13:p0340": {"author"},
"13:p0362": {"author"},
"13:p0626": {"author", "title"},
}
CORRECTION_FORBIDDEN = {
"01:p0003a": {"page"},
"01:p0003b": {"page"},
"01:p0003c": {"page"},
"01:p0016a": {"page"},
"01:p0016b": {"page"},
"01:p0020b": {"page"},
"01:p0020c": {"page"},
"01:p0027a": {"page"},
"01:p0027b": {"page"},
"01:p0097a": {"page"},
"01:p0097b": {"page"},
"01:p0097c": {"page"},
"01:p0097d": {"page"},
"01:p0097e": {"page"},
"01:p0097f": {"page"},
"01:p0097g": {"page"},
"01:p0098a": {"page"},
"01:p0098b": {"page"},
"01:p0098c": {"page"},
"01:p0118a": {"page"},
"01:p0118b": {"page"},
"01:p0118c": {"page"},
"01:p0118d": {"page"},
"01:p0118e": {"page"},
"01:p0118f": {"page"},
"01:p0244a": {"page"},
"01:p0244b": {"page"},
"01:p0244c": {"page"},
"01:p0244d": {"page"},
"01:p0244e": {"page"},
"01:p0244f": {"page"},
"01:p0244g": {"page"},
"01:p0244h": {"page"},
"01:p0008": {"place"},
"01:p0087": {"place"},
}
FROM_PREVIOUS = {"01:p0056"}
META_KV_2_RE = re.compile(r"""<meta key="([^"]*)" value="([^"]*)"/>""", re.S)
META_KEY_TRANS_DEF = tuple(
x.strip().split()
for x in """
pid pid
page page
seq n
title titleLevel1
status titleLevel1
author authorLevel1
authorFull authorLevel1
rawdate dateLevel1
place localization_placeLevel1
year witnessYearLevel1_from
month witnessMonthLevel1_from
day witnessDayLevel1_from
""".strip().split(
"\n"
)
)
META_KEY_ORDER = tuple(x[0] for x in META_KEY_TRANS_DEF)
COLOFON_KEYS = META_KEY_ORDER[5:]
ADD_META_KEYS = {"authorFull", "status"}
EXTRA_META_KEYS = ("brackets", "rest")
DERIVED_META_KEYS = set(
"""
day
month
year
authorFull
""".strip().split()
)
HEAD_RE = re.compile(r"""<head\b[^>]*>(.*?)</head>""", re.S)
FIRST_PAGE_RE = re.compile(r"""<pb\b[^>]*?\bn="([^"]*)"[^>]*>""", re.S)
def ucfirst(x, lower=True):
return (x[0].upper() + (x[1:].lower() if lower else x[1:])) if x else x
def longestFirst(x):
return -len(x)
NUM_SANITIZE_RE = re.compile(r"""[0-9][0-9 ]*[0-9]""", re.S)
def numSanitize(match):
return f" {match.group(0).replace(' ', '')} "
HI_CLEAN_STRONG_RE = re.compile(r"""<hi\b[^>]*>([^<]*)</hi>""", re.S)
TITLE_REST = {
"zonder plaats datum": dict(place="zonder plaats", rawdate="zonder datum"),
"ongedateerd zonder plaats": dict(place="zonder plaats", rawdate="zonder datum"),
"het cachet van die door ziekte niet zelf kon tekenen": {},
"zonder datum 1729": {},
}
TITLE_BRACKETS = {
f"({x.strip()})"
for x in """
EN de GEASSUMEERDE RADEN
niet getekend wegens ziekte
vermoedelijk 30 november
""".strip().split(
"\n"
)
}
DISTILL_SPECIALS = {
"13:p0626": dict(
title="Bijlage ladinglijst van twaalf retourschepen vertrokken op 15 en 30 oktober, 6 november 1760, 19 januari en 25 april 1761",
rawdate="25 april 1761",
place="Batavia",
author="",
authorFull="",
rest="",
)
}
MONTH_DEF = """
januari
februari
maart
april
mei
juni
juli
augustus
september
oktober
october
november
december
decem¬ber
""".strip().split(
"\n\n"
)
MONTH_VARIANTS = {}
MONTHS = set()
MONTH_NUM = {}
for (i, nameInfo) in enumerate(MONTH_DEF):
(intention, *variants) = nameInfo.strip().split()
MONTH_NUM[intention] = i + 1
MONTHS.add(intention)
for abb in (intention, intention[0:3], intention[0:4]):
MONTH_VARIANTS[abb] = intention
for variant in variants:
for abb in (
variant,
variant[0:3],
f"{variant[0:3]}.",
f"{variant[0:4]}.",
):
MONTH_VARIANTS[abb] = intention
MONTH_DETECT_PAT = "|".join(
sorted(
set(re.escape(mv) for mv in chain(MONTH_NUM, MONTH_VARIANTS)), key=longestFirst
)
)
PLACE_DEF = """
Afrika
Amboina
Amsterdam
Banda-Neira
bandaneira
banda-neira
banda
Bantam
Batavia
ratavia
ba¬tavia
Deventer
Fort
eort
Hoek
Hollandia
Ile
Jakatra
Kasteel
Makéan
Maleyo
Mauritius
Mayo
Nassau
_
neira
Ngofakiaha
Nieuw
Nieuw Hollandia
nieuw-hollandia
Rede
Schip
Straat
Sunda
Tafelbaai
Ternate
Utrecht
Vere
Vlakke
Wapen
Wesel
""".strip().split(
"\n\n"
)
PLACE_LOWERS = set(
"""
aan
boord
de
eiland
het
in
liggende
nabij
op
rede
schip
ter
van
voor
zuidpunt
""".strip().split()
)
PLACE_VARIANTS = {}
PLACES_LOWER = set()
for (i, nameInfo) in enumerate(PLACE_DEF):
(intention, *variants) = nameInfo.strip().split()
if intention == "_":
intention = ""
else:
PLACES_LOWER.add(intention.lower())
PLACE_VARIANTS[intention] = intention
PLACE_VARIANTS[intention.lower()] = intention
for variant in variants:
PLACE_VARIANTS[variant] = intention
PLACE_DETECT_PAT = "|".join(
sorted(set(re.escape(mv) for mv in chain(PLACE_VARIANTS)), key=longestFirst)
)
LOWERS_PAT = "|".join(
sorted(set(re.escape(mv) for mv in PLACE_LOWERS), key=longestFirst)
)
AUTHOR_SANITY = tuple(
entry[0:-1].split("=")
for entry in """
adri aan=adriaan .
adriaan denijs=adriaan de nijs .
albertusvan=albertus van .
andreasvan=andreas van .
antoniocaenenjoan=antonio caen en joan .
ba yen=bayen .
baer- le=baerle .
bijlage van ii=.
c astelijn=castelijn .
christi aan=christiaan .
christoffelvan=christoffel van .
cluysenaaren=cluysenaar en .
constant! jn=constantijn .
cornelisd'ableing=cornelis d'ableing .
cornelisspeelman=cornelis speelman .
d’ ableing=d'ableing .
daniëlnolthenius=daniël nolthenius .
dehaan=de haan .
dehaeze=de haeze .
derparra=der parra .
derwaeyen=der waeyen .
devlaming=de vlaming .
dirckjansz=dirck jansz .
dithardvan=dithard van .
eli as=elias .
enelias=en elias .
enjan=en jan .
enjeremias=en jeremias .
enmaurits=en maurits .
ferdin and=ferdinand .
gideonloten=gideon loten .
grcx)t=groot .
gusta af=gustaaf .
het cachet van=het_cachet_van .
huijbertwillem=huijbert willem .
huysm an=huysman .
isa ac=isaac .
j acob=jacob .
janelias=jan elias .
jo an=joan .
joanmaetsuycker=joan maetsuycker .
joh annes=johannes .
johannesthedens=johannes thedens .
juliuscoyett=julius coyett .
juliusvalentijn=julius valentijn .
librechthooreman=librecht hooreman .
m attheus=mattheus .
mac are=macaré
maetsuy cker=maetsuycker .
maetsu yker=maetsuycker .
manuelbornezee=manuel bornezee .
mattheuscluysenaar=mattheus cluysenaar .
mattheusde=mattheus de .
noltheniusen=nolthenius en .
out hoorn=outhoorn .
pi eter=pieter .
pietervan=pieter van .
rochuspasques=rochus pasques .
ryckloffvan=ryckloff van .
raden van indië=raden_van_^indië .
saint martin=saint-martin .
salomonsweers=salomon sweers .
sibrantabbema=sibrant abbema .
steijnvan=steijn van .
steunvan=steun van .
thomasvan=thomas van .
vanbazel=van bazel .
vanbroyel=van broyel .
vander=van der .
vanhohendorf=van hohendorf .
vanrheede=van rheede .
vanriemsdijk=van riemsdijk .
vanspreekens=van spreekens .
willemvan=willem van .
zw aardecroon=zwaardecroon .
z w a ardecröon=zwaardecroon .
z w a ardecröon=zwaardecroon .
""".strip().split(
"\n"
)
)
AUTHOR_DEF = """
abbema s
abraham f
adam f
adriaan f
adriaen f
aerden s
aernout f
arnoud
albert f
albertus f
alphen s
andreas f
antonio f
anthonio f
anthonto
anthonisz f
anthony f
antony
antzen s
arent f
arend f
arnold f
arrewijne s
artus f
backer s
baerle s
balthasar f
barendsz s
bayen s
bazel s
becker s
beecken s
beecke
bent s
berendregt s
beren-dregt
bergman s
bernard fs
beveren s
blocq s1
blom s
bogaerde s
bornezee s
bort s
bosch s
both s
broeckum s
brouck s
brouwer s
broyel s
burch s
caen s
caesar s
camphuys s
camphuys]
carel f
cakel
gabel
caron s
carpentier s
castelyn s
castelijn
chasteleyn s
chastelein
chavonnes s2
cha-vonnes
christiaan f
christoffel f
chistoffel
cloon s
cluysenaar s
coen s
comans s
constantijn f
constantin f
constanten
cops s
cornelis f
cornèlis
cornel1s
coyett s
crijn f
croocq s
crudop s
crul s
cunaeus s
dam s
&d'^ableing s
d'ableing
d’ableing
daniël f
de i
ue
dedel s
demmer s
den i
dex
der i
diderik f
d1derik
dider1k
diemen s
dirk f
dirck f
dircksz f
dircq f
dishoeck s
dithard f
douglas s
dr x
dubbeldekop s
dubbeldecop
duquesne s
durven s
dutecum s
duynen s
elias fs
everard f
ewout f
faes s
ferdinand f
françois f
erangois
eranqois
frangois
franqois
fran^ois
francois
fran£ois
frans f
frederick f
erederick
frederik f
gabry s
galenus f
gardenijs s
gaspar f
geleynsz s1
gerard f
gekard
gerarjd
gideon f
gijsbertus f
gijsels s
goens s
gollenesse s
gol-lenesse
golle-nesse
gorcom s
gouverneur-generaal s
groot s
guillot s
gualterus f
gustaaf f
haan s
haas s
haeze s
hans f
hartsinck s
hartzinck
harts1nck
hasselaar s
hendrick f
hendrik f
hendri
hendrix s
henrick f
henrik f
herman f
heussen s
heuvel s
heyningen s
hohendorf s
hooreman s
hoorn s
houtman s
hugo f
huijbert f
huij-bert
hulft s
hulet
hurdt s
hustaert s
huyghens s
huysman s
imhoff s
isaac f
jacob f
jacobsz sf
jacques f
jan f
jansz fs
janz
jeremias f
joachim f
joan f
joajst
jüan
joannes f
johannes
jochem f
johan f
jongh s2
josua f
julius f
jurgen f
jur-gen
justus f
klerk s
lakeman s
lake-man
lam s
laurens f
leene s
lefebvre s
librecht f
lijn s
letn
loten s
lucasz s
lycochthon s
maas s
macaré s
macare
macar
maerten f
maetsuycker s
maetsuyker
maetsijyker
manuel f
marten f
makten
martensz s2
martinus f
mattheus f
mat-theus
maurits f
meester s
mersen s
meyde s
michiel f
mijlendonk s
mossel s
mr x
nicolaas f
nico-laas
nicolaes f
nieustadt s
nijs s
nobel s
nolthenius s
nuyts s
oostwalt s
ottens s
outhoorn s
oudtshoorn s2
overbeek s
over-beek
overtwater s
padtbrugge s
parra s
pasques s1
pas-ques
pasoues
patras s
paviljoen s
pavilioen
paul f
paulus f
petrus f
philips f
philippus f
phoonsen s
pielat s
pieter f
fieter
p1eter
pie-ter
pietersz f
pijl s
pit s
pits s
putmans s
quaelbergh s
raden s
raden_van_^indië s
raemburch s
raemsburch
ranst s
reael s
reede s
reniers s
reyersz s
reynier f
reynst s