CREATE OR REPLACE function SHERLOCK.levenshtein_distance( sOriginalIn varchar2, sNewIn varchar2) return number deterministic is
type table_of_numbers is table of number index by pls_integer;
type array_of_tables is table of table_of_numbers index by pls_integer;
levenshtein_2d_grid array_of_tables;
type char_table_type is table of char(1) index by binary_integer;
origenS char_table_type;
newT char_table_type;
originLength pls_integer := length(sOriginalIn);
newLength pls_integer := length(sNewIn);
level_cost pls_integer;
begin
if originLength = 0
then return newLength;
end if;
if newLength = 0
then return originLength;
end if;
--let's load up the arrays
levenshtein_2d_grid (0) (0) := 0;
for i in 1..originLength
loop
origenS(i) := substr(sOriginalIn, i, 1);
levenshtein_2d_grid (i) (0) := i;
for j in 1..newLength
loop
newT(j) := substr(sNewIn, j, 1);
levenshtein_2d_grid (0) (j) := j;
if origenS(i) = newT(j)
then level_cost := 0;
else level_cost := 1;
end if;
levenshtein_2d_grid (i) (j) := least ( levenshtein_2d_grid (i-1)(j) + 1, levenshtein_2d_grid (i)(j-1) + 1, levenshtein_2d_grid (i-1)(j-1) + level_cost);
--dbms_output.put_line(i||':'||j||' := '||
--levenshtein_2d_grid (i) (j)||', cost:='||level_cost||
--', origenS(i):='||origenS(i)||', newT(j):='||newT(j));
end loop;
end loop;
--the levenshtein(edit) distance is found in cell d[originLength, newLength]
return levenshtein_2d_grid (originLength) (newLength);
exception
when others then
return 0;
end;
/