-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_data.pl
executable file
·628 lines (536 loc) · 14.9 KB
/
extract_data.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
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
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV_XS;
use XML::LibXML;
use utf8;
use encoding qw(utf8);
binmode STDOUT, ":utf8";
binmode STDERR, ":utf8";
my $works_file = 'new_works25.csv';
my $refrains_file = 'new_refrains24.csv';
die "cannot find files\n" unless (-e $works_file && -e $refrains_file);
my $objects = []; #extract the works and refrains to here
my $columns = initialise_columns(); #data about columns
my $tables;
$tables->{works} = load_data($works_file);
$tables->{refrains} = load_data($refrains_file);
my $heading_maps;
$heading_maps->{works} = initialise_heading_map($tables->{works}); #mapping of table heading to column index
$heading_maps->{refrains} = initialise_heading_map($tables->{refrains});
foreach my $row ( 1 .. $#{$tables->{refrains}} )
{
next unless val('refrains','Ref Number',$row);
my $refrain = {
medmus_type => 'refrain'
}; #each row is a refrain
push @{$objects}, $refrain;
process_generic_fields($refrain, 'refrains', $row);
#manuscript collocation
my $val = val('refrains', 'Manuscript Collocation', $row);
my ($manuscript, $location) = split(/,\s*/,$val,2);
$refrain->{manuscript_id} = $manuscript;
$refrain->{manuscript_location} = $location;
#remove witespace
$refrain->{'parent_work_id'} =~ s/\s//g;
#reading texts
foreach my $c (@{$columns->{refrains}->{complex_cols}})
{
my $val = val('refrains',$c, $row);
next unless $val;
if ($c eq 'Derived Reading Text')
{
push @{$refrain->{reading_texts}}, { text => $val };
}
elsif ($c =~ m/^Derived Reading Text \((.*)\)/)
{
my $position = $1;
my $reading = {
text => $val,
};
if ($c eq 'Derived Reading Text (Refrain 1 [sur l\'incipit français])')
{
$reading->{refrain} = '1_slf';
}
elsif ($position =~ m/;/) #two part position
{
foreach my $part (split(/\s*;\s*/, $position))
{
if ($part =~ m/[Rr]efrain\s*(.*)/)
{
my $r = lc($1);
$r =~ s/\s//g; #remove spaces
$reading->{refrain} = lc($r);
}
elsif ($part =~ m/st\.\s*([0-9]+)/)
{
$reading->{stanza} = lc($1);
}
elsif ($part =~ m/envoi\s*([I]*)/)
{
$reading->{envoi} = $1;
}
elsif ($part eq 'Fatras')
{
#do nothing?
}
else
{
die "unrecognised part in $position\n";
}
}
}
elsif ($position =~ m/^[Rr]efrain\s*(.*)/)
{
$reading->{refrain} = lc($1);
}
else
{
$reading->{refrain} = lc($position);
}
push @{$refrain->{reading_texts}}, $reading;
}
}
}
#process the rows
foreach my $row ( 1 .. $#{$tables->{works}} )
{
next unless val('works','ID', $row); #skip if there's no ID -- it's probably a blank line
my $work = {
medmus_type => 'work'
}; #each row is a reading
push @{$objects}, $work;
process_generic_fields($work, 'works', $row);
#manuscript collocation
my $val = val('works', 'Manuscript Collocation', $row);
my ($manuscript, $location) = split(/,\s*/,$val,2);
$work->{manuscript_id} = $manuscript;
$work->{manuscript_location} = $location;
#remove whitespace from work_id
$work->{'work_id'} =~ s/\s//g;
$work->{'host_work_id'} =~ s/\s//g if $work->{'host_work_id'};
#copy across the lu index
if ($work->{'work_id'} =~ m/^M([0-9].*)/)
{
$work->{lu_index} = $1 unless $work->{lu_index}
}
#authors
$val = val('works','Authors', $row);
my @author_strings = split(/\s*;\s*/, $val);
my $authors = [];
foreach my $author_string (@author_strings)
{
my $author = {};
#grab the bit after the name that's in brackets (if present)
if ($author_string =~ m/^(.*)\((.*)\)(.*)$/)
{
$author->{locations} = $2;
$author_string = $1 . ' ' . $3; #preserve question marks on either side of the bracketed bit
}
#if there's a question mark, it's an assumption
if ($author_string =~ m/^(.*)\?\s*$/)
{
$author->{assumed} = 'TRUE';
$author_string = $1;
}
else
{
$author->{assumed} = 'FALSE';
}
#strip leading and trailing space
$author_string =~ s/^\s*//;
$author_string =~ s/\s*$//;
#whatever is left is the name
$author->{name} = $author_string;
push @{$authors}, $author;
}
$work->{authors} = $authors;
}
#data is the data_object being built
#table is 'works' or 'refrains'
#row is the row number we're currently in
sub process_generic_fields
{
my ($data, $table, $row) = @_;
#simple values -- just copy in
while ( my ($heading, $tagname) = each %{$columns->{$table}->{simple}} )
{
my $val = val($table,$heading, $row);
my $single_with_semicolon = {
'Abstract Master Text' => 1,
'Abstract Title' => 1,
};
if ($val)
{
if (
$val =~ m/;/
&& !$single_with_semicolon->{$heading}
)
{
die "$table: Semicolon in singular field $heading, row $row\n";
}
$data->{$tagname} = $val;
}
}
#simple multiple values -- just copy in
while ( my ($heading, $tagname) = each %{$columns->{$table}->{multiple}} )
{
my $val = val($table,$heading, $row);
if (defined $val)
{
#bad data in source(?)
$val =~ s/3 \(F-Pn fr. 25566\)/3/g if $heading eq 'Number of parts';
my @val = split(/\s*;\s*/,$val);
#problem data
$data->{$tagname} = [@val] if scalar @val;
}
}
#set values
while ( my ($heading, $col) = each %{$columns->{$table}->{set}} )
{
my $val = val($table,$heading, $row);
next unless $val;
my $value; #this is what will be set
if ($col->{multiple})
{
my @val = split(/\s*;\s*/,$val);
foreach my $k (@val)
{
my $v = $col->{options}->{$k};
if (!$v)
{
die "Unrecognised set member $k in column $heading, row $row\n";
}
push @{$value}, $v;
}
}
else
{
my $v = $col->{options}->{$val};
if (!$v)
{
die "Unrecognised set member $val in column $heading, row $row\n";
}
$value = $v;
}
$data->{$col->{tagname}} = $value;
}
}
output_epxml($objects);
sub output_epxml
{
my ($arr) = @_;
my $doc = XML::LibXML::Document->new();
my $eprints = $doc->createElement('eprints');
foreach my $work (@{$arr})
{
my $eprint = $doc->createElement('eprint');
$eprints->appendChild($eprint);
foreach my $tagname (keys %{$work})
{
if ($work->{$tagname})
{
$eprint->appendChild(construct_val($tagname, $work->{$tagname}, $doc));
}
}
}
print $eprints->toString;
}
sub construct_val
{
my ($tagname, $val, $doc) = @_;
my $el = $doc->createElement($tagname);
if (!ref $val)
{
#exception for single questionmark in the number_of_stanzas tag
return $el if ($columns->{works}->{strip_questionmark_vals}->{$tagname} and $val eq '?');
if ($columns->{works}->{replace_questionmark_vals}->{$tagname} and $val eq '?')
{
$el->appendText($columns->{works}->{replace_questionmark_vals}->{$tagname});
}
else
{
$el->appendText($val);
}
return $el;
}
if (ref $val eq 'ARRAY')
{
foreach my $v (@{$val})
{
my $item = $doc->createElement('item');
$el->appendChild($item);
if (!ref $v)
{
$item->appendText($v);
}
elsif (ref $v eq 'HASH')
{
foreach my $k (keys %{$v})
{
next unless $v->{$k}; #ignore empty elements;
my $t = $doc->createElement($k);
$t->appendText($v->{$k});
$item->appendChild($t);
}
}
else
{
die "Unexpected content of array when generating XML\n";
}
}
}
return $el;
}
sub val
{
my ($table, $heading, $row) = @_;
if (!defined $heading_maps->{$table}->{$heading})
{
die "Undefined $heading in $table table\n";
}
return $tables->{$table}->[$row]->[$heading_maps->{$table}->{$heading}];
}
sub load_data
{
my ($filename) = @_;
use Text::CSV_XS;
my $csv = Text::CSV_XS->new ({ binary => 1 });
open my $fh, "<:encoding(utf8)", $filename or die "$filename: $!";
my $spreadsheet = $csv->getline_all($fh);
return $spreadsheet;
}
sub initialise_columns
{
my $c = {};
$c->{works}->{simple} =
{
"ID" => 'work_id',
"Instances of works" => 'instance_number',
"Abstract Title" => 'abstract_work_title',
"Instance Title" => 'title_input',
"Number of stanzas" => 'number_of_stanzas',
"Number of envois" => 'number_of_envois',
"RS Index" => 'rs_index',
"T Index (Motets)" => 't_index_motets',
"T Index (Songs)" => 't_index_songs',
"Lu Index" => 'lu_index',
"M Index" => 'm_index',
"vdB Index (Rondeaux)" => 'vdb_index',
"Number of parts" => 'number_of_parts',
"Host work ID" => 'host_work_id',
"Host Work Instance" => 'host_work_instance',
"Host Work Location" => 'location_in_host',
};
$c->{refrains}->{multiple} =
{
'Singer' => 'singer',
'Destinataire' => 'audience',
'Fonction' => 'function',
'Succeeding' => 'succeeding_lyric',
'Preceding' => 'preceeding_lyric',
# 'Marque de discours' => 'mark_of_discourse',
'Métrique' => 'meter',
'Other Refrain data' => 'other_refrain_data',
'Other Manuscript Data' => 'other_manuscript_data',
};
$c->{refrains}->{simple} =
{
'Parent Work (ID)' => 'parent_work_id',
'Parent Work (Instance)' => 'parent_work_instance',
'Parent Work (Location)' => 'location_in_parent',
'Ref Number' => 'refrain_id',
'Instance Number' => 'instance_number',
'Linker Number' => 'linker_number',
'Abstract Master Text' => 'abstract_text',
# 'Occurance Number' => 'occurence_number',
# 'Host Work' => '',
'Circonstances' => 'circumstance',
# 'Manuscript Number' => '',
'Musical structure' => 'musical_structure',
'Image File' => 'image_file',
};
$c->{refrains}->{set} =
{
'Refrain Location' =>
{
tagname => 'refrain_location',
multiple => 1,
options => {
'Enté' => 'enté',
'Enté interne' => 'enté_interne',
'Fin de strophe' => 'fin_de_strophe',
'Fin de stophe' => 'fin_de_strophe',
'Fin de strophe' => 'fin_de_strophe',
'Final' => 'final',
'Initial' => 'initial',
'Interne' => 'interne',
}
},
};
$c->{refrains}->{complex_cols} =
[
'Image File',
'Manuscript Collocation',
'Derived Reading Text',
'Derived Reading Text (Refrain 1 [sur l\'incipit français])',
'Derived Reading Text (Refrain 1)',
'Derived Reading Text (Refrain 2)',
'Derived Reading Text (Refrain 3)',
'Derived Reading Text (Refrain 4)',
'Derived Reading Text (Refrain 5)',
'Derived Reading Text (Refrain 6)',
'Derived Reading Text (Refrain triplum)',
'Derived Reading Text (Refrain duplum)',
'Derived Reading Text (Refrain teneur)',
'Derived Reading Text (Initial)',
'Derived Reading Text (Final)',
'Derived Reading Text (st. 1; refrain enté)',
'Derived Reading Text (st. 1; refrain initial)',
'Derived Reading Text (st. 1; refrain interne)',
'Derived Reading Text (st. 1 ; refrain interne 2)',
'Derived Reading Text (st. 1 ; refrain final)',
'Derived Reading Text (st. 2 ; refrain enté)',
'Derived Reading Text (st. 2 ; refrain initial)',
'Derived Reading Text (st. 2 ; refrain interne)',
'Derived Reading Text (st. 2 ; refrain interne 2)',
'Derived Reading Text (st. 2 ; refrain final)',
'Derived Reading Text (st. 3 ; refrain initial)',
'Derived Reading Text (st. 3 ; refrain interne)',
'Derived Reading Text (st. 3 ; refrain interne 2)',
'Derived Reading Text (st. 3 ; refrain final)',
'Derived Reading Text (st. 4 ; refrain initial)',
'Derived Reading Text (st. 4 ; refrain interne)',
'Derived Reading Text (st. 4 ; refrain interne 2)',
'Derived Reading Text (st. 4 ; refrain final)',
'Derived Reading Text (st. 5 ; refrain initial)',
'Derived Reading Text (st. 5 ; refrain interne)',
'Derived Reading Text (st. 5 ; refrain interne 2)',
'Derived Reading Text (st. 5 ; refrain final)',
'Derived Reading Text (st. 6 ; refrain initial)',
'Derived Reading Text (st. 6 ; refrain interne)',
'Derived Reading Text (st. 6 ; refrain interne 2)',
'Derived Reading Text (st. 6 ; refrain final)',
'Derived Reading Text (st. 7 ; refrain initial)',
'Derived Reading Text (st. 7 ; refrain interne)',
'Derived Reading Text (st. 7 ; refrain interne 2)',
'Derived Reading Text (st. 7 ; refrain final)',
'Derived Reading Text (st. 8 ; refrain initial)',
'Derived Reading Text (st. 8 ; refrain interne)',
'Derived Reading Text (st. 8 ; refrain interne 2)',
'Derived Reading Text (st. 8 ; refrain final)',
'Derived Reading Text (st. 9 ; refrain initial)',
'Derived Reading Text (st. 9 ; refrain interne)',
'Derived Reading Text (st. 9 ; refrain interne 2)',
'Derived Reading Text (st. 9 ; refrain final)',
'Derived Reading Text (st. 10 ; refrain initial)',
'Derived Reading Text (st. 10 ; refrain final)',
'Derived Reading Text (st. 11 ; refrain final)',
'Derived Reading Text (st. 12 ; refrain final)',
'Derived Reading Text (st. 13 ; refrain final)',
'Derived Reading Text (st. 14 ; refrain final)',
'Derived Reading Text (st. 15 ; refrain final)',
'Derived Reading Text (st. 16 ; refrain final)',
'Derived Reading Text (st. 17 ; refrain final)',
'Derived Reading Text (st. 18 ; refrain final)',
'Derived Reading Text (st. 19 ; refrain final)',
'Derived Reading Text (st. 20 ; refrain final)',
'Derived Reading Text (st. 21 ; refrain final)',
'Derived Reading Text (st. 22 ; refrain final)',
'Derived Reading Text (st. 23 ; refrain final)',
'Derived Reading Text (st. 24 ; refrain final)',
'Derived Reading Text (st. 25 ; refrain final)',
'Derived Reading Text (envoi I ; refrain final)',
'Derived Reading Text (envoi II ; refrain initial)',
'Derived Reading Text (envoi II ; refrain final)',
'Derived Reading Text (Refrain 1 ; Fatras)',
'Derived Reading Text (Refrain 2 ; Fatras)',
];
$c->{works}->{multiple} =
{
"Other data" => 'other_data',
"MW Index" => 'mw_index',
"L Index" => 'l_index',
"Edition" => 'edition',
"Date" => 'date_description',
"Generic Descriptor" => 'generic_descriptor',
"Author commentary" => 'author_commentary',
};
$c->{works}->{set} =
{
"Primary Language" =>
{
tagname => 'primary_language',
options =>
{
'Langue d\'oïl' => 'langue_d_oil',
'Langue d’oïl' => 'langue_d_oil',
'Latin' => 'latin'
},
multiple => 0
},
"Secondary Language" =>
{
tagname => 'secondary_language',
options =>
{
'Langue d\'oïl' => 'langue_d_oil',
'Latin' => 'latin'
},
multiple => 0
},
"Style of discourse" =>
{
tagname => 'style_of_discourse',
options =>
{
'Vers' => 'vers',
'Prose asonancée' => 'prose_asonancee',
'Prose' => 'prose',
},
multiple => 1
},
"Voice in the Polyphony" =>
{
tagname => 'voice_in_polyphony',
options =>
{
Duplum => 'duplum',
Triplum => 'triplum',
Quadruplum => 'quadruplum',
Motet => 'motet',
Teneur => 'teneur',
},
multiple => 1,
}
};
$c->{works}->{complex_cols} =
{
"Authors" => 'authors',
};
#these fields may contain a '?' -- treat as undef
$c->{works}->{strip_questionmark_vals} =
{
# 'number_of_stanzas' => 1,
};
$c->{works}->{replace_questionmark_vals} =
{
'number_of_stanzas' => 'Inconnu',
};
return $c;
}
#populate heading_map
sub initialise_heading_map
{
my ($sheet) = @_;
my $heading_map = {};
foreach my $col (0 .. $#{$sheet->[0]})
{
my $heading = $sheet->[0]->[$col];
if ($heading)
{
$heading_map->{$heading} = $col
}
}
return $heading_map;
};