2019-03-02 15:10:46 +01:00
|
|
|
/*
|
|
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
|
|
* To change this template file, choose Tools | Templates
|
|
|
|
* and open the template in the editor.
|
|
|
|
*/
|
|
|
|
package FunctionLayer;
|
|
|
|
|
2019-03-24 23:04:19 +01:00
|
|
|
import com.google.common.collect.MapMaker;
|
2019-03-16 23:02:52 +01:00
|
|
|
import java.util.Map;
|
2019-03-20 22:38:28 +01:00
|
|
|
import java.util.Map.Entry;
|
2019-03-03 13:17:07 +01:00
|
|
|
import java.util.concurrent.Callable;
|
2019-03-24 23:04:19 +01:00
|
|
|
import java.util.concurrent.ConcurrentMap;
|
2019-03-03 13:17:07 +01:00
|
|
|
|
2019-03-02 15:10:46 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author install1
|
|
|
|
*/
|
2019-03-24 23:04:19 +01:00
|
|
|
public class LevenshteinDistance implements Callable<ConcurrentMap<String, Integer>> {
|
2019-03-03 13:17:07 +01:00
|
|
|
|
|
|
|
private CharSequence lhs;
|
|
|
|
private CharSequence rhs;
|
2019-03-24 23:04:19 +01:00
|
|
|
private ConcurrentMap<String, Integer> distanceEntry = new MapMaker().concurrencyLevel(2).makeMap();
|
2019-03-02 15:10:46 +01:00
|
|
|
|
|
|
|
private static int minimum(int a, int b, int c) {
|
|
|
|
return Math.min(Math.min(a, b), c);
|
|
|
|
}
|
|
|
|
|
2019-03-03 13:17:07 +01:00
|
|
|
public LevenshteinDistance(CharSequence lhs, CharSequence rhs) {
|
|
|
|
this.lhs = lhs;
|
|
|
|
this.rhs = rhs;
|
|
|
|
}
|
|
|
|
|
2019-03-24 23:04:19 +01:00
|
|
|
public double computeLevenshteinDistance() {
|
2019-03-03 13:17:07 +01:00
|
|
|
int[][] distance = new int[lhs.length() + 1][rhs.length() + 1];
|
2019-03-02 15:10:46 +01:00
|
|
|
for (int i = 0; i <= lhs.length(); i++) {
|
|
|
|
distance[i][0] = i;
|
|
|
|
}
|
|
|
|
for (int j = 1; j <= rhs.length(); j++) {
|
|
|
|
distance[0][j] = j;
|
|
|
|
}
|
|
|
|
for (int i = 1; i <= lhs.length(); i++) {
|
|
|
|
for (int j = 1; j <= rhs.length(); j++) {
|
|
|
|
distance[i][j] = minimum(
|
|
|
|
distance[i - 1][j] + 1,
|
|
|
|
distance[i][j - 1] + 1,
|
|
|
|
distance[i - 1][j - 1] + ((lhs.charAt(i - 1) == rhs.charAt(j - 1)) ? 0 : 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return distance[lhs.length()][rhs.length()];
|
|
|
|
}
|
2019-03-03 13:17:07 +01:00
|
|
|
|
|
|
|
@Override
|
2019-03-24 23:04:19 +01:00
|
|
|
public ConcurrentMap<String, Integer> call() {
|
|
|
|
int[][] distance = new int[lhs.length() + 1][rhs.length() + 1];
|
|
|
|
for (int i = 0; i <= lhs.length(); i++) {
|
|
|
|
distance[i][0] = i;
|
|
|
|
}
|
|
|
|
for (int j = 1; j <= rhs.length(); j++) {
|
|
|
|
distance[0][j] = j;
|
|
|
|
}
|
|
|
|
for (int i = 1; i <= lhs.length(); i++) {
|
2019-03-03 13:17:07 +01:00
|
|
|
for (int j = 1; j <= rhs.length(); j++) {
|
2019-03-24 23:04:19 +01:00
|
|
|
distance[i][j] = minimum(
|
|
|
|
distance[i - 1][j] + 1,
|
|
|
|
distance[i][j - 1] + 1,
|
|
|
|
distance[i - 1][j - 1] + ((lhs.charAt(i - 1) == rhs.charAt(j - 1)) ? 0 : 1));
|
2019-03-03 13:17:07 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-24 23:04:19 +01:00
|
|
|
distanceEntry.put(lhs.toString(), distance[lhs.length()][rhs.length()]);
|
2019-03-16 23:02:52 +01:00
|
|
|
return distanceEntry;
|
2019-03-03 13:17:07 +01:00
|
|
|
}
|
2019-03-02 15:10:46 +01:00
|
|
|
}
|