sql levenshtein_distance

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql levenshtein_distance相关的知识,希望对你有一定的参考价值。

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;
/

以上是关于sql levenshtein_distance的主要内容,如果未能解决你的问题,请参考以下文章

sql 2008和sql 2012的区别

sql [sql技巧]一些sql技巧#sql

sql 2008 r2 在sql 2008上兼容么

SQL基础

有大神知道,sql server 中如何批量执行sql语句吗?

pl sql developer怎么执行sql