-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathxml2kwic.xsl
More file actions
1192 lines (918 loc) · 52.5 KB
/
xml2kwic.xsl
File metadata and controls
1192 lines (918 loc) · 52.5 KB
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
<!DOCTYPE xsl:stylesheet [
<!ENTITY prime "′">
<!ENTITY tcomma "ʻ">
<!ENTITY mlrexcl "ꜝ">
<!ENTITY startMark "(ⴵ">
<!ENTITY endMark "ⴵ)">
]>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:f="urn:stylesheet-functions"
xmlns:s="http://gutenberg.ph/segments"
xmlns:k="http://gutenberg.ph/kwic"
xmlns:xd="http://www.pnp-software.com/XSLTdoc"
exclude-result-prefixes="f fn xd xs s k map">
<xd:doc type="stylesheet">
<xd:short>Stylesheet to produce a KWIC from a TEI document</xd:short>
<xd:detail>This stylesheet produces a KWIC from a TEI document. It shows all occurrences of all words
in the document in their context in alphabetical order. The output can become rather big, as a rule-of-thumb,
30 to 40 times the size of the original. Furthermore, the processing time can be considerable.</xd:detail>
<xd:author>Jeroen Hellingman</xd:author>
<xd:copyright>2011-2026, Jeroen Hellingman</xd:copyright>
</xd:doc>
<xsl:output method="html" version="5.0" encoding="utf-8"/>
<xsl:include href="modules/segmentize.xsl"/>
<xsl:param name="output-segments" select="false()"/>
<xsl:param name="no-common-words" select="false()"/>
<xd:doc>
<xd:short>The keyword(s) to generate a KWIC for.</xd:short>
<xd:detail>The keyword(s) to generate a KWIC for. If omitted, a KWIC will be generated for all words
in the document.</xd:detail>
</xd:doc>
<xsl:param name="keyword" select="''"/>
<xd:doc>
<xd:short>The language(s) to generate a KWIC for.</xd:short>
<xd:detail>The language(s) to generate a KWIC for. If omitted, a KWIC will be generated for all languages
in the document. Use ISO-639 codes separated by spaces.</xd:detail>
</xd:doc>
<xsl:param name="select-language" select="''"/>
<xsl:variable name="select-language-sequence" select="tokenize($select-language, ' ')"/>
<xsl:variable name="default-language" select="/*[self::TEI.2 or self::TEI]/@lang"/>
<xsl:param name="case-sensitive" select="'false'" as="xs:string"/>
<xsl:param name="use-normalized-form" select="'true'" as="xs:string"/>
<xsl:param name="min-variant-count" select="1" as="xs:integer"/>
<xd:doc>
<xd:short>Indicate how the lists should be sorted.</xd:short>
<xd:detail>Indicate the sort-order of a KWIC-list. Possible values: 'following' or 'preceding'.</xd:detail>
</xd:doc>
<xsl:param name="sort-context" select="'following'"/>
<xd:doc>
<xd:short>Sort potentially mixed-up characters together.</xd:short>
<xd:detail>Sort (sequences of) characters that are often mixed-up (such as b and h, or rn and m) together.
The resulting KWIC will only include words that include the mixed-up characters and appear in at least two
variants.</xd:detail>
</xd:doc>
<xsl:param name="mixup" select="''"/>
<xsl:variable name="mixup-sequence" select="tokenize($mixup, ' ')"/>
<xd:doc>
<xd:short>The length of the context to show.</xd:short>
<xd:detail>The length of the context to show. This counts both words and non-words (spaces and punctuation marks).
The preceding and following contexts are counted separately.</xd:detail>
</xd:doc>
<xsl:param name="context-size" select="16"/>
<xd:doc>
<xd:short>The name of the file that contains stopwords.</xd:short>
</xd:doc>
<xsl:param name="stopword-file" select="'locale/stopwords.xml'" as="xs:string"/>
<xsl:param name="commonword-file" select="'locale/commonwords.xml'" as="xs:string"/>
<xd:doc>
<xd:short>A map that maps a language code to a sequence of stopwords in that language.</xd:short>
<xd:detail>The map is read from the file indicated in the parameter <code>stopword-file</code>.</xd:detail>
</xd:doc>
<xsl:variable name="stopwords" as="map(xs:string, xs:string*)">
<xsl:map>
<xsl:for-each select="document($stopword-file)//words">
<xsl:map-entry key="xs:string(@lang)" select="tokenize(., ' ')"/>
</xsl:for-each>
</xsl:map>
</xsl:variable>
<xsl:variable name="commonwords" as="map(xs:string, xs:string*)">
<xsl:map>
<xsl:for-each select="document($commonword-file)//words">
<xsl:map-entry key="xs:string(@lang)" select="tokenize(., ' ')"/>
</xsl:for-each>
</xsl:map>
</xsl:variable>
<xd:doc>
<xd:short>Determine whether a word is a stop-word.</xd:short>
<xd:detail>Determine whether a word appears in the list of stop-words for a given language.</xd:detail>
<xd:param name="word">The word to test.</xd:param>
<xd:param name="lang">The (declared) language of the word (used to select the stop-word list).</xd:param>
</xd:doc>
<xsl:function name="f:is-stopword" as="xs:boolean">
<xsl:param name="word" as="xs:string"/>
<xsl:param name="lang" as="xs:string"/>
<xsl:variable name="lang" select="if (not(exists($stopwords($lang))) and contains($lang, '-')) then substring-before($lang, '-') else $lang"/>
<xsl:variable name="word" select="lower-case($word)"/>
<xsl:sequence select="$stopwords($lang) = $word"/>
</xsl:function>
<xsl:function name="f:is-common-word" as="xs:boolean">
<xsl:param name="word" as="xs:string"/>
<xsl:param name="lang" as="xs:string"/>
<xsl:variable name="lang" select="if (not(exists($commonwords($lang))) and contains($lang, '-')) then substring-before($lang, '-') else $lang"/>
<xsl:variable name="word" select="lower-case($word)"/>
<xsl:sequence select="$commonwords($lang) = $word"/>
</xsl:function>
<xsl:function name="f:is-ignored-word" as="xs:boolean">
<xsl:param name="word" as="xs:string"/>
<xsl:param name="lang" as="xs:string"/>
<xsl:sequence select="f:is-stopword($word, $lang) or ($no-common-words and f:is-common-word($word, $lang))"/>
</xsl:function>
<xd:doc>
<xd:short>Generate the HTML output.</xd:short>
<xd:detail>Generate the HTML output.</xd:detail>
</xd:doc>
<xsl:template match="/">
<html>
<head>
<meta charset="UTF-8"/>
<title>KWIC for <xsl:value-of select="*[self::TEI.2 or self::TEI]/teiHeader/fileDesc/titleStmt/title[not(@type) or @type='main']"/></title>
<style>
table {
margin-left: auto;
margin-right: auto;
}
.tag {
font-size: xx-small;
color: grey;
}
.cnt {
font-size: small;
color: grey;
}
.pre, .pn {
text-align: right;
}
.lang {
padding-left: 2em;
font-weight: bold;
color: blue;
}
th.ph {
font-size: small;
color: gray;
}
.match {
font-weight: bold;
color: #D10000;
}
.sc {
font-variant: small-caps;
}
.asc {
font-variant: small-caps;
text-transform: lowercase;
}
.uc {
text-transform: uppercase;
}
.ex {
letter-spacing: 0.2em;
}
.tt {
font-family: monospace;
}
.green {
color: green;
font-weight: bold;
}
.ref {
color: blue;
text-decoration: underline;
}
.notemark {
color: green;
font-weight: bold;
vertical-align: super;
font-size: smaller;
}
.var1 {
background-color: white;
}
.var2 {
background-color: #80FFEE;
}
.var3 {
background-color: #BFFF80;
}
.var4 {
background-color: #FFFF80;
}
.var5 {
background-color: #FFD780;
}
.var6, .var7, .var8, .var9, .var10, .var11, .var12 {
background-color: #FF8080;
color: black;
}
.ix {
background-color: yellow;
}
.nt {
background-color: #ffccff;
}
.fig {
background-color: #d9ffb3;
}
.tbl {
background-color: #ccffff;
}
</style>
</head>
<body>
<h1>
KWIC for <xsl:value-of select="*[self::TEI.2 or self::TEI]/teiHeader/fileDesc/titleStmt/title[not(@type) or @type='main']"/>
<xsl:if test="$keyword != ''">
(<xsl:value-of select="$keyword"/>)
</xsl:if>
</h1>
<xsl:call-template name="build-kwic"/>
</body>
</html>
</xsl:template>
<xd:doc>
<xd:short>Generate the KWIC</xd:short>
<xd:detail>Generate the KWIC. This template build a KWIC in the following steps.
<ol>
<li>Segmentize the text.</li>
<li>Collect all words that appear in the document.</li>
<li>Loop over the list of words, and find matches for each word.</li>
</ol>
</xd:detail>
</xd:doc>
<xsl:template name="build-kwic">
<xsl:variable name="segments">
<xsl:apply-templates mode="segmentize" select="/"/>
</xsl:variable>
<xsl:if test="$output-segments">
<xsl:call-template name="output-segments">
<xsl:with-param name="segments" select="$segments"/>
</xsl:call-template>
</xsl:if>
<xsl:choose>
<xsl:when test="$keyword != ''">
<!-- Build KWIC for searched word(s) only -->
<xsl:variable name="keywords" select="tokenize(f:normalize-string($keyword), '\s+')"/>
<xsl:call-template name="report-single-match">
<xsl:with-param name="keyword" select="$keywords"/>
<xsl:with-param name="segments" select="$segments"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- Build KWIC for all words in document -->
<xsl:variable name="matches">
<xsl:apply-templates mode="multi-kwic" select="$segments//k:w"/>
</xsl:variable>
<xsl:for-each-group select="$matches/k:match" group-by="@form">
<xsl:sort select="(current-group()[1])/@form" order="ascending"/>
<xsl:if test="fn:matches(current-group()[1]/@form, '^[\p{L}\p{M}′-]+$')">
<xsl:call-template name="report-multiple-matches">
<xsl:with-param name="matches" select="current-group()"/>
</xsl:call-template>
</xsl:if>
</xsl:for-each-group>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="report-multiple-matches">
<xsl:param name="matches" as="element()*"/>
<xsl:variable name="keyword" select="$matches[1]/k:word"/>
<xsl:variable name="baseword" select="f:normalize-string($keyword)"/>
<xsl:variable name="variant-count" select="f:count-variants($matches)"/>
<xsl:if test="f:should-report-match($baseword, $variant-count)">
<h2>
<span class="cnt">Word:</span><xsl:text> </xsl:text>
<xsl:value-of select="$baseword"/><xsl:text> </xsl:text>
<xsl:if test="count($matches) > 1">
<span class="cnt"><xsl:value-of select="count($matches)"/></span>
</xsl:if>
</h2>
<xsl:if test="$variant-count > 1 or $baseword != $keyword">
<xsl:copy-of select="f:output-variants($matches, $variant-count)"/>
</xsl:if>
<xsl:variable name="variants">
<xsl:for-each-group select="$matches" group-by="./k:word">
<xsl:sort select="count(current-group())" order="descending"/>
<k:w><xsl:value-of select="current-group()[1]/k:word"/></k:w>
</xsl:for-each-group>
</xsl:variable>
<table>
<xsl:call-template name="table-headers"/>
<xsl:apply-templates mode="output" select="$matches">
<xsl:with-param name="variants" tunnel="yes" select="$variants/k:w"/>
<xsl:sort select="f:context-sort-key(k:preceding, k:following)" order="ascending"/>
</xsl:apply-templates>
</table>
</xsl:if>
</xsl:template>
<xsl:function name="f:context-sort-key" as="xs:string">
<xsl:param name="preceding" as="xs:string"/>
<xsl:param name="following" as="xs:string"/>
<xsl:choose>
<xsl:when test="$sort-context = 'preceding'">
<xsl:value-of select="f:reverse(f:normalize-string($preceding))"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="f:normalize-string($following)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="f:reverse">
<xsl:param name="string" as="xs:string"/>
<!-- TODO: split the string with a regular expression, taking into account combining diacritics -->
<xsl:sequence select="codepoints-to-string(reverse(string-to-codepoints($string)))"/>
</xsl:function>
<xsl:function name="f:should-report-match" as="xs:boolean">
<xsl:param name="word" as="xs:string"/>
<xsl:param name="variant-count" as="xs:integer"/>
<xsl:sequence select="if ($mixup = '')
then $variant-count >= $min-variant-count
else ($variant-count >= 2 and contains($word, $mixup-sequence[1]))"/>
</xsl:function>
<xsl:template name="table-headers">
<tr>
<th/>
<th/>
<th/>
<th class="pn">Page</th>
</tr>
</xsl:template>
<xd:doc mode="output">
<xd:short>Mode used to output the matches.</xd:short>
<xd:detail>This produces the HTML to report the matches found.</xd:detail>
</xd:doc>
<xd:doc>
<xd:short>Find and report matches.</xd:short>
<xd:detail>Find and report matches. The matches are first collected in a variable, using the mode <code>kwic</code>, and then reported, using the mode <code>output</code>.</xd:detail>
<xd:param name="segments">The document being processed, split in to segments.</xd:param>
<xd:param name="keyword">The keyword for which the KWIC is generated.</xd:param>
</xd:doc>
<xsl:template name="report-single-match">
<xsl:param name="segments"/>
<xsl:param name="keyword"/>
<xsl:variable name="matchlist">
<k:matches>
<xsl:apply-templates mode="single-kwic" select="$segments//k:w">
<xsl:with-param name="keyword" select="$keyword"/>
</xsl:apply-templates>
</k:matches>
</xsl:variable>
<xsl:variable name="matches" select="$matchlist/k:matches/k:match"/>
<h2>
<span class="cnt">Query:</span><xsl:text> </xsl:text>
<xsl:value-of select="$keyword"/><xsl:text> </xsl:text>
<xsl:if test="count($matches) > 1">
<span class="cnt"><xsl:value-of select="count($matches)"/></span>
</xsl:if>
</h2>
<xsl:variable name="variant-count" select="f:count-variants($matches)"/>
<xsl:if test="$variant-count > 1">
<xsl:copy-of select="f:output-variants($matches, $variant-count)"/>
</xsl:if>
<xsl:variable name="variants">
<xsl:for-each-group select="$matches" group-by="./k:word">
<xsl:sort select="count(current-group())" order="descending"/>
<k:w><xsl:value-of select="current-group()[1]/k:word"/></k:w>
</xsl:for-each-group>
</xsl:variable>
<xsl:apply-templates mode="output" select="$matchlist">
<xsl:with-param name="variants" tunnel="yes" select="$variants/k:w"/>
<xsl:sort select="f:context-sort-key(k:preceding, k:following)" order="ascending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:function name="f:count-variants" as="xs:integer">
<xsl:param name="matches"/>
<xsl:variable name="groups">
<xsl:for-each-group select="$matches" group-by="./k:word">.</xsl:for-each-group>
</xsl:variable>
<xsl:sequence select="string-length($groups)"/>
</xsl:function>
<xsl:function name="f:output-variants">
<xsl:param name="matches"/>
<xsl:param name="variant-count" as="xs:integer"/>
<p><span class="cnt"><xsl:value-of select="if ($variant-count > 1) then 'Variants' else 'Variant'"/>:</span>
<xsl:for-each-group select="$matches" group-by="./k:word">
<xsl:sort select="count(current-group())" order="descending"/>
<xsl:text> </xsl:text><b class="var{position()}"><xsl:value-of select="current-group()[1]/k:word"/></b>
<xsl:if test="count(current-group()) > 1">
<xsl:text> </xsl:text><span class="cnt"><xsl:value-of select="count(current-group())"/></span>
</xsl:if>
</xsl:for-each-group>
</p>
</xsl:function>
<xd:doc>
<xd:short>Output matches.</xd:short>
<xd:detail>Output an HTML table for the matches.</xd:detail>
</xd:doc>
<xsl:template mode="output" match="k:matches">
<table>
<xsl:call-template name="table-headers"/>
<xsl:apply-templates mode="#current">
<xsl:sort select="f:context-sort-key(k:preceding, k:following)" order="ascending"/>
</xsl:apply-templates>
</table>
</xsl:template>
<xd:doc>
<xd:short>Output a match.</xd:short>
<xd:detail>Output an HTML table-row for a match.</xd:detail>
</xd:doc>
<xsl:template mode="output" match="k:match">
<xsl:param name="variants" tunnel="yes" as="xs:string*"/>
<tr>
<xsl:if test="@category = 'index'"><xsl:attribute name="class" select="'ix'"/></xsl:if>
<xsl:if test="@category = 'note'"><xsl:attribute name="class" select="'nt'"/></xsl:if>
<xsl:if test="@category = 'figure'"><xsl:attribute name="class" select="'fig'"/></xsl:if>
<xsl:if test="@category = 'table'"><xsl:attribute name="class" select="'tbl'"/></xsl:if>
<td class="pre">
<xsl:apply-templates mode="#current" select="k:preceding"/>
</td>
<td class="match var{index-of($variants, string(k:word))}">
<xsl:apply-templates mode="#current" select="k:word"/>
</td>
<td class="post">
<xsl:apply-templates mode="#current" select="k:following"/>
</td>
<td class="pn">
<xsl:value-of select="@page"/>
</td>
<xsl:if test="@xml:lang != $default-language">
<td class="lang">
<xsl:value-of select="@xml:lang"/>
</td>
</xsl:if>
</tr>
</xsl:template>
<xd:doc>
<xd:short>Output a match (in right-to-left script).</xd:short>
<xd:detail>Output an HTML table-row for a match, using special processing to deal with bidirectional text.</xd:detail>
</xd:doc>
<xsl:template mode="output" match="k:match[k:word/k:w/@dir='rtl']">
<xsl:param name="variants" tunnel="yes" as="xs:string*"/>
<tr>
<xsl:if test="@category = 'index'"><xsl:attribute name="class" select="'ix'"/></xsl:if>
<xsl:if test="@category = 'note'"><xsl:attribute name="class" select="'nt'"/></xsl:if>
<xsl:if test="@category = 'figure'"><xsl:attribute name="class" select="'fig'"/></xsl:if>
<xsl:if test="@category = 'table'"><xsl:attribute name="class" select="'tbl'"/></xsl:if>
<td class="pre">
<xsl:apply-templates mode="drop-rtl-at-end" select="k:preceding"/>
<xsl:apply-templates mode="copy-rtl-at-start" select="k:following"/>
</td>
<td class="match var{index-of($variants, string(k:word))}">
<xsl:apply-templates mode="#current" select="k:word"/>
</td>
<td class="post">
<xsl:apply-templates mode="copy-rtl-at-end" select="k:preceding"/>
<xsl:apply-templates mode="drop-rtl-at-start" select="k:following"/>
</td>
<td class="pn">
<xsl:value-of select="@page"/>
</td>
<xsl:if test="@xml:lang != $default-language">
<td class="lang">
<xsl:value-of select="@xml:lang"/>
</td>
</xsl:if>
</tr>
</xsl:template>
<xd:doc>
<xd:short>Output font styles.</xd:short>
</xd:doc>
<xsl:template mode="output" match="i | b | sup | sub">
<xsl:copy><xsl:value-of select="."/></xsl:copy>
</xsl:template>
<xd:doc>
<xd:short>Output font styles (in span element).</xd:short>
</xd:doc>
<xsl:template mode="output" match="span">
<span class="{@class}"><xsl:value-of select="."/></span>
</xsl:template>
<xd:doc>
<xd:short>Reorder multi-directional scripts.</xd:short>
<xd:detail>
<p>Reorder multi-directional scripts, such that the display order is correct. Based on
the idea presented in the article <hi><a href="https://www.sketchengine.eu/wp-content/uploads/2015/03/Displaying_Bidirectional_Text_2007.pdf">Displaying Bidirectional Text Concordances in KWICformat</a></hi>.</p>
<p>The normal assumption here is that the text runs from left to right (e.g. Latin script); we also assume that a single word is either RTL or LTR.</p>
<ol>
<li>If the keyword contains no right-to-left (RTL) characters, no further processing is needed.</li>
<li>Find a sequence of RTL tokens from the end of the preceding context, and remove it.</li>
<li>Find a sequence of RTL tokens from the beginning of the following context, and remove it.</li>
<li>Move the stripped sequences to the opposite side of the opposite context.</li>
</ol>
<p>This is handled using the modes drop-rtl-at-end, copy-rtl-at-end, drop-rtl-at-start, and copy-rtl-at-start when generating the output.</p>
</xd:detail>
</xd:doc>
<xsl:template mode="drop-rtl-at-end" match="k:w[@dir='ltr']" priority="1">
<xsl:apply-templates mode="output" select="."/>
</xsl:template>
<xsl:template mode="drop-rtl-at-end" match="k:w[following-sibling::k:w[@dir='ltr']]" priority="2">
<xsl:apply-templates mode="output" select="."/>
</xsl:template>
<xsl:template mode="drop-rtl-at-end" match="k:nw[preceding-sibling::k:w[1][@dir!='rtl'] or following-sibling::k:w[@dir='ltr']]" priority="3">
<xsl:apply-templates mode="output" select="."/>
</xsl:template>
<xsl:template mode="drop-rtl-at-end" match="k:nw|k:w"/>
<xsl:template mode="copy-rtl-at-end" match="k:w[@dir='ltr']" priority="1"/>
<xsl:template mode="copy-rtl-at-end" match="k:w[following-sibling::k:w[@dir='ltr']]" priority="2"/>
<xsl:template mode="copy-rtl-at-end" match="k:nw[preceding-sibling::k:w[1][@dir!='rtl'] or following-sibling::k:w[@dir='ltr']]" priority="3"/>
<xsl:template mode="copy-rtl-at-end" match="k:nw|k:w">
<xsl:apply-templates mode="output" select="."/>
</xsl:template>
<xsl:template mode="drop-rtl-at-start" match="k:w[@dir='ltr']" priority="1">
<xsl:apply-templates mode="output" select="."/>
</xsl:template>
<xsl:template mode="drop-rtl-at-start" match="k:w[preceding-sibling::k:w[@dir='ltr']]" priority="2">
<xsl:apply-templates mode="output" select="."/>
</xsl:template>
<xsl:template mode="drop-rtl-at-start" match="k:nw[following-sibling::k:w[1][@dir!='rtl'] or preceding-sibling::k:w[@dir='ltr']]" priority="3">
<xsl:apply-templates mode="output" select="."/>
</xsl:template>
<xsl:template mode="drop-rtl-at-start" match="k:nw|k:w"/>
<xsl:template mode="copy-rtl-at-start" match="k:w[@dir='ltr']" priority="1"/>
<xsl:template mode="copy-rtl-at-start" match="k:w[preceding-sibling::k:w[@dir='ltr']]" priority="2"/>
<xsl:template mode="copy-rtl-at-start" match="k:nw[following-sibling::k:w[1][@dir!='rtl'] or preceding-sibling::k:w[@dir='ltr']]" priority="3"/>
<xsl:template mode="copy-rtl-at-start" match="k:nw|k:w">
<xsl:apply-templates mode="output" select="."/>
</xsl:template>
<!-- Not all Unicode regular expressions are supported in XSLT (https://www.regular-expressions.info/unicode.html), also,
for this tool, we use a simplified approximation of handling bidirectional script. -->
<xsl:variable name="rtl-character" select="'[֐-޿]'"/>
<xsl:function name="f:is-right-to-left" as="xs:boolean">
<xsl:param name="word" as="xs:string"/>
<!-- <xsl:sequence select="matches($word, '\p{Bidi_Class:Right_to_Left}|\p{Bidi_Class:Arabic_Letter}')"/> -->
<!-- <xsl:sequence select="matches($word, '\p{Letter}') and matches($word, '\p{Arabic}|\p{Hebrew}|\p{Syriac}|\p{Thaana}')"/> -->
<xsl:sequence select="matches($word, '\p{L}') and matches($word, $rtl-character)"/>
</xsl:function>
<xsl:function name="f:is-left-to-right" as="xs:boolean">
<xsl:param name="word" as="xs:string"/>
<!-- <xsl:sequence select="matches($word, '\p{Bidi_Class:Left_To_Right}')"/> -->
<!-- <xsl:sequence select="matches($word, '\p{Letter}') and not(matches($word, '\p{Arabic}|\p{Hebrew}|\p{Syriac}|\p{Thaana}'))"/> -->
<xsl:sequence select="matches($word, '\p{L}') and not(matches($word, $rtl-character))"/>
</xsl:function>
<xd:doc mode="single-kwic">
<xd:short>Mode used to find matches.</xd:short>
<xd:detail>This traverses the segments, looking for matching words.</xd:detail>
</xd:doc>
<xd:doc>
<xd:short>Find a match in a segment.</xd:short>
<xd:detail>Find a match in a segment.</xd:detail>
</xd:doc>
<xsl:template mode="single-kwic" match="s:segment">
<xsl:apply-templates mode="#current" select="."/>
</xsl:template>
<xd:doc>
<xd:short>Find a matching word.</xd:short>
<xd:detail>Find a matching word, when a word is found, the preceding and following contexts are kept with the match.</xd:detail>
</xd:doc>
<xsl:template mode="single-kwic" match="k:w">
<xsl:param name="keyword" required="yes"/>
<xsl:if test="$keyword = @form">
<k:match form="{@form}" page="{@page}" category="{@category}" xml:lang="{@xml:lang}">
<k:preceding><xsl:copy-of select="preceding-sibling::*[position() < $context-size]"/></k:preceding>
<k:word><xsl:copy-of select="."/></k:word>
<k:following><xsl:copy-of select="following-sibling::*[position() < $context-size]"/></k:following>
</k:match>
</xsl:if>
</xsl:template>
<xd:doc>
<xd:short>Collect preceding and following contexts.</xd:short>
<xd:detail>
<p>For every word the preceding and following contexts are kept with the match.</p>
<p>A significant improvement in speed and memory usage can be obtained here by already rendering the preceding
and following context, instead of doing this during the output phase. This is not done, as it complicates the
handling of bidirectional text. For large files, please run Saxon with sufficient memory (java -Xms3072m -Xmx3072m).</p>
</xd:detail>
</xd:doc>
<xsl:template mode="multi-kwic" match="k:w">
<xsl:if test="not(f:is-ignored-word(., @xml:lang)) and f:is-selected-language(@xml:lang)">
<k:match form="{@form}" page="{@page}" category="{@category}" xml:lang="{@xml:lang}">
<k:preceding><xsl:copy-of select="preceding-sibling::*[position() < $context-size]"/></k:preceding>
<k:word><xsl:copy-of select="."/></k:word>
<k:following><xsl:copy-of select="following-sibling::*[position() < $context-size]"/></k:following>
</k:match>
</xsl:if>
</xsl:template>
<xd:doc mode="context">
<xd:short>Mode used to copy the context of a match.</xd:short>
<xd:detail>Only used to copy preceding and following contexts when a match is found.</xd:detail>
</xd:doc>
<xd:doc>
<xd:short>Copy matching words and non-words in the context.</xd:short>
<xd:detail>Copy matching words and non-words in the context, taking care to apply the correct text style.</xd:detail>
</xd:doc>
<xsl:template mode="output" match="k:w|k:nw">
<xsl:choose>
<xsl:when test="@ref and normalize-space(.) = ''"><span class="ref"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="normalize-space(.) = ''"><xsl:value-of select="."/></xsl:when>
<xsl:when test="@ref and @style = 'i'"><i class="ref"><xsl:value-of select="."/></i></xsl:when>
<xsl:when test="@ref and @style = 'b'"><b class="ref"><xsl:value-of select="."/></b></xsl:when>
<xsl:when test="@ref and @style = 'sup'"><sup class="ref"><xsl:value-of select="."/></sup></xsl:when>
<xsl:when test="@ref and @style = 'sub'"><sub class="ref"><xsl:value-of select="."/></sub></xsl:when>
<xsl:when test="@style = 'i'"><i><xsl:value-of select="."/></i></xsl:when>
<xsl:when test="@style = 'b'"><b><xsl:value-of select="."/></b></xsl:when>
<xsl:when test="@style = 'sup'"><sup><xsl:value-of select="."/></sup></xsl:when>
<xsl:when test="@style = 'sub'"><sub><xsl:value-of select="."/></sub></xsl:when>
<xsl:when test="@ref and @style = 'sc'"><span class="sc ref"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@ref and @style = 'asc'"><span class="asc ref"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@ref and @style = 'uc'"><span class="uc ref"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@ref and @style = 'ex'"><span class="ex ref"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@ref and @style = 'tt'"><span class="tt ref"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@style = 'sc'"><span class="sc"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@style = 'asc'"><span class="asc"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@style = 'uc'"><span class="uc"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@style = 'ex'"><span class="ex"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@style = 'tt'"><span class="tt"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@style = 'green'"><span class="green"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@style = 'notemark'"><span class="notemark"><xsl:value-of select="."/></span></xsl:when>
<xsl:when test="@ref"><span class="ref"><xsl:value-of select="."/></span></xsl:when>
<xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
</xsl:choose>
</xsl:template>
<xd:doc>
<xd:short>Split a string into words.</xd:short>
<xd:detail>Split a string into words, using a regular expression syntax.</xd:detail>
<xd:param name="string">The string to be split in words.</xd:param>
</xd:doc>
<xsl:function name="f:words" as="xs:string*">
<xsl:param name="string" as="xs:string"/>
<xsl:analyze-string select="$string" regex="{'[\p{L}\p{N}\p{M}′-]+'}">
<xsl:matching-substring>
<xsl:sequence select="."/>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:function>
<xd:doc>
<xd:short>Normalize a word, based on the current settings.</xd:short>
</xd:doc>
<xsl:function name="f:normalize-string" as="xs:string">
<xsl:param name="string" as="xs:string"/>
<xsl:variable name="string" select="if ($case-sensitive = 'true') then $string else lower-case($string)"/>
<xsl:variable name="string" select="f:strip-diacritics-and-marks($string)"/>
<xsl:variable name="string" select="f:normalize-ligatures($string)"/>
<xsl:variable name="string" select="f:normalize-special-letters($string)"/>
<xsl:variable name="string" select="f:normalize-mixup-characters($string)"/>
<xsl:sequence select="$string"/>
</xsl:function>
<xd:doc>
<xd:short>Remove diacritics from a string.</xd:short>
<xd:detail>Remove diacritics form a string to produce a string suitable for sorting purposes. This function
uses the Unicode NFD normalization form to separate diacritics from the letters carrying them, and might
result too much being removed in some scripts (in particular Indic scripts, where vowel-signs are stripped).</xd:detail>
<xd:param name="string">The string to processed.</xd:param>
</xd:doc>
<xsl:function name="f:strip-diacritics-and-marks" as="xs:string">
<xsl:param name="string" as="xs:string"/>
<xsl:variable name="string" select="fn:replace($string, '[ـʾʼʽʿʹ&tcomma;′&mlrexcl;-]', '')" as="xs:string"/>
<xsl:variable name="string" select="f:mask-indic-vowel-signs($string)" as="xs:string"/>
<xsl:variable name="string" select="fn:replace(fn:normalize-unicode($string, 'NFD'), '\p{M}', '')" as="xs:string"/>
<xsl:variable name="string" select="f:restore-indic-vowel-signs($string)" as="xs:string"/>
<xsl:sequence select="$string"/>
</xsl:function>
<xsl:function name="f:mask-indic-vowel-signs" as="xs:string">
<xsl:param name="string" as="xs:string"/>
<!-- Devanagari vowel signs and virama -->
<xsl:variable name="devanagari" select="'ऺऻा-्'" as="xs:string"/>
<!-- Bengali vowel signs and virama -->
<xsl:variable name="bengali" select="'া-ৄে-ৈো-্'" as="xs:string"/>
<!-- Gujarati vowel signs and virama -->
<xsl:variable name="gujarati" select="'ા-ૅે-ૉો-્'" as="xs:string"/>
<!-- Tibetan vowel signs and subjoined consonants -->
<xsl:variable name="tibetan" select="'ཱ-ཽྐ-ྗྙ-ྼ'" as="xs:string"/>
<xsl:variable name="result">
<xsl:analyze-string select="$string" regex="{'[' || $devanagari || $bengali || $gujarati || $tibetan || ']'}">
<xsl:matching-substring>
<xsl:value-of select="'&startMark;' || string-to-codepoints(.) || '&endMark;'"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:sequence select="string-join($result, '')"/>
</xsl:function>
<xsl:function name="f:restore-indic-vowel-signs" as="xs:string">
<xsl:param name="string" as="xs:string"/>
<xsl:variable name="result">
<xsl:analyze-string select="$string" regex="&startMark;([0-9]+)&endMark;">
<xsl:matching-substring>
<xsl:value-of select="codepoints-to-string(xs:integer(regex-group(1)))"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:sequence select="string-join($result, '')"/>
</xsl:function>
<xsl:function name="f:normalize-ligatures" as="xs:string">
<xsl:param name="string" as="xs:string"/>
<xsl:variable name="string" select="fn:replace($string, 'ß', 'ss')"/> <!-- German eszet -->
<xsl:variable name="string" select="fn:replace($string, 'ẞ', 'SS')"/> <!-- German capital eszet -->
<xsl:variable name="string" select="fn:replace($string, 'Æ', 'AE')"/> <!-- AE ligature -->
<xsl:variable name="string" select="fn:replace($string, 'æ', 'ae')"/> <!-- ae ligature -->
<xsl:variable name="string" select="fn:replace($string, 'IJ', 'IJ')"/> <!-- Dutch IJ -->
<xsl:variable name="string" select="fn:replace($string, 'ij', 'ij')"/> <!-- Dutch ij -->
<xsl:variable name="string" select="fn:replace($string, 'Œ', 'OE')"/> <!-- OE ligature -->
<xsl:variable name="string" select="fn:replace($string, 'œ', 'oe')"/> <!-- oe ligature -->
<xsl:variable name="string" select="fn:replace($string, 'Ꜵ', 'AO')"/> <!-- Ligature AO (Old Icelandic) -->
<xsl:variable name="string" select="fn:replace($string, 'ꜵ', 'ao')"/> <!-- Ligature ao (Old Icelandic) -->
<xsl:variable name="string" select="fn:replace($string, 'Ỻ', 'LL')"/> <!-- ligature Ll (older Welsh) -->
<xsl:variable name="string" select="fn:replace($string, 'ỻ', 'll')"/> <!-- Ligature ll (older Welsh) -->
<xsl:sequence select="$string"/>
</xsl:function>
<xsl:function name="f:normalize-special-letters" as="xs:string">
<xsl:param name="string" as="xs:string"/>
<xsl:variable name="string" select="fn:replace($string, 'Ø', 'O')"/> <!-- Latin capital letter O with stroke -->
<xsl:variable name="string" select="fn:replace($string, 'ø', 'o')"/> <!-- Latin small letter o with stroke -->
<xsl:variable name="string" select="fn:replace($string, 'Ð', 'D')"/> <!-- Latin capital letter Eth -->
<xsl:variable name="string" select="fn:replace($string, 'ð', 'd')"/> <!-- Latin small letter eth -->
<xsl:variable name="string" select="fn:replace($string, 'Đ', 'D')"/> <!-- Latin capital letter D with stroke -->
<xsl:variable name="string" select="fn:replace($string, 'đ', 'd')"/> <!-- Latin small letter d with stroke -->
<xsl:variable name="string" select="fn:replace($string, 'Ħ', 'H')"/> <!-- Latin capital letter H with stroke -->
<xsl:variable name="string" select="fn:replace($string, 'ħ', 'h')"/> <!-- Latin small letter h with stroke -->
<xsl:variable name="string" select="fn:replace($string, 'Ł', 'L')"/> <!-- Latin capital letter L with stroke -->
<xsl:variable name="string" select="fn:replace($string, 'ł', 'l')"/> <!-- Latin small letter l with stroke -->
<xsl:variable name="string" select="fn:replace($string, 'Ŧ', 'T')"/> <!-- Latin capital letter T with stroke -->
<xsl:variable name="string" select="fn:replace($string, 'ŧ', 't')"/> <!-- Latin small letter t with stroke -->
<xsl:variable name="string" select="fn:replace($string, 'ſ', 's')"/> <!-- Latin small letter long s -->
<xsl:variable name="string" select="fn:replace($string, 'ⁿ', 'n')"/> <!-- Superscript Latin small letter n -->
<!-- Phonetic extensions -->
<xsl:variable name="normalization-map" as="map(xs:string, xs:string)" select="map {
'ᴀ' : 'A',
'ᴄ' : 'C',
'ᴅ' : 'D',
'ᴇ' : 'E',
'ᴊ' : 'J',
'ᴋ' : 'K',
'ᴌ' : 'L',
'ᴍ' : 'M'
}"/>
<xsl:variable name="string" select="fn:replace($string, 'ᴀ', 'a')"/> <!-- Latin Letter Small Capital A -->
<xsl:variable name="string" select="fn:replace($string, 'ᴇ', 'e')"/> <!-- Latin Letter Small Capital E -->
<xsl:variable name="string" select="fn:replace($string, 'ʟ', 'l')"/> <!-- Latin Letter Small Capital L -->
<xsl:variable name="string" select="fn:replace($string, 'ᴌ', 'l')"/> <!-- Latin Letter Small Capital L with stroke -->
<!-- Hebrew script -->
<xsl:variable name="string" select="fn:replace($string, 'ﬡ', 'א')"/> <!-- Hebrew letter wide alef -> alef -->
<xsl:variable name="string" select="fn:replace($string, 'ﬢ', 'ד')"/> <!-- Hebrew letter wide dalet -> dalet -->
<xsl:variable name="string" select="fn:replace($string, 'ﬣ', 'ה')"/> <!-- Hebrew letter wide he -> he -->
<xsl:variable name="string" select="fn:replace($string, 'ﬤ', 'כ')"/> <!-- Hebrew letter wide kaf -> kaf -->
<xsl:variable name="string" select="fn:replace($string, 'ﬥ', 'ל')"/> <!-- Hebrew letter wide lamed -> lamed -->
<xsl:variable name="string" select="fn:replace($string, 'ﬦ', 'ם')"/> <!-- Hebrew letter wide final mem -> final mem -->
<xsl:variable name="string" select="fn:replace($string, 'ﬧ', 'ר')"/> <!-- Hebrew letter wide resh -> resh -->
<xsl:variable name="string" select="fn:replace($string, 'ﬨ', 'ת')"/> <!-- Hebrew letter wide tav -> tav -->
<xsl:sequence select="$string"/>
</xsl:function>
<xsl:function name="f:normalize-mixup-characters" as="xs:string">
<xsl:param name="string" as="xs:string"/>
<!-- replace all potentially mixed-up characters in the list whith the first in the sequence -->
<xsl:sequence select="if ($mixup-sequence[2])
then f:multi-replace($string, $mixup-sequence[position() > 1], $mixup-sequence[1])
else $string"/>
</xsl:function>