Java 比较两个字符串的类似度算法(Levenshtein Distance)

Levenshtein 距离,又称编辑距离

指的是两个字符串之间,由一个转换成另外一个所需的最少编辑操做次数。java

许可的编辑操做包括将一个字符替换成另外一个字符,插入一个字符,删除一个字符。算法

编辑距离的算法是首先由俄国科学家Levenshtein提出的,故又叫Levenshtein Distance。spa

算法实现原理图解:code

a.首先是有两个字符串,这里写一个简单的 abc 和 abe

b.将字符串想象成下面的结构。

A 处 是一个标记,为了方便讲解,不是这个表的内容。three

  abc a b c
abe 0 1 2 3
a 1 A处    
b 2      
e 3      

c.来计算 A 处 出得值

它的值取决于:左边的 一、上边的 一、左上角的 0。字符串

按照 Levenshtein distance 的意思:get

上面的值加 1 ,获得 1+1=2 ,it

左面的值加 1 ,获得 1+1=2 ,io

左上角的值根据字符是否相同,相同加 0 ,不一样加 1 。A 处因为是两个 a 相同,左上角的值加 0 ,获得 0+0=0 。table

而后从咱们上面计算出来的 2,2,0 三个值中选取最小值,因此 A 处的值为 0 。

d.因而表成为下面的样子

  abc a b c
abe 0 1 2 3
a 1 0    
b 2 B处    
e 3      

在 B 处 会一样获得三个值,左边计算后为 3 ,上边计算后为 1 ,在 B 处 因为对应的字符为 a、b ,不相等,因此左上角应该在当前值的基础上加 1 ,这样获得 1+1=2 ,在(3,1,2)中选出最小的为 B 处的值。

e.因而表就更新了

 

  abc a b c
abe 0 1 2 3
a 1 0    
b 2 1    
e 3 C处    

C 处 计算后:上面的值为 2 ,左边的值为 4 ,左上角的:a 和 e 不相同,因此加 1 ,即 2+1 ,左上角的为 3 。

在(2,4,3)中取最小的为 C 处的值。

f.因而依次推获得

    a b c
  0 1 2 3
a 1 A处 0 D处 1 G处 2
b 2 B处 1 E处 0 H处 1
e 3 C处 2 F处 1 I处 1

 

I 处: 表示 abc 和 abe 有1个须要编辑的操做( c 替换成 e )。这个是须要计算出来的。

同时,也得到一些额外的信息:

A处: 表示a      和a       须要有0个操做。字符串同样

B处: 表示ab    和a       须要有1个操做。

C处: 表示abe  和a       须要有2个操做。

D处: 表示a      和ab     须要有1个操做。

E处: 表示ab    和ab     须要有0个操做。字符串同样

F处: 表示abe  和ab     须要有1个操做。

G处: 表示a      和abc   须要有2个操做。

H处: 表示ab    和abc   须要有1个操做。

I处: 表示abe   和abc    须要有1个操做。

g.计算类似度

先取两个字符串长度的最大值 maxLen,用 1-(须要操做数 除 maxLen),获得类似度。

例如 abc 和  abe  一个操做,长度为 3 ,因此类似度为 1-1/3=0.666 。

 

public class CompareStrSimUtil {
	private static int compare(String str, String target, boolean isIgnore) {
		int d[][]; // 矩阵
		int n = str.length();
		int m = target.length();
		int i; // 遍历str的
		int j; // 遍历target的
		char ch1; // str的
		char ch2; // target的
		int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
		if (n == 0) {
			return m;
		}
		if (m == 0) {
			return n;
		}
		d = new int[n + 1][m + 1];
		for (i = 0; i <= n; i++) { // 初始化第一列
			d[i][0] = i;
		}

		for (j = 0; j <= m; j++) { // 初始化第一行
			d[0][j] = j;
		}

		for (i = 1; i <= n; i++) { // 遍历str
			ch1 = str.charAt(i - 1);
			// 去匹配target
			for (j = 1; j <= m; j++) {
				ch2 = target.charAt(j - 1);
				if (isIgnore) {
					if (ch1 == ch2 || ch1 == ch2 + 32 || ch1 + 32 == ch2) {
						temp = 0;
					} else {
						temp = 1;
					}
				} else {
					if (ch1 == ch2) {
						temp = 0;
					} else {
						temp = 1;
					}
				}

				// 左边+1,上边+1, 左上角+temp取最小
				d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
			}
		}
		return d[n][m];
	}

	private static int min(int one, int two, int three) {
		return (one = one < two ? one : two) < three ? one : three;
	}

	public static float getSimilarityRatio(String str, String target, boolean isIgnore) {
		float ret = 0;
		if (Math.max(str.length(), target.length()) == 0) {
			ret = 1;
		} else {
			ret = 1 - (float) compare(str, target, isIgnore) / Math.max(str.length(), target.length());
		}
		return ret;
	}

	public static void main(String[] args) {
		CompareStrSimUtil lt = new CompareStrSimUtil();
		String str = "ab";
		String target = "ABC";
		System.out.println("similarityRatio=" + lt.getSimilarityRatio(str, target, true));
	}
}