-
Notifications
You must be signed in to change notification settings - Fork 7
/
msdesc2solr.xquery
1076 lines (974 loc) · 44 KB
/
msdesc2solr.xquery
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
module namespace bod="http://www.bodleian.ox.ac.uk/bdlss";
import module namespace functx = "http://www.functx.com" at "functx.xquery";
import module namespace lang = "http://www.bodleian.ox.ac.uk/bdlss/lang" at "languages.xquery";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace html="http://www.w3.org/1999/xhtml";
(:~
: --------------------------------
: Function Library for TEI catalogues
: --------------------------------
: This library is for useful generic functions (date handling, language code lookups, etc)
: and functions specific to building the Solr files for specific indexes, but likely to be
: shared across multiple TEI catalogues
:
:)
declare variable $bod:disablelogging as xs:boolean external := false();
declare variable $bod:nonwordregex as xs:string external := concat('["', "\s'\-\[\]\(\)\{\}]");
declare variable $bod:wordregex as xs:string external := concat('[^"', "\s'\-\[\]\(\)\{\}]");
declare variable $bod:yearregex as xs:string external := "\-?\d\d\d\d";
(:~
: --------------------------------
: Generic helper functions
: --------------------------------
:)
declare function bod:logging($level, $msg, $values)
{
if (not($bod:disablelogging)) then
(: Trick XQuery into doing trace() to output message to STDERR but not insert it into the XML :)
substring(trace('', concat(upper-case($level), ' ', $msg, ' ', string-join($values, ' '), ' ')), 0, 0)
else ()
};
declare function bod:ordinal($num as xs:integer) as xs:string
{
(: TODO: Reimplement this using modulo to do 22nd, 31st, 101st, 211th, etc :)
switch($num)
case 1 return "st"
case 2 return "nd"
case 3 return "rd"
case 21 return "st"
case 22 return "nd"
case 23 return "rd"
default return "th"
};
declare function bod:shortenToNearestWord($stringval as xs:string, $tolength as xs:integer) as xs:string
{
let $cutoffat as xs:integer := $tolength - 1
let $nsstringval as xs:string := normalize-space($stringval)
return if (string-length($nsstringval) le $tolength) then
(: Already short enough, so return unmodified :)
$nsstringval
else if (substring($nsstringval, $cutoffat, 1) = (' ', '	', ' ')) then
(: The cut-off is at the location of some whitespace, so won't be cutting off any words :)
concat(normalize-space(substring($nsstringval, 1, $cutoffat)), '…')
else if (substring($nsstringval, $tolength, 1) = (' ', '	', ' ')) then
(: The cut-off is at the end of a word, so won't be cutting off any words :)
concat(substring($nsstringval, 1, $cutoffat), '…')
else
(: The cut-off is in the middle of a word, so return everything up to the preceding word :)
concat(replace(substring($nsstringval, 1, $cutoffat), '\s\S*$', ''), '…')
};
declare function bod:formatCentury($centuryNum as xs:integer) as xs:string
{
(: Converts century in number form (negative integers for BCE, positive integers for CE) into human-readable form :)
if ($centuryNum gt 0) then
concat($centuryNum, bod:ordinal($centuryNum), ' Century')
else
concat(abs($centuryNum), bod:ordinal(abs($centuryNum)), ' Century BCE')
};
declare function bod:findCenturies($earliestYear as xs:string*, $latestYear as xs:string*) as xs:string*
{
(: Converts a year range into a sequence of century names.
Input parametes are strings containing four-digit years (e.g. "0050", "0500", "1500")
BCE dates should be prefixed with a minus (e.g. "-0100")
For single years, pass it as first param and an empty string for the second, or both. :)
(: Zero below stands for null, as there is no Year 0 :)
let $ey as xs:double := number(functx:if-empty($earliestYear, 0))
let $ly as xs:double := number(functx:if-empty($latestYear, 0))
let $earliestIsTurnOfCentury as xs:boolean := ends-with($earliestYear, '00')
let $latestIsTurnOfCentury as xs:boolean := ends-with($latestYear, '00')
(: Convert years to centuries. Special cases required for turn-of-the-century years, e.g. 1500 AD is treated
as 16th century if at the start of a range, or the only known year, but as 15th if at the end of a range;
while 200 BC is treated as 3rd century BCE if at the end of a range but as 2nd BCE at the start. :)
let $earliestCentury as xs:integer := xs:integer(
if (string($ey) eq 'NaN') then
(0)
else if ($ey gt 0 and $earliestIsTurnOfCentury) then
($ey div 100) + 1
else if ($ey lt 0) then
floor($ey div 100)
else
ceiling($ey div 100)
)
let $latestCentury as xs:integer := xs:integer(
if (string($ly) eq 'NaN') then
(0)
else if ($ly lt 0 and $latestIsTurnOfCentury) then
($ly div 100) - 1
else if ($ly lt 0) then
floor($ly div 100)
else
ceiling($ly div 100)
)
return
if ($ey gt $ly and $ly ne 0) then
bod:logging('info', 'Date range not valid', concat($earliestYear, '-', $latestYear))
else if ($earliestCentury ne 0 and $latestCentury ne 0) then
(: A date range, something like "After 1400 and before 1650", so fill in all the possible centuries between :)
for $century in (-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)
return
if ($century ge $earliestCentury and $century le $latestCentury) then
bod:formatCentury($century)
else
()
else if ($earliestCentury ne 0 or $latestCentury ne 0) then
(: Only a single date, either a precise year or an open-ended range like "Before 1500" or "After 1066", so just output the known century :)
bod:formatCentury(($earliestCentury, $latestCentury)[. ne 0])
else
bod:logging('info', 'Unreadable dates', concat($earliestYear, '-', $latestYear))
};
declare function bod:summarizeDates($teinodes as element()*) as xs:string?
{
let $years :=
for $date in $teinodes/(@when|@notBefore|@notAfter|@from|@to)/normalize-space()
return functx:get-matches($date, $bod:yearregex)[1]
let $earliestYear := min($years)
let $latestYear := max($years)
let $centuries :=
if (string-length($earliestYear) gt 0 and string-length($latestYear) gt 0) then
bod:findCenturies($earliestYear, $latestYear)
else if (string-length($earliestYear) gt 0) then
bod:findCenturies($earliestYear, '')
else if (string-length($latestYear) gt 0) then
bod:findCenturies('', $latestYear)
else ()
return
if (count($centuries) eq 0) then
()
else if (count($centuries) eq 1) then
$centuries[1]
else
concat($centuries[1], ' – ', $centuries[count($centuries)])
};
declare function bod:personRoleLookup($role as xs:string) as xs:string
{
let $lcrole := lower-case($role)
return
if ($lcrole eq 'author') then
"Author"
else if ($lcrole eq 'editor') then
"Editor"
else if ($lcrole eq 'subject') then
"Subject of a work"
else if (string-length($role) eq 3) then
(: Expecting three-char MARC relator codes. Merge and rename
some special cases, but otherwise lookup label. :)
if ($lcrole = ('fmo', 'sgn', 'dnr')) then
"Owner, signer, or donor"
else if ($lcrole = ('dte', 'pat')) then
"Commissioner, dedicatee, or patron"
else if ($lcrole eq 'bsl') then
"Stationer or bookseller"
else
let $relator as xs:string := bod:roleLookupMarcCode($lcrole)
return if ($relator ne '') then $relator else concat('Unknown role code: ', $lcrole)
else
(: Anything else, assume it is a label, and display as-is, except capitalized :)
functx:capitalize-first($role)
};
declare function bod:personRoleLookup2($role as xs:string) as xs:string
{
(: Same as previous function but without merging and renaming of special cases, which originated with the Medieval catalogue :)
let $lcrole := lower-case($role)
return
if ($lcrole eq 'author') then
"Author"
else if ($lcrole eq 'editor') then
"Editor"
else if ($lcrole eq 'subject') then
"Subject of a work"
else if (string-length($role) eq 3) then
(: Expecting three-char MARC relator codes. Lookup label. :)
let $relator as xs:string := bod:roleLookupMarcCode($lcrole)
return if ($relator ne '') then $relator else concat('Unknown role code: ', $lcrole)
else
(: Anything else, assume it is a label, and display as-is, except capitalized :)
functx:capitalize-first($role)
};
declare function bod:roleLookupMarcCode($rolecode as xs:string) as xs:string
{
(: This is the MARC Code List for Relators standard, copy taken on 2018-06-22 from http://id.loc.gov/vocabulary/relators.tsv :)
switch (lower-case($rolecode))
case 'abr' return "Abridger"
case 'acp' return "Art copyist"
case 'act' return "Actor"
case 'adi' return "Art director"
case 'adp' return "Adapter"
case 'aft' return "Author of afterword or colophon"
case 'anl' return "Analyst"
case 'anm' return "Animator"
case 'ann' return "Annotator"
case 'ant' return "Bibliographic antecedent"
case 'ape' return "Appellee"
case 'apl' return "Appellant"
case 'app' return "Applicant"
case 'aqt' return "Author in quotations or text abstracts"
case 'arc' return "Architect"
case 'ard' return "Artistic director"
case 'arr' return "Arranger"
case 'art' return "Artist"
case 'asg' return "Assignee"
case 'asn' return "Associated name"
case 'ato' return "Autographer"
case 'att' return "Attributed name"
case 'auc' return "Auctioneer"
case 'aud' return "Author of dialog"
case 'aui' return "Author of introduction"
case 'aus' return "Screenwriter"
case 'aut' return "Author"
case 'bdd' return "Binding designer"
case 'bjd' return "Bookjacket designer"
case 'bkd' return "Book designer"
case 'bkp' return "Book producer"
case 'blw' return "Blurb writer"
case 'bnd' return "Binder"
case 'bpd' return "Bookplate designer"
case 'brd' return "Broadcaster"
case 'brl' return "Braille embosser"
case 'bsl' return "Bookseller"
case 'cas' return "Caster"
case 'ccp' return "Conceptor"
case 'chr' return "Choreographer"
case 'cli' return "Client"
case 'cll' return "Calligrapher"
case 'clr' return "Colorist"
case 'clt' return "Collotyper"
case 'cmm' return "Commentator"
case 'cmp' return "Composer"
case 'cmt' return "Compositor"
case 'cnd' return "Conductor"
case 'cng' return "Cinematographer"
case 'cns' return "Censor"
case 'coe' return "Contestant-appellee"
case 'col' return "Collector"
case 'com' return "Compiler"
case 'con' return "Conservator"
case 'cor' return "Collection registrar"
case 'cos' return "Contestant"
case 'cot' return "Contestant-appellant"
case 'cou' return "Court governed"
case 'cov' return "Cover designer"
case 'cpc' return "Copyright claimant"
case 'cpe' return "Complainant-appellee"
case 'cph' return "Copyright holder"
case 'cpl' return "Complainant"
case 'cpt' return "Complainant-appellant"
case 'cre' return "Creator"
case 'crp' return "Correspondent"
case 'crr' return "Corrector"
case 'crt' return "Court reporter"
case 'csl' return "Consultant"
case 'csp' return "Consultant to a project"
case 'cst' return "Costume designer"
case 'ctb' return "Contributor"
case 'cte' return "Contestee-appellee"
case 'ctg' return "Cartographer"
case 'ctr' return "Contractor"
case 'cts' return "Contestee"
case 'ctt' return "Contestee-appellant"
case 'cur' return "Curator"
case 'cwt' return "Commentator for written text"
case 'dbp' return "Distribution place"
case 'dfd' return "Defendant"
case 'dfe' return "Defendant-appellee"
case 'dft' return "Defendant-appellant"
case 'dgg' return "Degree granting institution"
case 'dgs' return "Degree supervisor"
case 'dis' return "Dissertant"
case 'dln' return "Delineator"
case 'dnc' return "Dancer"
case 'dnr' return "Donor"
case 'dpc' return "Depicted"
case 'dpt' return "Depositor"
case 'drm' return "Draftsman"
case 'drt' return "Director"
case 'dsr' return "Designer"
case 'dst' return "Distributor"
case 'dtc' return "Data contributor"
case 'dte' return "Dedicatee"
case 'dtm' return "Data manager"
case 'dto' return "Dedicator"
case 'dub' return "Dubious author"
case 'edc' return "Editor of compilation"
case 'edm' return "Editor of moving image work"
case 'edt' return "Editor"
case 'egr' return "Engraver"
case 'elg' return "Electrician"
case 'elt' return "Electrotyper"
case 'eng' return "Engineer"
case 'enj' return "Enacting jurisdiction"
case 'etr' return "Etcher"
case 'evp' return "Event place"
case 'exp' return "Expert"
case 'fac' return "Facsimilist"
case 'fds' return "Film distributor"
case 'fld' return "Field director"
case 'flm' return "Film editor"
case 'fmd' return "Film director"
case 'fmk' return "Filmmaker"
case 'fmo' return "Former owner"
case 'fmp' return "Film producer"
case 'fnd' return "Funder"
case 'fpy' return "First party"
case 'frg' return "Forger"
case 'gis' return "Geographic information specialist"
case 'his' return "Host institution"
case 'hnr' return "Honoree"
case 'hst' return "Host"
case 'ill' return "Illustrator"
case 'ilu' return "Illuminator"
case 'ins' return "Inscriber"
case 'inv' return "Inventor"
case 'isb' return "Issuing body"
case 'itr' return "Instrumentalist"
case 'ive' return "Interviewee"
case 'ivr' return "Interviewer"
case 'jud' return "Judge"
case 'jug' return "Jurisdiction governed"
case 'lbr' return "Laboratory"
case 'lbt' return "Librettist"
case 'ldr' return "Laboratory director"
case 'led' return "Lead"
case 'lee' return "Libelee-appellee"
case 'lel' return "Libelee"
case 'len' return "Lender"
case 'let' return "Libelee-appellant"
case 'lgd' return "Lighting designer"
case 'lie' return "Libelant-appellee"
case 'lil' return "Libelant"
case 'lit' return "Libelant-appellant"
case 'lsa' return "Landscape architect"
case 'lse' return "Licensee"
case 'lso' return "Licensor"
case 'ltg' return "Lithographer"
case 'lyr' return "Lyricist"
case 'mcp' return "Music copyist"
case 'mdc' return "Metadata contact"
case 'med' return "Medium"
case 'mfp' return "Manufacture place"
case 'mfr' return "Manufacturer"
case 'mod' return "Moderator"
case 'mon' return "Monitor"
case 'mrb' return "Marbler"
case 'mrk' return "Markup editor"
case 'msd' return "Musical director"
case 'mte' return "Metal-engraver"
case 'mtk' return "Minute taker"
case 'mus' return "Musician"
case 'nrt' return "Narrator"
case 'opn' return "Opponent"
case 'org' return "Originator"
case 'orm' return "Organizer"
case 'osp' return "Onscreen presenter"
case 'oth' return "Other"
case 'own' return "Owner"
case 'pan' return "Panelist"
case 'pat' return "Patron"
case 'pbd' return "Publishing director"
case 'pbl' return "Publisher"
case 'pdr' return "Project director"
case 'pfr' return "Proofreader"
case 'pht' return "Photographer"
case 'plt' return "Platemaker"
case 'pma' return "Permitting agency"
case 'pmn' return "Production manager"
case 'pop' return "Printer of plates"
case 'ppm' return "Papermaker"
case 'ppt' return "Puppeteer"
case 'pra' return "Praeses"
case 'prc' return "Process contact"
case 'prd' return "Production personnel"
case 'pre' return "Presenter"
case 'prf' return "Performer"
case 'prg' return "Programmer"
case 'prm' return "Printmaker"
case 'prn' return "Production company"
case 'pro' return "Producer"
case 'prp' return "Production place"
case 'prs' return "Production designer"
case 'prt' return "Printer"
case 'prv' return "Provider"
case 'pta' return "Patent applicant"
case 'pte' return "Plaintiff-appellee"
case 'ptf' return "Plaintiff"
case 'pth' return "Patent holder"
case 'ptt' return "Plaintiff-appellant"
case 'pup' return "Publication place"
case 'rbr' return "Rubricator"
case 'rcd' return "Recordist"
case 'rce' return "Recording engineer"
case 'rcp' return "Addressee"
case 'rdd' return "Radio director"
case 'red' return "Redaktor"
case 'ren' return "Renderer"
case 'res' return "Researcher"
case 'rev' return "Reviewer"
case 'rpc' return "Radio producer"
case 'rps' return "Repository"
case 'rpt' return "Reporter"
case 'rpy' return "Responsible party"
case 'rse' return "Respondent-appellee"
case 'rsg' return "Restager"
case 'rsp' return "Respondent"
case 'rsr' return "Restorationist"
case 'rst' return "Respondent-appellant"
case 'rth' return "Research team head"
case 'rtm' return "Research team member"
case 'sad' return "Scientific advisor"
case 'sce' return "Scenarist"
case 'scl' return "Sculptor"
case 'scr' return "Scribe"
case 'sds' return "Sound designer"
case 'sec' return "Secretary"
case 'sgd' return "Stage director"
case 'sgn' return "Signer"
case 'sht' return "Supporting host"
case 'sll' return "Seller"
case 'sng' return "Singer"
case 'spk' return "Speaker"
case 'spn' return "Sponsor"
case 'spy' return "Second party"
case 'srv' return "Surveyor"
case 'std' return "Set designer"
case 'stg' return "Setting"
case 'stl' return "Storyteller"
case 'stm' return "Stage manager"
case 'stn' return "Standards body"
case 'str' return "Stereotyper"
case 'tcd' return "Technical director"
case 'tch' return "Teacher"
case 'ths' return "Thesis advisor"
case 'tld' return "Television director"
case 'tlp' return "Television producer"
case 'trc' return "Transcriber"
case 'trl' return "Translator"
case 'tyd' return "Type designer"
case 'tyg' return "Typographer"
case 'uvp' return "University place"
case 'vac' return "Voice actor"
case 'vdg' return "Videographer"
case 'wac' return "Writer of added commentary"
case 'wal' return "Writer of added lyrics"
case 'wam' return "Writer of accompanying material"
case 'wat' return "Writer of added text"
case 'wdc' return "Woodcutter"
case 'wde' return "Wood engraver"
case 'win' return "Writer of introduction"
case 'wit' return "Witness"
case 'wpr' return "Writer of preface"
case 'wst' return "Writer of supplementary textual content"
default return bod:logging('warn', 'Unknown role code', $rolecode)
};
declare function bod:orgRoleLookup($role as xs:string) as xs:string
{
(: For the moment, using the same roles as persons. But could add organization-specific ones here. :)
let $normalizedRole := bod:personRoleLookup($role)
return $normalizedRole
};
declare function bod:physFormLookup($form as xs:string) as xs:string
{
switch(lower-case($form))
case 'concertina_book' return 'Concertina book'
case 'concertina__book' return 'Concertina book'
case 'rolled_book' return 'Rolled book'
case 'palm_leaf' return 'Palm leaf'
case 'modern_notebook' return 'Modern notebook'
case 'printed_book' return 'Printed book'
default return functx:capitalize-first($form)
};
declare function bod:isLeadingStopWord($word as xs:string*) as xs:boolean
{
let $result := switch(lower-case($word))
case 'the' return true()
case 'a' return true()
case 'an' return true()
case 'le' return true()
case 'la' return true()
case 'les' return true()
case 'l' return true()
case 'il' return true()
case 'li' return true()
case 'der' return true()
case 'die' return true()
case 'das' return true()
case 'al' return true()
default return false()
return $result
};
declare function bod:isStopWord($word as xs:string) as xs:boolean
{
let $result := switch(lower-case($word))
case 'and' return true()
case 'of' return true()
case 'for' return true()
default return bod:isLeadingStopWord($word)
return $result
};
declare function bod:stripStopWords($string as xs:string) as xs:string
{
let $tokens := tokenize($string, "[ ']")
let $stopwordsfound := distinct-values(for $token in $tokens return if (bod:isStopWord($token)) then $token else ())
return if (count($stopwordsfound) gt 0) then
let $pattern := concat("(", string-join($stopwordsfound, "|"), ")[ ']")
return replace($string, $pattern, "", "i")
else $string
};
declare function bod:stripLeadingStopWordsOld($string as xs:string) as xs:string
{
let $tokens := tokenize($string, "[ ']")
return if (bod:isLeadingStopWord($tokens[1])) then replace($string, "^.+?[ ']", "", "i") else $string
};
declare function bod:stripLeadingStopWords($string as xs:string) as xs:string
{
let $tokens := tokenize($string, $bod:nonwordregex)[string-length(.) gt 0]
return if (bod:isLeadingStopWord($tokens[1])) then replace($string, concat('^(', $bod:nonwordregex, '*)', $bod:wordregex, '+', $bod:nonwordregex, '+'), '$1') else $string
};
declare function bod:alphabetizeTitle($string as xs:string) as xs:string
{
let $firstLetter := functx:capitalize-first(substring(replace(bod:stripLeadingStopWords($string), '[^\p{L}|\p{N}]+', ''), 1, 1))
return $firstLetter
};
declare function bod:alphabetize($string as xs:string) as xs:string
{
let $firstLetter := functx:capitalize-first(substring(replace($string, '[^\p{L}|\p{N}]+', ''), 1, 1))
return $firstLetter
};
declare function bod:pathFromCollections($fullpath as xs:string) as xs:string
{
let $relativepath as xs:string := substring-after($fullpath, 'collections/')
return $relativepath
};
declare function bod:italicizeTitles($elem as element()) as xs:string
{
normalize-space(
string-join(
for $x in $elem//(text()|tei:title|tei:ref[@target and matches(@target, '^(#|http)')])
return
if ($x/self::tei:title and not($x/ancestor::tei:ref[@target and matches(@target, '^(#|http)')]) and not($x/parent::tei:ref[@target and matches(@target, '^(#|http)')])) then
concat('<i>', normalize-space(string-join($x//text())), '</i>')
else if ($x/self::tei:ref[@target and matches(@target, '^(#|http)')]) then
let $link as xs:string :=
concat('<a href="',
if (starts-with($x/@target, '#')) then
concat('/catalog/', substring-after($x/@target/string(), '#'), '">')
else
concat($x/@target/string(), '" target="_blank">')
, normalize-space(string-join($x//text()))
, '</a>'
)
return
if ($x/tei:title or $x/parent::tei:title) then
concat('<i>', $link, '</i>')
else
$link
else if (not($x/ancestor::tei:title) and not($x/ancestor::tei:ref[@target and matches(@target, '^(#|http)')])) then
$x
else
()
, '')
)
};
declare function bod:latLongDecimal2DMS($lat as xs:double, $long as xs:double) as xs:string*
{
for $coord at $pos in ($lat, $long)
let $direction := if ($pos eq 1) then (if ($coord lt 0) then 'S' else 'N') else (if ($coord lt 0) then 'W' else 'E')
let $abscoord := abs($coord)
let $wholedegrees := floor($abscoord)
let $remainder := $abscoord - $wholedegrees
let $minutes := $remainder * 60
let $wholeminutes := floor($minutes)
let $remainder2 := $minutes - $wholeminutes
let $wholeseconds := round($remainder2 * 60)
let $wholeminutes := if ($wholeseconds eq 60) then $wholeminutes + 1 else $wholeminutes
let $wholeseconds := if ($wholeseconds eq 60) then 0 else $wholeseconds
let $wholeminutes := if ($wholeminutes eq 60) then 0 else $wholeminutes
return concat($wholedegrees, '° ', $wholeminutes, "' ", $wholeseconds, '" ', $direction)
};
declare function bod:lookupAuthorityName($name as xs:string) as xs:string
{
switch(upper-case($name))
case 'VIAF' return "VIAF: Virtual International Authority File (authority record)"
case 'LC' return "Library of Congress (authority record)"
case 'BNF' return "Bibliothèque nationale de France (authority record)"
case 'SUDOC' return "SUDOC: Système Universitaire de Documentation (authority record)"
case 'GND' return "GND: Gemeinsame Normdatei (authority record)"
case 'TGN' return "Getty Thesaurus of Geographic Names® Online (authority record)"
case 'ISNI' return "ISNI: International Standard Name Identifier (authority record)"
default return $name
};
declare function bod:shelfmarkVariants($shelfmarks as xs:string*) as xs:string*
{
let $variants := distinct-values(($shelfmarks, for $shelfmark in $shelfmarks return replace($shelfmark, '\.\s+', ' ')))
return $variants
};
(:~
: --------------------------------
: TEI-to-Solr field mapping functions
: --------------------------------
:)
declare function bod:one2one_TESTING($teinode as element()*, $solrfield as xs:string)
{
(: Use this when debugging to find which TEI elements thought to be only exist once per document are actually multiple.
Hence either many2one or many2many should be used, or the XPath changed to select only one. :)
let $value as xs:string := normalize-space(string-join($teinode[1]//text(), ' '))
return if (count($teinode) lt 2) then
if (string-length($value) gt 0) then
<field name="{ $solrfield }">{ $value }</field>
else
()
else
bod:logging('error', concat(count($teinode), ' elements found when only one expected for ', $solrfield), bod:pathFromCollections(base-uri($teinode[1])))
};
declare function bod:one2one($teinode as element()?, $solrfield as xs:string)
{
(: One TEI element maps to a single Solr field :)
let $value as xs:string := normalize-space(string-join($teinode//text(), ' '))
return if (string-length($value) gt 0) then
<field name="{ $solrfield }">{ $value }</field>
else
()
};
declare function bod:one2one($teinode as element()?, $solrfield as xs:string, $ifnone as xs:string)
{
(: Overload the same function above to handle when nothing is found in the source TEI. Third param should be either a default value to use instead, or 'error' to prevent indexing. :)
let $result := bod:one2one($teinode, $solrfield)
return if (count($result) eq 0) then
if (lower-case($ifnone) eq 'error') then
bod:logging('error', 'No values for mandatory field', $solrfield)
else
<field name="{ $solrfield }">{ $ifnone }</field>
else
$result
};
declare function bod:many2one($teinodes as element()*, $solrfield as xs:string)
{
(: Concatenate a sequence of TEI elements, into a single Solr field :)
let $values as xs:string* := $teinodes/string-join(.//text(), ' ')
return if (not(empty($values))) then
<field name="{ $solrfield }">{ normalize-space(string-join(distinct-values($values), ' ')) }</field>
else
()
};
declare function bod:many2one($teinodes as element()*, $solrfield as xs:string, $ifnone as xs:string)
{
(: Overload the same function above to handle when nothing is found in the source TEI. Third param should be either a default value to use instead, or 'error' to prevent indexing. :)
let $result := bod:many2one($teinodes, $solrfield)
return if (count($result) eq 0) then
if (lower-case($ifnone) eq 'error') then
bod:logging('error', 'No values for mandatory field', $solrfield)
else
<field name="{ $solrfield }">{ $ifnone }</field>
else
$result
};
declare function bod:many2many($teinodes as element()*, $solrfield as xs:string)
{
(: Generate multiple Solr fields, one for each distinct value from a sequence of TEI elements :)
let $values as xs:string* := for $n in $teinodes return normalize-space(string-join($n//text()))
for $v in distinct-values($values)
return
if (string-length($v) > 0) then
<field name="{ $solrfield }">{ $v }</field>
else ()
};
declare function bod:many2many($teinodes as element()*, $solrfield as xs:string, $ifnone as xs:string)
{
(: Overload the same function above to handle when nothing is found in the source TEI. Third param should be either a default value to use instead, or 'error' to prevent indexing. :)
let $result := bod:many2many($teinodes, $solrfield)
return if (count($result) eq 0) then
if (lower-case($ifnone) eq 'error') then
bod:logging('error', 'No values for mandatory field', $solrfield)
else
<field name="{ $solrfield }">{ $ifnone }</field>
else
$result
};
declare function bod:string2one($value as xs:string, $solrfield as xs:string)
{
(: Generate a Solr field from a string :)
let $result := normalize-space($value)
return if (string-length($result) gt 0) then
<field name="{ $solrfield }">{ $result }</field>
else
()
};
declare function bod:strings2many($values as xs:string*, $solrfield as xs:string)
{
(: Generate multiple Solr fields, one for each distinct value from a sequence of strings :)
for $v in distinct-values(for $s in $values return normalize-space($s))
return
if (string-length($v) > 0) then
<field name="{ $solrfield }">{ $v }</field>
else ()
};
declare function bod:oneoranother2one($teinodes as element()*, $solrfield as xs:string)
{
(: Accepts a sequence of teinodes and uses the first matching one with content to populate a single Solr field :)
(: TODO: Test this, and maybe use it for ms_institution_s, so that Christ Church College's manuscripts are included in Medieval? :)
let $result := bod:many2many($teinodes, $solrfield)
return $result[1]
};
declare function bod:trueIfExists($teinode as element()*, $solrfield as xs:string)
{
(: Return true/false Solr field depending on presence of a TEI field :)
let $exists as xs:boolean := exists($teinode)
return if ($exists = true()) then
<field name="{ $solrfield }">true</field>
else
<field name="{ $solrfield }">false</field>
};
declare function bod:dateEarliest($teinodes as element()*, $solrfield as xs:string)
{
(: Find the earliest date of all TEI date/origDate fields passed to it :)
let $dates := $teinodes[string(number(@notBefore)) != 'NaN']/@notBefore
return if (empty($dates)) then
()
else
<field name="{ $solrfield }">{ min($dates) }</field>
};
declare function bod:dateLatest($teinodes as element()*, $solrfield as xs:string)
{
(: Find the latest date of all TEI date/origDate fields passed to it :)
let $dates := $teinodes[string(number(@notAfter)) != 'NaN']/@notAfter
return if (empty($dates)) then
()
else
<field name="{ $solrfield }">{ max($dates) }</field>
};
declare function bod:centuries($teinodes as element()*, $solrfield as xs:string)
{
(: Convert TEI date files into one Solr field for each century covered by
the year or year-range specified in @when/@notAfter/@NotBefore attributes :)
let $centuries := distinct-values(
for $date in $teinodes
return
if ($date[@when]) then
if (matches($date/@when/data(), $bod:yearregex)) then
bod:findCenturies(functx:get-matches($date/@when/data(), $bod:yearregex)[1], '')
else
bod:logging('info', 'Unreadable date', $date[@when]/data())
else if ($date[@notBefore] or $date[@notAfter]) then
if (matches($date/@notBefore/data(), $bod:yearregex) or matches($date/@notAfter/data(), $bod:yearregex)) then
bod:findCenturies(functx:get-matches($date/@notBefore/data(), $bod:yearregex)[1], functx:get-matches($date/@notAfter/data(), $bod:yearregex)[1])
else
bod:logging('info', 'Unreadable dates', concat($date/@notBefore/data(), '-', $date/@notAfter/data()))
else if ($date[@from] or $date[@to]) then
if (matches($date/@from/data(), $bod:yearregex) or matches($date/@to/data(), $bod:yearregex)) then
bod:findCenturies(functx:get-matches($date/@from/data(), $bod:yearregex)[1], functx:get-matches($date/@to/data(), $bod:yearregex)[1])
else
bod:logging('info', 'Unreadable dates', concat($date/@from/data(), '-', $date/@to/data()))
else
()
)[string-length(.) gt 0]
return
(
for $century in $centuries
order by $century
return
<field name="{ $solrfield }">{ $century }</field>
,
if (count($centuries) gt 1) then <field name="{ $solrfield }">Multiple Centuries</field> else ()
)
};
declare function bod:centuries($teinodes as element()*, $solrfield as xs:string, $ifnone as xs:string)
{
(: Overload the same function above to handle when nothing is found in the source TEI. Third param should be either a
default value to use instead, or 'error' to prevent indexing. :)
let $result := bod:centuries($teinodes, $solrfield)
return if (count($result) eq 0) then
if (lower-case($ifnone) eq 'error') then
bod:logging('error', 'No values for mandatory field', $solrfield)
else
<field name="{ $solrfield }">{ $ifnone }</field>
else
$result
};
declare function bod:years($teinodes as element()*)
{
let $years := for $dateval in $teinodes/(@when|@notBefore|@notAfter|@from|@to)/data()
return
if ($dateval castable as xs:integer) then
xs:integer($dateval)
else if (matches($dateval, $bod:yearregex)) then
for $y in functx:get-matches($dateval, $bod:yearregex)[string-length() gt 0] return xs:integer($y)
else
()
return if (count($years) gt 0) then
(
<field name="start_year_i">{ min($years) }</field>
,
<field name="end_year_i">{ max($years) }</field>
)
else
()
};
declare function bod:materials($teinodes as element()*, $solrfield as xs:string)
{
let $materials := distinct-values($teinodes/string(@material))
return
(
for $item in $materials
let $material := (
switch ($item)
case "perg" return "Parchment"
case "chart" return "Paper"
case "paper" return "Paper"
case "papyrus" return "Papyrus"
case "mixed" return "Mixed"
case "unknown" return "Unknown"
default return "Other"
)
return <field name="{ $solrfield }">{ $material }</field>
,
(: Add "Mixed" for manuscripts containing multiple materials, not just the ones catalogued as "mixed" :)
if (count($materials[not(. eq 'mixed')]) gt 1) then <field name="{ $solrfield }">Mixed</field> else ()
)
};
declare function bod:materials($teinodes as element()*, $solrfield as xs:string, $ifnone as xs:string)
{
(: Overload the same function above to handle when nothing is found in the source TEI. Third param should be either a
default value to use instead, or 'error' to prevent indexing. :)
let $result := bod:materials($teinodes, $solrfield)
return if (count($result) eq 0) then
if (lower-case($ifnone) eq 'error') then
bod:logging('error', 'No values for mandatory field', $solrfield)
else
<field name="{ $solrfield }">{ $ifnone }</field>
else
$result
};
declare function bod:languages($teinodes as element()*, $solrfield as xs:string)
{
let $langCodes := distinct-values(for $attr in $teinodes/@* return if (name($attr) = 'mainLang' or name($attr) = 'otherLangs') then tokenize($attr, ' ') else ())
return
(
for $code in $langCodes
for $lang in lang:languageCodeLookup($code)
return <field name="{ $solrfield }">{ normalize-space($lang) }</field>
,
if (count($langCodes) gt 1) then <field name="{ $solrfield }">Multiple Languages</field> else ()
)
};
declare function bod:languages($teinodes as element()*, $solrfield as xs:string, $ifnone as xs:string)
{
(: Overload the same function above to handle when nothing is found in the source TEI. Third param should be either a
default value to use instead, or 'error' to prevent indexing. :)
let $result := bod:languages($teinodes, $solrfield)
return if (count($result) eq 0) then
if (lower-case($ifnone) eq 'error') then
bod:logging('error', 'No values for mandatory field', $solrfield)
else
<field name="{ $solrfield }">{ $ifnone }</field>
else
$result
};
declare function bod:languageCodeLookup($lang as xs:string) as xs:string*
{
lang:languageCodeLookup($lang)
};
declare function bod:physForm($teinodes as element()*, $solrfield as xs:string)
{
for $form in distinct-values($teinodes/@form)
return <field name="{ $solrfield }">{ bod:physFormLookup($form) }</field>
};
declare function bod:physForm($teinodes as element()*, $solrfield as xs:string, $ifnone as xs:string)
{
(: Overload the same function above to handle when nothing is found in the source TEI. Third param should be either a default value to use instead, or 'error' to prevent indexing. :)
let $result := bod:physForm($teinodes, $solrfield)
return if (count($result) eq 0) then
if (lower-case($ifnone) eq 'error') then
bod:logging('error', 'No values for mandatory field', $solrfield)
else
<field name="{ $solrfield }">{ $ifnone }</field>
else
$result
};
declare function bod:digitized($teinodes as element()*, $solrfield as xs:string)
{
(: This function returns some or all or the following Solr fields (the last 3 are Oxford-only):
ms_digitized_s: a label describing whether and how much of a manuscript has been digitized (the 'Digital facsimile online' facet)
ms_bestsurrogate_sni: the URL of the "best" digital surrogate (the first full one, if available, otherwise first partial)
ms_digbod_b: whether there is at least one Digital Bodleian object UUID (which Digital Bodleian uses to search for records it can link back to)
ms_digbod_sm: Digital Bodleian UUIDs (which Digital Bodleian uses to match catalogue records to its objects)
ms_digbodpath_s: the path part of the catalogue URL (which Digital Bodleian uses to build links back to the catalogue records)
:)
let $uuids as xs:string* :=
for $dburl in $teinodes/tei:ref[matches(@target, '(digital|iiif)\.bodleian\.ox\.ac\.uk')]
let $matchinguuids := tokenize($dburl/@target, '/')[matches(., '\w{8}\-\w{4}\-4\w{3}\-\w{4}\-\w{12}')][1]
return
if (count($matchinguuids) eq 1) then
$matchinguuids[1]
else
bod:logging('warn', 'Invalid Digital Bodleian URL', $dburl/@target)
return (
<field name="ms_digitized_s">
{
if ($teinodes[@type=('digital-fascimile','digital-facsimile') and @subtype='full']) then 'Yes'
else if ($teinodes[@type=('digital-fascimile','digital-facsimile') and @subtype='partial']) then 'Selected pages only'