forked from filak/hOCR-to-ALTO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hocr2alto2.1.xsl
146 lines (115 loc) · 5.91 KB
/
hocr2alto2.1.xsl
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
<?xml version="1.0" encoding="UTF-8"?>
<!--
Author: Filip Kriz (@filak)
Version: 1.3.1 13-9-2016
License: MIT License (MIT)
-->
<xsl:stylesheet version="2.0"
xmlns="http://www.loc.gov/standards/alto/ns-v2#"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mf="http://myfunctions"
xpath-default-namespace="*"
exclude-result-prefixes="mf">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*"/>
<!-- Optional: ISO 639-2/B 3-letter code for default language -->
<xsl:param name="language" select="unknown" />
<xsl:variable name="langcodes" select="document('codes_lookup.xml')/*:codes/*:code" />
<xsl:template match="/">
<alto xsi:schemaLocation="http://www.loc.gov/standards/alto/ns-v2# http://www.loc.gov/standards/alto/alto.xsd">
<xsl:apply-templates/>
</alto>
</xsl:template>
<xsl:template match="*:head">
<Description>
<MeasurementUnit>pixel</MeasurementUnit>
<sourceImageInformation>
<xsl:variable name="title" select="//*:body/*:div[@class='ocr_page']/@title"/>
<fileName><xsl:value-of select="mf:getFname($title)"/></fileName>
</sourceImageInformation>
<OCRProcessing ID="IdOcr">
<ocrProcessingStep>
<processingSoftware>
<softwareName><xsl:value-of select="*:meta[@name='ocr-system']/@content"/></softwareName>
<softwareVersion><xsl:value-of select="*:meta[@name='ocr-system']/@content"/></softwareVersion>
</processingSoftware>
</ocrProcessingStep>
</OCRProcessing>
</Description>
</xsl:template>
<xsl:template match="*:body/*:div[@class='ocr_page']">
<Layout>
<!-- bbox 552 999 1724 1141 x1-L2-T3-R4-B5 -->
<xsl:variable name="box" select="tokenize(mf:getBoxPage(@title), ' ')"/>
<Page ID="{@id}" PHYSICAL_IMG_NR="1" HEIGHT="{$box[5]}" WIDTH="{$box[4]}">
<PrintSpace HEIGHT="{$box[5]}" WIDTH="{$box[4]}" VPOS="0" HPOS="0">
<xsl:apply-templates select="*:div[@class='ocr_carea']"/>
</PrintSpace>
</Page>
</Layout>
</xsl:template>
<xsl:template match="*:div[@class='ocr_carea']">
<xsl:variable name="box" select="tokenize(mf:getBox(@title), ' ')"/>
<ComposedBlock ID="{@id}" HEIGHT="{number($box[5]) - number($box[3])}" WIDTH="{number($box[4]) - number($box[2])}" VPOS="{$box[3]}" HPOS="{$box[2]}">
<xsl:apply-templates select="*:p[@class='ocr_par']"/>
</ComposedBlock>
</xsl:template>
<xsl:template match="*:p[@class='ocr_par']">
<xsl:variable name="box" select="tokenize(mf:getBox(@title), ' ')"/>
<TextBlock ID="{@id}" HEIGHT="{number($box[5]) - number($box[3])}" WIDTH="{number($box[4]) - number($box[2])}" VPOS="{$box[3]}" HPOS="{$box[2]}">
<xsl:choose>
<xsl:when test="@lang != ''">
<xsl:variable name="lookup" select="@lang" />
<xsl:attribute name="LANG">
<xsl:value-of select="$langcodes[@a3h=$lookup]/@a3b" />
</xsl:attribute>
</xsl:when>
<xsl:when test="$language != 'unknown'">
<xsl:attribute name="LANG">
<xsl:value-of select="$language"/>
</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:apply-templates select="*:span[@class='ocr_line']"/>
</TextBlock>
</xsl:template>
<xsl:template match="*:span[@class='ocr_line']">
<xsl:variable name="box" select="tokenize(mf:getBox(@title), ' ')"/>
<TextLine ID="{@id}" HEIGHT="{number($box[5]) - number($box[3])}" WIDTH="{number($box[4]) - number($box[2])}" VPOS="{$box[3]}" HPOS="{$box[2]}">
<xsl:apply-templates select="*:span[@class='ocrx_word']"/>
</TextLine>
</xsl:template>
<xsl:template match="*:span[@class='ocrx_word']">
<xsl:variable name="box" select="tokenize(mf:getBox(@title), ' ')"/>
<xsl:choose>
<xsl:when test="*:strong">
<String ID="{@id}" CONTENT="{normalize-space(.)}" HEIGHT="{number($box[5]) - number($box[3])}" WIDTH="{number($box[4]) - number($box[2])}" VPOS="{$box[3]}" HPOS="{$box[2]}" STYLE="bold"/>
</xsl:when>
<xsl:when test="*:em">
<String ID="{@id}" CONTENT="{normalize-space(.)}" HEIGHT="{number($box[5]) - number($box[3])}" WIDTH="{number($box[4]) - number($box[2])}" VPOS="{$box[3]}" HPOS="{$box[2]}" STYLE="italics"/>
</xsl:when>
<xsl:when test="*:i">
<String ID="{@id}" CONTENT="{normalize-space(.)}" HEIGHT="{number($box[5]) - number($box[3])}" WIDTH="{number($box[4]) - number($box[2])}" VPOS="{$box[3]}" HPOS="{$box[2]}" STYLE="italics"/>
</xsl:when>
<xsl:otherwise>
<String ID="{@id}" CONTENT="{normalize-space(.)}" HEIGHT="{number($box[5]) - number($box[3])}" WIDTH="{number($box[4]) - number($box[2])}" VPOS="{$box[3]}" HPOS="{$box[2]}" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:function name="mf:getFname">
<xsl:param name="titleString"/>
<xsl:variable name="pPat">"</xsl:variable>
<xsl:variable name="fpath" select="tokenize(tokenize(normalize-space($titleString),'; ')[1],' ')[2]"/>
<xsl:value-of select="reverse(tokenize(replace($fpath,$pPat,''),'\\'))[1]"/>
</xsl:function>
<xsl:function name="mf:getBoxPage">
<xsl:param name="titleString"/>
<xsl:value-of select="tokenize(normalize-space($titleString),'; ')[2]"/>
</xsl:function>
<xsl:function name="mf:getBox">
<xsl:param name="titleString"/>
<xsl:value-of select="tokenize(normalize-space($titleString),'; ')"/>
</xsl:function>
</xsl:stylesheet>