-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-works-authority-files.xquery
170 lines (156 loc) · 6.69 KB
/
update-works-authority-files.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
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace saxon="http://saxon.sf.net/";
declare option saxon:output "indent=yes";
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:generateVariations($title as xs:string) as xs:string*
{
let $variations1 := (
replace($title, '\.', ''),
replace($title, ',', ''),
replace($title, '[\-–—]', ''),
replace($title, '[\.,\-–—]', ''),
replace(replace(replace(replace($title, "[ʻ’'ʻ‘]", "'"), 'ʻ̐', "'"), 'ʹ̨', "'"), 'ʻ̨', "'"),
replace(replace(replace(replace($title, "[ʻ’'ʻ‘]" ,""), 'ʻ̐', ''), 'ʹ̨', ''), 'ʻ̨', ''),
replace($title, 'ʺ', '"'),
replace($title, '["ʺ]', '')
)
let $variations2 := for $v in distinct-values($variations1)
return (
$v,
replace($v , '\(.*\)', ''),
replace($v, '[\(\)]', ''),
replace($v, '\[.*\]', ''),
replace($v, '[\[\]]', '')
)
let $variations3 := for $v in distinct-values($variations2)
return (
$v,
replace(replace(replace(translate($v, 'āṅñèṃīṇ', 'annemin'), 'o̐', 'o'), 'ą̄', 'a'), 'ą̄', 'a')
)
let $variations4 := for $v in distinct-values($variations3)
return (
$v,
replace($v, '^(the|a|an) ', '')
)
let $variations5 := for $v in distinct-values($variations4)
return replace(lower-case($v), '\s', '')
return distinct-values($variations5[not(. eq $title)])
};
processing-instruction xml-model {'href="http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng" type="application/xml" schamtypens="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" schamtypens="http://purl.oclc.org/dsdl/schematron"'},
processing-instruction xml-model {'href="authority-schematron.sch" type="application/xml" schamtypens="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>
<listBibl>
{
let $collection := collection('../../fihrist-mss/collections/?select=*.xml;recurse=yes')
let $currentworks := doc('../../fihrist-mss/authority/works.xml')//tei:bibl
let $currentids := $currentworks//tei:ref/@target/data()
let $currentkeys := $currentworks//@xml:id/data()
let $highestcurrentkey := max((for $k in $currentkeys return xs:integer(replace($k, '\D', ''))))
let $linebreak := ' '
(: Look for works not already in the authority file :)
let $newworks := (
for $w in $collection//tei:msItem[@xml:id and tei:title and not(@n) and not(lower-case(normalize-space(string-join(tei:title, ''))) = ('none', 'unknown', 'unknown.', ''))]
return
if ($w/@xml:id/data() = $currentids) then
( )
else
let $titles := for $t in $w/tei:title return normalize-space(string-join($t//text(), ' '))
let $sharestitleswith := $currentworks[(for $t in tei:title/text() return ($t, local:generateVariations($t))) = (for $t in $titles return ($t, local:generateVariations($t)))]
return
if ( count($sharestitleswith) gt 0 ) then
(: This work appears to be another copy of an existing work :)
<work>
{
for $t in $titles return <title>{ $t }</title>
}
{
for $o in $sharestitleswith
return
<instanceof>{ $o/@xml:id/data() }</instanceof>
}
<id infile="{ tokenize(base-uri($w), '/')[last()] }">{ $w/@xml:id/data() }</id>
</work>
else
(: This work's title is not in the authority file :)
<work>
{
for $t in $titles return <title>{ $t }</title>
}
<id infile="{ tokenize(base-uri($w), '/')[last()] }">{ $w/@xml:id/data() }</id>
</work>
)
let $newentries := (
(
for $w at $pos in $newworks[not(instanceof)]
order by $w/title[1]/text()
(:order by :)
return
<bibl xml:id="{ concat('work_', $highestcurrentkey + $pos) }">
{
for $t at $i in $w/title/text()
return
if ($i eq 1) then
<title type="uniform">{ $t }</title>
else
<title type="variant">{ $t }</title>
}
{
for $id in $w/id
return (
<ref target="{ $id/text() }"/>
,
comment{concat(' ../collections/', $id/@infile/data(), '#', $id/text(), ' ')}
)
}
</bibl>
),(
for $w in $newworks[instanceof]
order by $w/title[1]/text()
return
<bibl copyOf="{ string-join($w/instanceof/text(), ' ') }">
{
for $t at $i in $w/title/text()
return
if ($i eq 1) then
<title type="uniform">{ $t }</title>
else
<title type="variant">{ $t }</title>
}
{
for $id in $w/id
return (
<ref target="{ $id/text() }"/>
,
comment{concat(' ../collections/', $id/@infile/data(), '#', $id/text(), ' ')}
)
}
</bibl>
)
)
(: Output the new authority file :)
return ($linebreak, doc('../authority/works_additions.xml')//tei:bibl, $linebreak, for $e in $newentries return ($e, $linebreak))
}
</listBibl>
</body>
</text>
</TEI>