]> Pierre Choffet | Git repositories - wdef_tools.git/commitdiff
Add translation string fuzzy compare.
authorPierre Choffet <peuc@wanadoo.fr>
Sat, 15 Jul 2023 06:36:57 +0000 (02:36 -0400)
committerPierre Choffet <peuc@wanadoo.fr>
Sat, 15 Jul 2023 06:36:57 +0000 (02:36 -0400)
It allows case unsensitive compare, with additional approximations. User will now receive a warning if a translation is added in a language which already has a string.

xslts/merge_rdf.xslt

index bb3730689081e1bdd4918bda375ca3805a351e6d..4297046ba2f15078786440526e9127156c688ae7 100644 (file)
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!-- merge_rdf.xslt - Merge Wikidata element properties from its RDF.
-     Copyright (C) 2020, 2021, 2022  Pierre Choffet
+     Copyright (C) 2020, 2021, 2022, 2023  Pierre Choffet
 
      This program is free software: you can redistribute it and/or modify
      it under the terms of version 3 of the GNU General Public License as
                        </xsl:when>
                        <xsl:when test="wdef:translation">
                                <xsl:choose>
-                                       <xsl:when test="$wd-resource/*[name(.) = concat('wdt:', $PID) and @xml:lang = current()/wdef:translation/@xml:lang and text() = current()/wdef:translation/text()]">
-                                               <xsl:text>yes</xsl:text>
+                                       <xsl:when test="$wd-resource/*[name(.) = concat('wdt:', $PID) and @xml:lang = current()/wdef:translation/@xml:lang]">
+                                               <xsl:variable name="string1" select="$wd-resource/*[name(.) = concat('wdt:', $PID) and @xml:lang = current()/wdef:translation/@xml:lang]/text()"/>
+                                               <xsl:variable name="string2" select="current()/wdef:translation/text()"/>
+                                               <xsl:variable name="similar-translation">
+                                                       <xsl:call-template name="strings-similar" mode="relax">
+                                                               <xsl:with-param name="string1" select="$string1"/>
+                                                               <xsl:with-param name="string2" select="$string2"/>
+                                                       </xsl:call-template>
+                                               </xsl:variable>
+                                               
+                                               <xsl:if test="$similar-translation = 'no'">
+                                                       <xsl:message terminate="no">
+                                                               <xsl:text>WARNING: </xsl:text>
+                                                               <xsl:value-of select="$element-id"/>
+                                                               <xsl:text>-</xsl:text>
+                                                               <xsl:value-of select="$PID"/>
+                                                               <xsl:text>: Add translation (</xsl:text>
+                                                               <xsl:value-of select="$string2"/>
+                                                               <xsl:text>) while another different one (</xsl:text>
+                                                               <xsl:value-of select="$string1"/>
+                                                               <xsl:text>) already exists in this language.</xsl:text>
+                                                       </xsl:message>
+                                               </xsl:if>
+                                               <xsl:value-of select="$similar-translation"/>
                                        </xsl:when>
                                        <xsl:otherwise>
                                                <xsl:text>no</xsl:text>
                </xsl:choose>
        </xsl:template>
        
+       <xsl:template name="strings-similar" mode="relax">
+               <xsl:param name="string1"/>
+               <xsl:param name="string2"/>
+               
+               <xsl:variable name="translate-from">ABCDEFGHIJKLMNOPQRSTUVWXYZÀàÂâÇçÉÈéèêÎîÔôÛûÙù  :-_’',?!()</xsl:variable>
+               <xsl:variable name="translate-to"  >abcdefghijklmnopqrstuvwxyzaaaacceeeeeiioouuuu            </xsl:variable>
+               
+               <xsl:choose>
+                       <xsl:when test="translate(translate($string1, $translate-from, $translate-to), ' ', '') = translate(translate($string2, $translate-from, $translate-to), ' ', '')">
+                               <xsl:text>yes</xsl:text>
+                       </xsl:when>
+                       <xsl:otherwise>
+                               <xsl:text>no</xsl:text>
+                       </xsl:otherwise>
+               </xsl:choose>
+       </xsl:template>
+       
        <xsl:template name="substring-after-last">
                <xsl:param name="string" />
                <xsl:param name="delimiter" />