From: Pierre Choffet <peuc@wanadoo.fr>
Date: Sat, 15 Jul 2023 06:36:57 +0000 (-0400)
Subject: Add translation string fuzzy compare.
X-Git-Url: https://git.choffet.net/?a=commitdiff_plain;h=d1f760f3b50f178002919e0aab0ed1a8b0fff285;p=wdef_tools.git

Add translation string fuzzy compare.

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.
---

diff --git a/xslts/merge_rdf.xslt b/xslts/merge_rdf.xslt
index bb37306..4297046 100644
--- a/xslts/merge_rdf.xslt
+++ b/xslts/merge_rdf.xslt
@@ -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
@@ -197,8 +197,30 @@
 			</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>
@@ -354,6 +376,23 @@
 		</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" />