-
Notifications
You must be signed in to change notification settings - Fork 1
/
expand_signatures.pl
460 lines (370 loc) · 11.8 KB
/
expand_signatures.pl
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
#!/bin/env perl
# nbt, 26.6.2023
# cleans up and expands signatures
# Normally should be part of parse_findbuch.pl on ite-srv24 - which does not
# work currently. So all expanded signatures from ite-srv24 are completely
# reset and an extended expansion is applied here.
# Adds start_sig_expanded, end_sig_expaended instead of overwriting
use strict;
use warnings;
use utf8;
use Data::Dumper;
use JSON;
use Path::Tiny;
use Readonly;
use Scalar::Util qw(looks_like_number);
## for debug output
binmode( STDOUT, ":utf8" );
my $klassdata_root = path('../data/klassdata');
my $filmdata_root = path('../data/filmdata');
my $film_web_root = path('../web/film');
# notation to country name mapping for signature extension
my %country =
%{ decode_json( $klassdata_root->child('ag_lookup.json')->slurp ) };
my %sh =
%{ decode_json( $klassdata_root->child('je_lookup.json')->slurp ) };
my %co =
%{ decode_json( $filmdata_root->child('co_lookup.json')->slurp ) };
# lookup table for geo signatures
my %geo_id;
my $klassdata_file = $klassdata_root->child("geo_by_signature.de.json");
my $klassdata_ref = decode_json( $klassdata_file->slurp );
foreach my $entry ( @{ $klassdata_ref->{results}{bindings} } ) {
$geo_id{ $entry->{signature}{value} } = $entry->{id}{value};
}
my %page = (
h => {
name => 'Hamburgisches Welt-Wirtschafts-Archiv (HWWA)',
list => {
'h1_sh' => {
title => 'Länder-Sacharchiv',
covers => 'von 1908 (z.T. früher) bis ca. 1949',
},
'h2_sh' => {
title => 'Länder-Sacharchiv',
covers => 'von ca. 1949 bis ca. 1960',
},
'h1_co' => {
title => 'Firmen- und Institutionenarchiv',
covers => 'von 1908 (z.T. früher) bis ca. 1949',
},
'h2_co' => {
title => 'Firmen- und Institutionenarchiv',
covers => 'von ca. 1949 bis ca. 1960',
},
'h1_wa' => {
title => 'Warenarchiv',
covers => 'von 1908 (z.T. früher) bis ca. 1946',
},
'h2_wa' => {
title => 'Warenarchiv',
covers => 'von ca. 1947 bis ca. 1960',
},
},
},
k => {
name => 'Wirtschaftsarchiv des Instituts für Weltwirtschaft (WiA)',
list => {
k1_sh => {
title => 'Sacharchiv',
},
k2_sh => {
title => 'Sacharchiv',
},
},
},
);
foreach my $prov (qw/ h /) {
foreach my $page_name ( sort keys %{ $page{$prov}{list} } ) {
my $coll = substr( $page_name, 3, 2 );
my $set = substr( $page_name, 0, 2 );
# read json input
my @film_sections =
@{ decode_json( $filmdata_root->child( $page_name . '.json' )->slurp ) };
# iterate through the list of film sections (from the excel file)
foreach my $film_section (@film_sections) {
my $film_id = $film_section->{film_id};
# skip film if it has no content (is only a line in the list)
next unless -d "$film_web_root/$set/$coll/$film_id";
my $id = "film/$set/$coll/$film_id";
# save already expanded signatures
my $old_start_sig = $film_section->{start_sig};
my $old_end_sig = $film_section->{end_sig};
# reset signatures to the part in square brackets
my $start_sig = "$film_section->{start_sig}";
if ( $start_sig =~ m/\[(.+)\]/ ) {
$start_sig = $1;
}
my $end_sig = "$film_section->{end_sig}";
if ( $end_sig =~ m/\[(.+)\]/ ) {
$end_sig = $1;
}
# replace expanded signatures
if ( $coll eq 'sh' ) {
( $film_section->{start_sig}, $film_section->{start_geo_id} ) =
expand_sh_signature( $start_sig, $film_id );
( $film_section->{end_sig}, $film_section->{end_geo_id} ) = expand_sh_signature( $end_sig, $film_id );
}
if ( $coll eq 'co' ) {
next unless ( $film_section->{start_sig} and $film_section->{end_sig} );
$film_section->{start_sig} =
expand_co_signature( $start_sig, $film_id );
$film_section->{end_sig} = expand_co_signature( $end_sig, $film_id );
}
if ( $coll eq 'wa' ) {
$film_section->{start_sig} =
expand_wa_signature( $start_sig, $film_id );
$film_section->{end_sig} = expand_wa_signature( $end_sig, $film_id );
}
# output old/new differences for debugging
$old_start_sig =~ s/\s+/ /g;
if ( $film_section->{start_sig} ne $old_start_sig ) {
##print "$old_start_sig ==> $film_section->{start_sig}\n";
}
$old_end_sig =~ s/\s+/ /g;
if ( $film_section->{end_sig} ne $old_end_sig ) {
##print "$old_end_sig} ==> $film_section->{end_sig}\n";
}
}
# write output (replace file)
my $out = $filmdata_root->child( $page_name . '.expanded.json' );
$out->spew( encode_json( \@film_sections ) );
}
}
########
sub clean_sh_signature {
my $signature = shift || die "param missing";
# replace individual typos
$signature =~ s/^C85 q m32$/C85 q Sm32/;
$signature =~ s/^A1 n4 sm40$/A1 n4 Sm40/;
$signature =~ s/^A24g 4 a/A29 g4a/;
# h1_sh2
$signature =~ s/^A24g$/A29 g/;
$signature =~ s/^B82/B86/;
$signature =~ s/^C102b-u2a/C102 b-u2a/;
$signature =~ s/^C20/D20/;
# geo
# blank after continent
if ( $signature =~ m/^([A-H]) (\d.*)$/ ) {
$signature = "$1$2";
}
# geo sig including small letter
if ( $signature =~ /^([A-G]\d+) ([a-z] [a-q].*)$/ ) {
## don't replace buggy signatures
if ( not( $signature =~ m/A22 i h/ or $signature =~ m/A30 q n/ or $signature =~ m/A15 g a Sm/ ) ) {
$signature = "$1$2";
}
}
# subject
# blank after first level
if ( $signature =~ m/^([A-H]\S+\s[a-q]) (\d.*)$/ ) {
## don't replace buggy signatures with missing "Sm"
if ( not $signature =~ m/q 50[123]/ ) {
$signature = "$1$2";
}
}
# extension character (e.g., m4a)
if ( $signature =~ m/^(\S+ [a-q]\d+) ([a-c].*)$/ ) {
$signature = "$1$2";
}
# blank before Sm
if ( $signature =~ m/^(.+)SM (\d.*)/i ) {
$signature = "$1Sm$2";
}
# dot before roman Sm number extension
if ( $signature =~ m/^(.+Sm\d+)([IVX].*)$/ ) {
$signature = "$1.$2";
}
return $signature;
}
sub clean_co_signature {
my $signature = shift || die "param missing";
# replace individual typos
$signature =~ s/^A 10\/19 (.*)$/A10(19) $1/;
$signature =~ s/^A10\/19 (.*)$/A10(19) $1/;
$signature =~ s/^A10A35$/A10 A35/;
$signature =~ s/^E87 B7$/E86 B7/;
$signature =~ s/^(.*?\(Magazin)$/$1\)/;
# geo
# blank after continent
if ( $signature =~ m/^([A-H]) (\d.*)$/ ) {
$signature = "$1$2";
}
# company
if ( $signature =~ m/^([A-H]\d+ [A-Z])(\d.*)$/ ) {
$signature = "$1 $2";
}
return $signature;
}
sub clean_wa_signature {
my $signature = shift || die "param missing";
# replace individual typos
$signature =~ s/^Zucker A10 I 4$/Zucker - A10 I 4/;
$signature =~ s/^Wein C19 IV$/Wein - C19 IV/;
$signature =~ s/^k35 n13a$/Eisen - A35/;
$signature =~ s/^Metalle - A35$/Eisen - A35/;
$signature =~ s/^Chemikalien B102 IV$/Chemikalien - B102 IV/;
$signature =~ s/^B58\/59$/B58 B59/;
$signature =~ s/ \/ Räder/ (Räder)/;
$signature =~ s/A1\/W/A1 IV/;
$signature =~ s/ A86/ E86/;
$signature =~ s/ B93/ C93/;
$signature =~ s/ B97/ E97/;
$signature =~ s/ B87/ C87/;
$signature =~ s/ B69/ B64/;
$signature =~ s/ A40o/ A40c/;
# with slash
if ( $signature =~ m;^(?:.+?) - (.+?)/(.+)$; ) {
$signature = "$1 $2";
}
return $signature;
}
sub expand_sh_signature {
my $signature = shift || die "param missing";
my $film_id = shift || die "param missing";
return $signature if ( $signature eq 'x' );
# squeeze multiple blanks
$signature =~ s/\s+/ /g;
my $old_signature = $signature;
$signature = clean_sh_signature($signature);
if ( $signature ne $old_signature ) {
##print "$old_signature\t->\t$signature\n";
}
$signature =~ m/^(\S+)(\s(.*))?$/;
my $geo = $1;
my $topic = $3;
if ( not $geo ) {
warn "Unmatching signature: $signature\n";
}
# expand geo
my $expanded_signature;
if ( $country{$geo} ) {
$expanded_signature = $country{$geo};
} else {
$expanded_signature = '???';
}
# get geo category id
my $geo_id = $geo_id{$geo};
# expand topic
if ($topic) {
# cleanup Sm entries
$topic =~ s/ [Ss][Mm]/ Sm/;
$topic =~ s/ Sm\s+/ Sm/;
# check backwards to recognize the longest possible substring
my @parts = split( / /, $topic );
my @unrecognized;
my $topic_expanded = '';
my $i = @parts;
PART: while ( $i > 0 ) {
# if matched
if ( $sh{ join( ' ', @parts ) } ) {
# append the unrecognized parts to the expanded recognized
$topic_expanded =
$sh{ join( ' ', @parts ) } . ' ' . join( ' ', @unrecognized );
last PART;
}
if ( $i == 1 ) {
##print "$film_id not found:\t$topic\n";
$topic_expanded = $topic;
}
my $last = pop(@parts);
unshift( @unrecognized, $last );
$i--;
}
$expanded_signature = "$expanded_signature : $topic_expanded";
}
$expanded_signature = "$expanded_signature [$signature]";
# squeeze multiple blanks
$expanded_signature =~ s/\s+/ /g;
return $expanded_signature, $geo_id;
}
sub expand_co_signature {
my $signature = shift || die "param missing";
my $film_id = shift || die "param missing";
return $signature if ( $signature eq 'x' );
return $signature if ( $signature =~ m/ \(Magazin\)$/ );
# squeeze multiple blanks
$signature =~ s/\s+/ /g;
my $old_signature = $signature;
$signature = clean_co_signature($signature);
if ( $signature ne $old_signature ) {
##print "$old_signature\t->\t$signature\n";
}
# company signature includes the geographic part
$signature =~ m/^(\S+)(\s(.*))?$/;
my $geo = $1;
my $company = $signature;
if ( not $geo ) {
warn "Unmatching signature: $signature\n";
}
# expand geo
my $expanded_signature;
if ( $country{$geo} ) {
$expanded_signature = $country{$geo};
} else {
$expanded_signature = '???';
}
# expand company
if ($company) {
# check backwards to recognize the longest possible substring
my @parts = split( / /, $company );
my @unrecognized;
my $company_expanded = '';
my $i = @parts;
PART: while ( $i > 0 ) {
# if matched
if ( $co{ join( ' ', @parts ) } ) {
# append the unrecognized parts to the expanded recognized
$company_expanded =
$co{ join( ' ', @parts ) } . ' ' . join( ' ', @unrecognized );
last PART;
}
if ( $i == 1 ) {
##print "$film_id not found:\t$company\n";
$company_expanded = $company;
}
my $last = pop(@parts);
unshift( @unrecognized, $last );
$i--;
}
# use en dash as separator
$expanded_signature = "$expanded_signature – $company_expanded";
}
$expanded_signature = "$expanded_signature [$signature]";
# squeeze multiple blanks
$expanded_signature =~ s/\s+/ /g;
return $expanded_signature;
}
sub expand_wa_signature {
my $signature = shift || die "param missing";
my $film_id = shift || die "param missing";
# squeeze multiple blanks
$signature =~ s/\s+/ /g;
my $old_signature = $signature;
$signature = clean_wa_signature($signature);
my $expanded_signature;
if ( $signature =~ m/^(.+) - (.+)$/ ) {
my $ware = $1;
my $rest = $2;
if ( $rest =~ m/([A-H]\d+[a-z]?)(?: (.+))?$/ ) {
my $geo = $1;
my $rest2 = $2 || '';
if ( $country{$geo} ) {
$expanded_signature = $country{$geo};
} else {
$expanded_signature = '???';
}
$expanded_signature = "$ware - $expanded_signature";
if ($rest2) {
$expanded_signature .= " $rest2";
}
$expanded_signature .= " [$rest]";
##print "$expanded_signature\n";
} else {
$expanded_signature = "$ware - $rest";
}
} else {
$expanded_signature = $signature;
}
return $expanded_signature;
}