| 1 | <?xml version='1.0' encoding='UTF-8'?> |
| 2 | <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> |
| 3 | |
| 4 | <!-- |
| 5 | |
| 6 | $Revision$ |
| 7 | |
| 8 | This stylesheet converts a TEI dictionary where homographs are not |
| 9 | necessarily grouped with <hom> elements into a TEI file, where this |
| 10 | grouping exists. Grouped homographs are required for conversion to |
| 11 | bedic format, where multiple entries with headwords that are equal |
| 12 | are not allowed. |
| 13 | |
| 14 | Grouping is done with the <hom> element and not the <superEntry> element, |
| 15 | so the structure could be translated more directly into the bedic format |
| 16 | by the SAX event handlers in `tei2dic.py'. |
| 17 | |
| 18 | This variant expects the entries sorted in any order, as long as |
| 19 | entries where the first <orth> elements compare equal, are next to each |
| 20 | other. The <xsl:sort> element can be used for that (see `sort.xsl'). |
| 21 | By processing a sorted dictionary we are trying to be faster by not |
| 22 | having to look more than one entry backward for homographs. |
| 23 | This variant is worlds faster than the variant without sorting. |
| 24 | |
| 25 | <gramGrp> elements become children of the respective <hom> elements. |
| 26 | |
| 27 | Limitations: |
| 28 | |
| 29 | * entries with multiple <orth> elements are not supported |
| 30 | * <pron> elements of all but the first homograph are discarded |
| 31 | * the <pron> element of the first homograph will apply for all homographs |
| 32 | * fate of preexistent <hom> elements undefined |
| 33 | |
| 34 | If you process the output of this stylesheet further into the bedic format, |
| 35 | the xerox tool of bedic might give warnings about duplicate headwords. These |
| 36 | may be caused by characters from the "search-ignore-chars" property in bedic |
| 37 | appearing in headwords, eg. an entry with headword "ba" and one with headword |
| 38 | "ba-" compare equal, causing a warning about them to be emitted. |
| 39 | |
| 40 | Another cause of duplicate warnings from xerox is capitalization eg. english |
| 41 | "turkey" (the bird) and "Turkey" (the country) compare equal according to the |
| 42 | default "char-precedence" property. |
| 43 | |
| 44 | Those warnings are no source of concern. To avoid them you could adjust the |
| 45 | respective properties for the cost of loosing flexibility in word lookup. |
| 46 | |
| 47 | --> |
| 48 | |
| 49 | <!-- |
| 50 | |
| 51 | Using the doctype-public and doctype-system attributes here is in vain, |
| 52 | since TEI needs an internal subset to include optional portions of the |
| 53 | TEI DTD |
| 54 | |
| 55 | --> |
| 56 | <xsl:output method="xml" encoding="UTF-8"/> |
| 57 | |
| 58 | <!-- |
| 59 | |
| 60 | Since XSLT 1.1 provides no way of outputting an internal DTD subset, we use |
| 61 | a wrapper script as suggested in the XSLT Recommendation, |
| 62 | 16.1 XML Output Method. The wrapper contains the FreeDict default of an |
| 63 | internal subset and will try to include the output of this stylesheet |
| 64 | via an entity reference. The output of this file has to be saved in a |
| 65 | file called `sorted.tei'. For the wrapper to work we have to: |
| 66 | |
| 67 | 1. Do not output the TEI.2 element. If we output it, the wrapper would |
| 68 | have leave it out, which would make him non-well formed. |
| 69 | 2. The wrapper and the output of this stylesheet are then used together. |
| 70 | 3. Optionally, unwrapping to a single file can be done like this |
| 71 | (replace each + by minus signs): |
| 72 | |
| 73 | xmllint ++noent tei-wrapper.xml >unwrapped.tei |
| 74 | |
| 75 | --> |
| 76 | |
| 77 | <!-- |
| 78 | |
| 79 | The 'verbose' parameter determines, whether to output informational messages |
| 80 | or not. It is off by default. To switch it on using Sablotron, use: |
| 81 | |
| 82 | sabcmd group-homographs.xsl infile.tei >grouped.tei '$verbose=1' |
| 83 | |
| 84 | Though not required, you can explicitly switch it off for example by calling |
| 85 | the stylesheet with the Sablotron XSLT processor this way: |
| 86 | |
| 87 | sabcmd group-homographs.xsl infile.tei >grouped.tei '$verbose=' |
| 88 | |
| 89 | As the parameter is always handed over as a string, only the empty string will |
| 90 | be interpreted as a boolean 'false'. |
| 91 | |
| 92 | --> |
| 93 | <xsl:param name="verbose" select="''"/> |
| 94 | |
| 95 | <!-- Do no output the TEI.2 element --> |
| 96 | <xsl:template match="/TEI.2"> |
| 97 | <xsl:apply-templates/> |
| 98 | </xsl:template> |
| 99 | |
| 100 | <xsl:template match="text/body"> |
| 101 | <body> |
| 102 | <xsl:apply-templates select="entry"> |
| 103 | <xsl:sort select="form/orth[1]"/> |
| 104 | </xsl:apply-templates> |
| 105 | </body> |
| 106 | </xsl:template> |
| 107 | |
| 108 | <xsl:template match="entry"> |
| 109 | <xsl:variable name="myorth" select="form/orth"/> |
| 110 | |
| 111 | <!-- Progress message every 100 entries, if verbose mode --> |
| 112 | <xsl:if test="$verbose and (position() mod 100)=0"> |
| 113 | <xsl:message terminate="no">Processing entry <xsl:value-of select="position()"/></xsl:message> |
| 114 | </xsl:if> |
| 115 | |
| 116 | <xsl:choose> |
| 117 | |
| 118 | <!-- |
| 119 | |
| 120 | If an entry has a preceding homograph, skip it, |
| 121 | as it has already been processed. In this place, since |
| 122 | the entry node list is sorted, it is enough to check |
| 123 | just the first preceding entry. |
| 124 | |
| 125 | We use the preceding/following-sibling axises here. |
| 126 | Note that they refer to the entries of the document in |
| 127 | document order. If we did the sorting in this stylesheet, |
| 128 | we would require a means to refer to the sorted list. |
| 129 | We would have to refer to the preceding/following nodes in the |
| 130 | context node list. Unfortunately, there are no such things |
| 131 | as context node list axises or a function to access the |
| 132 | context node list in XSLT 1.1. |
| 133 | |
| 134 | --> |
| 135 | <xsl:when test="preceding-sibling::entry[1]/form/orth = $myorth"> |
| 136 | <xsl:if test="$verbose"> |
| 137 | <xsl:message terminate="no"> |
| 138 | <xsl:text> Skipping already handled homograph '</xsl:text> |
| 139 | <xsl:value-of select="$myorth"/> |
| 140 | <xsl:text>'.</xsl:text> |
| 141 | </xsl:message> |
| 142 | </xsl:if> |
| 143 | </xsl:when> |
| 144 | |
| 145 | <!-- |
| 146 | |
| 147 | If the immediately following entry is not a homograph, then |
| 148 | the current entry has no homographs and can be copied. |
| 149 | |
| 150 | --> |
| 151 | <xsl:when test="following-sibling::entry[1]/form/orth != $myorth"> |
| 152 | <entry> |
| 153 | <xsl:apply-templates/> |
| 154 | </entry> |
| 155 | </xsl:when> |
| 156 | |
| 157 | <!-- This entry has homographs after it. Group them using the <hom> element --> |
| 158 | <xsl:otherwise> |
| 159 | |
| 160 | <entry><xsl:text>
</xsl:text> |
| 161 | <xsl:text>
 </xsl:text> |
| 162 | |
| 163 | <xsl:if test="$verbose"> |
| 164 | <xsl:message terminate="no"> |
| 165 | <xsl:text>Transforming using <hom>: '</xsl:text> |
| 166 | <xsl:value-of select="$myorth"/> |
| 167 | <xsl:text>' has </xsl:text> |
| 168 | <!--xsl:value-of select="1+count($myfollowinghomographs)"/--> |
| 169 | <xsl:text>? homographs.</xsl:text> |
| 170 | </xsl:message> |
| 171 | </xsl:if> |
| 172 | |
| 173 | <xsl:if test="count($myorth) > 1"> |
| 174 | <xsl:message terminate="no"> |
| 175 | <xsl:text>Warning: Can't handle multiple <orth> elements: </xsl:text> |
| 176 | <xsl:for-each select="$myorth"> |
| 177 | '<xsl:value-of select="."/>'<xsl:text> </xsl:text> |
| 178 | </xsl:for-each>- Results undefined. They should be separated into their own entries. |
| 179 | </xsl:message> |
| 180 | </xsl:if> |
| 181 | |
| 182 | <!-- XXX we should remove the <pron> from the first <form> --> |
| 183 | <xsl:apply-templates select="form"/> |
| 184 | <xsl:text>
</xsl:text> |
| 185 | |
| 186 | <!-- copy the first homograph --> |
| 187 | <xsl:text>
 </xsl:text> |
| 188 | <hom> |
| 189 | <xsl:text>
 </xsl:text> |
| 190 | <xsl:apply-templates select="*[name(.) != 'form']"/> |
| 191 | <xsl:text>
 </xsl:text> |
| 192 | </hom> |
| 193 | <xsl:text>
 </xsl:text> |
| 194 | |
| 195 | <!-- copy the following homographs --> |
| 196 | <xsl:call-template name="recursive-following-homograps-into-hom"> |
| 197 | <xsl:with-param name="orth-of-first-homograph" select="$myorth"/> |
| 198 | <xsl:with-param name="current" select="."/> |
| 199 | </xsl:call-template> |
| 200 | |
| 201 | <xsl:text>
 </xsl:text> |
| 202 | </entry> |
| 203 | </xsl:otherwise> |
| 204 | </xsl:choose> |
| 205 | </xsl:template> |
| 206 | |
| 207 | <xsl:template name="recursive-following-homograps-into-hom"> |
| 208 | <xsl:param name="orth-of-first-homograph"/> |
| 209 | <xsl:param name="current" value="."/> |
| 210 | |
| 211 | <xsl:variable name="next" select="$current/following-sibling::entry[1]"/> |
| 212 | |
| 213 | <xsl:if test="$next/form/orth = $orth-of-first-homograph"> |
| 214 | <xsl:text>
 </xsl:text> |
| 215 | <hom> |
| 216 | <xsl:text>
 </xsl:text> |
| 217 | <xsl:apply-templates select="$next/*[name(.) != 'form']"/> |
| 218 | <xsl:text>
 </xsl:text> |
| 219 | </hom> |
| 220 | <xsl:text>
 </xsl:text> |
| 221 | |
| 222 | <!-- Look forward recursively until we came across the first non-homograph |
| 223 | |
| 224 | eng-spa, 5910 entries |
| 225 | |
| 226 | Unrecursive: |
| 227 | real 2m33.495s |
| 228 | user 1m41.348s |
| 229 | sys 0m0.431s |
| 230 | |
| 231 | Recursive: |
| 232 | real 0m10.815s |
| 233 | user 0m7.035s |
| 234 | sys 0m0.388s |
| 235 | |
| 236 | --> |
| 237 | <xsl:call-template name="recursive-following-homograps-into-hom"> |
| 238 | <xsl:with-param name="orth-of-first-homograph" select="$orth-of-first-homograph"/> |
| 239 | <xsl:with-param name="current" select="$next"/> |
| 240 | </xsl:call-template> |
| 241 | |
| 242 | </xsl:if> |
| 243 | </xsl:template> |
| 244 | |
| 245 | <!-- if no other template matches, copy the encountered attributes and elements --> |
| 246 | <xsl:template match='@* | node()'> |
| 247 | <xsl:copy><xsl:apply-templates select='@* | node()'/></xsl:copy> |
| 248 | </xsl:template> |
| 249 | |
| 250 | </xsl:stylesheet> |
| 251 |