-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathupdate-persons-authority-file.xquery
293 lines (272 loc) · 12.8 KB
/
update-persons-authority-file.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
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare option saxon:output "indent=yes";
(: Read authority files :)
declare variable $base := doc("../authority/persons_base.xml")/tei:TEI/tei:text/tei:body/tei:listPerson/tei:person[@xml:id];
declare variable $additions := doc("../authority/persons_additions.xml")/tei:TEI/tei:text/tei:body/tei:listPerson/tei:person[@xml:id];
declare variable $currentpeople := ($base, $additions);
declare variable $collection := collection('../collections/?select=*.xml;recurse=yes');
declare variable $currentkeys := $currentpeople//@xml:id/data();
declare variable $highestcurrentkey := max(for $k in $currentkeys return if (starts-with($k, 'person_f')) then xs:integer(replace($k, '\D', '')) else () );
declare variable $linebreak := ' ';
declare function local:logging($level, $msg, $values)
{
(: 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)
};
declare function local:normalize4Crossrefing($name as xs:string) as xs:string
{
let $normalized1 := normalize-space(replace(normalize-unicode($name, 'NFKD'), '(^|\s)(the |al-|el-)', ' ', 'i'))
let $normalized2 :=
translate(
translate(
replace(
replace(
replace(
lower-case($normalized1),
'[^\p{L}\d]', ''
),
'æ', 'ae'),
'œ', 'oe'),
'ạĀāàáâḅÇçČḌḍḏèéëēĞğĠġǦǧḢḣḤḥḪḫẖĪīĭİıÎÏìíîïḲḳḴṇōóÖṛŕśṢṣŞşŠšṬṭṯúûüŪūżẒẓẔẕ', 'aaaaaabcccdddeeeegggggghhhhhhhiiiiiiiiiiikkknooorrssssssstttuuuuuzzzzz'),
'ʼ', '')
let $normalized3 := replace(replace(replace(replace(replace($normalized2, "[ʻ’'ʻ‘ʺʹ]" ,""), 'ʻ̐', ''), 'ʹ̨', ''), 'ʻ̨', ''), '"', '')
return $normalized3
};
declare function local:percentEncode($str as xs:string) as xs:string
{
string-join(tokenize(string-join(for $s in tokenize($str, '%') return encode-for-uri($s), '%'), '-'), '%2D')
};
processing-instruction xml-model {'href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"'},
processing-instruction xml-model {'href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"'},
processing-instruction xml-model {'href="authority-schematron.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"'},
<TEI xmlns="http://www.tei-c.org/ns/1.0">
<teiHeader>
<fileDesc>
<titleStmt>
<title>Title</title>
</titleStmt>
<publicationStmt>
<p>Publication Information</p>
</publicationStmt>
<sourceDesc>
<p>Information about the source</p>
</sourceDesc>
</fileDesc>
</teiHeader>
<text>
<body>
<listPerson>
{
let $newviafpeople := (
for $p in $collection/tei:TEI[@xml:id]//(tei:author|tei:editor|tei:persName[not(ancestor::tei:author/@key or ancestor::tei:editor/@key)]|tei:name[tei:persName])[matches(@key, 'person_\d+') or matches(@key/lower-case(data()), 'viaf_\d+')]
let $keys := (distinct-values(
for $key in tokenize($p/@key, '\s+')
return
if (starts-with($key, 'person_')) then
$key
else if (starts-with(lower-case($key), 'viaf_')) then
(: The BL's IAMS system uses "viaf_123" or "Viaf_123" instead of "person_123" for VIAF-based person IDs :)
concat('person_', substring-after($key, '_'))
else
()
))[not(. = $currentkeys)][matches(., '^person_\d+$')]
let $names :=
if ($p/tei:persName) then
for $n in $p/tei:persName
return
normalize-space(string-join($n//text(), ' '))
else
normalize-space(string-join($p//text(), ' '))
return
if (count($keys) gt 0) then
<person xml:id="{ $keys[1] }">
{
for $n at $pos in distinct-values($names)
return
if ($pos eq 1) then
<persName type="display">{ $n }</persName>
else
<persName type="variant">{ $n }</persName>
}
{
comment{concat(' ../collections/', local:percentEncode(substring-after(base-uri($p), 'collections/')), '#', local:percentEncode($p/ancestor::*[@xml:id][1]/@xml:id), ' ')}
}
</person>
else
()
)
let $dedupednewviafpeople := (
for $k in distinct-values($newviafpeople/@xml:id/data())
return
<person xml:id="{ $k }">
{
for $n at $pos in distinct-values($newviafpeople[@xml:id = $k]/persName/text())
return
if ($pos eq 1) then
<persName type="display">{ $n }</persName>
else
<persName type="variant">{ $n }</persName>
}
<note type="links">
<list type="links">
<item>
<ref target="https://viaf.org/viaf/{ substring-after($k, 'person_') }/">
<title>VIAF</title>
</ref>
</item>
</list>
</note>
{
for $c in distinct-values($newviafpeople[@xml:id = $k]/comment())
order by $c
return comment{ $c }
}
</person>
)
let $viafpeoplefrompreviousrun := $additions[matches(@xml:id, 'person_\d+')]
let $newlocalpeople := (
for $p in $collection/tei:TEI[@xml:id]//(tei:author|tei:editor|tei:persName[not(ancestor::tei:author/@key or ancestor::tei:editor/@key)]|tei:name[tei:persName])[@key and (@key = '' or not(some $k in tokenize(@key, '\s+') satisfies (starts-with($k, 'person_') or starts-with(lower-case($k), 'viaf_')))) and string-length(normalize-space(string())) gt 1]
let $names :=
if ($p/tei:persName) then
for $n in $p/tei:persName
return
normalize-space(string-join($n//text(), ' '))
else
normalize-space(string-join($p//text(), ' '))
return
<person>
{
for $n at $pos in distinct-values($names)
return
(
<norm>{ local:normalize4Crossrefing($n) }</norm>,
if ($pos eq 1) then
<persName type="display">{ $n }</persName>
else
<persName type="variant">{ $n }</persName>
)
}
{
comment{concat(' ../collections/', local:percentEncode(substring-after(base-uri($p), 'collections/')), '#', local:percentEncode($p/ancestor::*[@xml:id][1]/@xml:id), ' ')}
}
</person>
)
let $localpeoplefrompreviousrun := (
for $p in $additions[starts-with(@xml:id, 'person_f')]
return
<person>
{ $p/@* }
{ $p/* }
{ for $n in $p/persName[text()] return <norm>{ local:normalize4Crossrefing($n/text()) }</norm> }
{ $p/comment() }
</person>
)
let $peopleinbase := (
for $p in $base
return
<person>
{ $p/@* }
{ $p/* }
{ for $n in $p/persName[text()] return <norm>{ local:normalize4Crossrefing($n/text()) }</norm> }
{ $p/comment() }
</person>
)
let $dedupednewlocalpeople := (
for $n at $pos in $newlocalpeople
return
if (some $v in $n/norm/text() satisfies $v = $localpeoplefrompreviousrun/norm/text()) then
(: This is a duplicate of an entry created by a previous running of this script, so skip it :)
()
else if (some $v in $n/norm/text() satisfies $v = $newlocalpeople[position() lt $pos]/norm/text()) then
(: This is a duplicate of another new entry already processed in this for-loop, so skip it :)
()
else
let $possibleduplicatesinbase := $peopleinbase[some $v in ./norm/text() satisfies $v = $n/norm/text()]
let $possibleduplicatesinbasecomments := for $pd in $possibleduplicatesinbase return comment{concat(' POSSIBLE DUPLICATE OF: persons_base.xml#', $pd/@xml:id/data(), ' ')}
let $duplicates := $newlocalpeople[position() gt $pos and (some $v in ./norm/text() satisfies $v = $n/norm/text())]
return
if (count($duplicates) gt 0) then
(: This has duplicates, so merge them into this entry :)
<person>
{
for $f at $pos2 in distinct-values(($n/persName/text(), $duplicates/persName/text()))
return
(
if ($pos2 eq 1) then
<persName type="display">{ $f }</persName>
else
<persName type="variant">{ $f }</persName>
)
}
{
for $c in distinct-values(($n/comment(), $duplicates/comment()))
order by $c
return comment{ $c }
}
{ $possibleduplicatesinbasecomments }
</person>
else
<person>
{ $n/* }
{ $n/comment() }
{ $possibleduplicatesinbasecomments }
</person>
)
let $localpeoplefrompreviousrunwithnewvariants := (
for $n in $localpeoplefrompreviousrun
let $duplicates := $newlocalpeople[some $v in ./norm/text() satisfies $v = $n/norm/text()]
return
if (count($duplicates) gt 0) then
<person>
{ $n/@xml:id }
{
for $f at $pos in distinct-values(($n/persName/text(), $duplicates/persName/text()))
return
(
if ($pos eq 1) then
<persName type="display">{ $f }</persName>
else
<persName type="variant">{ $f }</persName>
)
}
{
for $c in distinct-values(($n/comment(), $duplicates/comment()))
order by $c
return comment{ $c }
}
</person>
else
$n
)
let $dedupedlocalpeoplewithids := (
for $n at $pos in $dedupednewlocalpeople
return
<person xml:id="{ concat('person_f', ($highestcurrentkey + $pos)) }">
{ $n/*[not(self::norm)] }
{ $n/comment() }
</person>
,
for $n in $localpeoplefrompreviousrunwithnewvariants
return
<person xml:id="{ $n/@xml:id }">
{ $n/*[not(self::norm)] }
{ $n/comment() }
</person>
)
(: Output the new _additions authority file :)
return (
$linebreak,
<person>{ comment{' Dummy person, just so this file validates, do not delete '} }<persName type="display"/></person>,
$linebreak,
$linebreak,
comment{' TODO: Review the following entries, update their key attributes in the TEI files, then cut and paste them into persons_base.xml '},
$linebreak,
for $e in ($dedupednewviafpeople, $viafpeoplefrompreviousrun, $dedupedlocalpeoplewithids) order by $e/persName[@type='display']/text() return ($e, $linebreak),
$linebreak,
$linebreak
)
}
</listPerson>
</body>
</text>
</TEI>