File 4133-stdlib-Fix-jaro_similarity-for-matching-strings-of-l.patch of Package erlang
From 69422aa4d14b1084d5343d7cf4e15f20a0a5dced Mon Sep 17 00:00:00 2001
From: Jesse Stimpson <jesse.stimpson@gmail.com>
Date: Sun, 2 Feb 2025 10:32:37 -0500
Subject: [PATCH] stdlib: Fix jaro_similarity for matching strings of length 1
---
lib/stdlib/src/string.erl | 2 +-
lib/stdlib/test/string_SUITE.erl | 3 +++
2 files changed, 4 insertions(+), 1 deletion(-)
diff --git a/lib/stdlib/src/string.erl b/lib/stdlib/src/string.erl
index 6df9ba75b1..7ee5ccffb0 100644
--- a/lib/stdlib/src/string.erl
+++ b/lib/stdlib/src/string.erl
@@ -1061,7 +1061,7 @@ The Jaro distance between two strings can be calculated with
jaro_similarity(A0, B0) ->
{A, ALen} = str_to_gcl_and_length(A0),
{B, BLen} = str_to_indexmap(B0),
- Dist = max(ALen, BLen) div 2,
+ Dist = max(1, max(ALen, BLen) div 2),
{AM, BM} = jaro_match(A, B, -Dist, Dist, [], []),
if
ALen =:= 0 andalso BLen =:= 0 ->
diff --git a/lib/stdlib/test/string_SUITE.erl b/lib/stdlib/test/string_SUITE.erl
index 4e33a4d47b..b4ab56e1bd 100644
--- a/lib/stdlib/test/string_SUITE.erl
+++ b/lib/stdlib/test/string_SUITE.erl
@@ -808,6 +808,9 @@ jaro_similarity(_Config) ->
?TEST("Sunday", ["Saturday"], 0.71944444),
%% Short strings (no translations counted)
+ ?TEST("a", ["a"], 1.0),
+ ?TEST("c", ["a"], 0.0),
+ ?TEST("ca", ["ac"], 0.0),
?TEST("ca", ["abc"], 0.0),
?TEST("ca", ["cb"], ((1/2+1/2+1)/3)),
?TEST("ca", ["cab"], ((2/2+2/3+1)/3)),
--
2.43.0