-- 创建临时表,用于存储矩阵(过程值) CREATE GLOBAL TEMPORARY TABLE similarity_matrix ( id1 NUMBER, id2 NUMBER, value NUMBER ) ON COMMIT DELETE ROWS;
-- 创建函数 计算字符串相似度
CREATE OR REPLACE FUNCTION func_get_similarity_ratio(str VARCHAR2, target VARCHAR2) RETURN NUMBER DETERMINISTIC IS -- 使用自治事务 pragma autonomous_transaction; n NUMBER := LENGTH(str); m NUMBER := LENGTH(target); i NUMBER; j NUMBER; temp NUMBER; d_val NUMBER; ch1 VARCHAR2(1); ch2 VARCHAR2(1); BEGIN IF n = 0 OR m = 0 THEN RETURN 0; END IF;
-- 初始化矩阵的第一列 FOR i IN 0..n LOOP INSERT INTO similarity_matrix(id1, id2, value) VALUES (i, 0, i); END LOOP;
-- 初始化矩阵的第一行 FOR j IN 0..m LOOP INSERT INTO similarity_matrix(id1, id2, value) VALUES (0, j, j); END LOOP;
-- 动态规划填充矩阵 FOR i IN 1..n LOOP FOR j IN 1..m LOOP ch1 := SUBSTR(str, i, 1); ch2 := SUBSTR(target, j, 1);
IF ch1 = ch2 THEN temp := 0; ELSE temp := 1; END IF;
-- 获取三者中的最小值 SELECT MIN(value) INTO d_val FROM (SELECT value + 1 as value FROM similarity_matrix WHERE id1 = i - 1 AND id2 = j union SELECT value + 1 as value FROM similarity_matrix WHERE id1 = i AND id2 = j - 1 union SELECT value + temp as value FROM similarity_matrix WHERE id1 = i - 1 AND id2 = j - 1);
-- 更新当前格子的值 INSERT INTO similarity_matrix(id1, id2, value) VALUES (i, j, d_val); END LOOP; END LOOP;
SELECT value into d_val FROM similarity_matrix WHERE id1 = n AND id2 = m; commit; -- 计算并返回相似度 RETURN (1 - round(d_val / GREATEST(n, m), 4)) * 100; END func_get_similarity_ratio;
-- 调用函数测试 select func_get_similarity_ratio('12345a', '12345A') as ratio from dual;
create FUNCTION func_get_similarity_ratio(str VARCHAR2, target VARCHAR2) RETURN NUMBER DETERMINISTIC IS PRAGMA AUTONOMOUS_TRANSACTION; n NUMBER := LENGTH(str); m NUMBER := LENGTH(target); i NUMBER; j NUMBER; temp NUMBER; d_val NUMBER; ch1 VARCHAR2(1 CHAR); ch2 VARCHAR2(1 CHAR); BEGIN IF n = 0 OR m = 0 THEN RETURN 0; END IF;
-- 初始化矩阵的第一列 FOR i IN 0..n LOOP INSERT INTO similarity_matrix(id1, id2, value) VALUES (i, 0, i); END LOOP;
-- 初始化矩阵的第一行 FOR j IN 0..m LOOP INSERT INTO similarity_matrix(id1, id2, value) VALUES (0, j, j); END LOOP;
-- 动态规划填充矩阵 FOR i IN 1..n LOOP FOR j IN 1..m LOOP ch1 := SUBSTR(str, i, 1); ch2 := SUBSTR(target, j, 1);
IF ch1 = ch2 THEN temp := 0; ELSE temp := 1; END IF;
-- 定义变量存储左、上、左上角的值 SELECT MIN(value) INTO d_val FROM (SELECT value + 1 as value FROM similarity_matrix WHERE id1 = i - 1 AND id2 = j union SELECT value + 1 as value FROM similarity_matrix WHERE id1 = i AND id2 = j - 1 union SELECT value + temp as value FROM similarity_matrix WHERE id1 = i - 1 AND id2 = j - 1);
-- 更新当前格子的值 INSERT INTO similarity_matrix(id1, id2, value) VALUES (i, j, d_val); END LOOP; END LOOP;
SELECT value INTO d_val FROM similarity_matrix WHERE id1 = n AND id2 = m;