Skip to content

Commit da07641

Browse files
committed
Fix levenshtein with costs. The previous code multiplied by the cost in only
3 of the 7 relevant locations. Marcin Mank, slightly adjusted by me.
1 parent 03d7b06 commit da07641

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

contrib/fuzzystrmatch/fuzzystrmatch.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Joe Conway <mail@joeconway.com>
77
*
8-
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.c,v 1.30 2009/06/11 14:48:51 momjian Exp $
8+
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.c,v 1.31 2009/12/10 01:54:17 rhaas Exp $
99
* Copyright (c) 2001-2009, PostgreSQL Global Development Group
1010
* ALL RIGHTS RESERVED;
1111
*
@@ -207,13 +207,13 @@ levenshtein_internal(const char *s, const char *t,
207207
n = strlen(t);
208208

209209
/*
210-
* If either m or n is 0, the answer is the other value. This makes sense
211-
* since it would take that many insertions to build a matching string
210+
* We can transform an empty s into t with n insertions, or a non-empty t
211+
* into an empty s with m deletions.
212212
*/
213213
if (!m)
214-
return n;
214+
return n * ins_c;
215215
if (!n)
216-
return m;
216+
return m * del_c;
217217

218218
/*
219219
* For security concerns, restrict excessive CPU+RAM usage. (This
@@ -241,7 +241,7 @@ levenshtein_internal(const char *s, const char *t,
241241

242242
/* Initialize the "previous" row to 0..cols */
243243
for (i = 0; i < m; i++)
244-
prev[i] = i;
244+
prev[i] = i * del_c;
245245

246246
/* Loop through rows of the notional array */
247247
for (y = t, j = 1; j < n; y++, j++)
@@ -252,7 +252,7 @@ levenshtein_internal(const char *s, const char *t,
252252
* First cell must increment sequentially, as we're on the j'th row of
253253
* the (m+1)x(n+1) array.
254254
*/
255-
curr[0] = j;
255+
curr[0] = j * ins_c;
256256

257257
for (x = s, i = 1; i < m; x++, i++)
258258
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy