【Similarity calculation】Jaro Winkler distance

based on
http://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance



import java.util.Arrays;

public class JaroDistance {

public static double jaroDistance(String source, String target) {
int slen = source.length();
int tlen = target.length();
int i, j;

// the match window
int matchWindow = Math.max(slen, tlen)/2 - 1;

//the number of matching characters
int m = 0;

//half the number of transposition
int t = 0;

boolean[] smatched = new boolean[slen];
boolean[] tmatched = new boolean[tlen];

Arrays.fill(smatched, false);
Arrays.fill(tmatched, false);


if (slen == 0) {
return tlen == 0 ? 1.0 : 0.0;
}

for (i = 0; i < slen; ++i) {
int start = Math.max(0, i-matchWindow);
int end = Math.min(i+matchWindow, tlen);
for (j = start; j < end; j++) {
if (tmatched[j]) continue;
if (source.charAt(i) != target.charAt(j))
continue;
smatched[i] = true;
tmatched[j] = true;
++m;
break;
}
}

if (m == 0) return 0.0;

j = 0;
for (i = 0; i < slen; ++i) {
if (!smatched[i]) continue;
while (!tmatched[j]) ++j;
if (source.charAt(i) != target.charAt(j))
++t;
++j;
}
t = t / 2;
// System.out.println(m + " " + t);

return ((double)m/slen + (double)m/tlen + (double)(m-t)/m) / 3.0;
}

public static double jaroWinklerDistance(String source, String target) {
int max = Math.min(4, Math.min(source.length(), target.length()));
int len = 0;
for (int i = 0; i < max; ++i) {
if (source.charAt(len) == target.charAt(len))
++len;
}

double jaro = jaroDistance(source, target);
return jaro + 0.1 * len * (1.0 - jaro);
}
public static void main(String[] args) {
String source = "MARTHA";
String target = "MARHTA";
System.out.println("Dj = " + jaroDistance(source, target));
System.out.println("Dw = " + jaroWinklerDistance(source, target));
System.out.println();

source = "DICKSONX";
target = "DIXON";
System.out.println("Dj = " + jaroDistance(source, target));
System.out.println("Dw = " + jaroWinklerDistance(source, target));
System.out.println();
}

}




Output:
Dj = 0.9444444444444445
Dw = 0.9611111111111111

Dj = 0.7666666666666666
Dw = 0.8133333333333332

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值