@Test public void testPlainDamerauSpeedTest() { System.out.println("Plain Damerau speed test"); for (int i = 0; i < 100; i++) { EditDistance.damerau(H1N1, H1N5); } }
public static int damerau(String x, String y) { if (x.length() < y.length()) { String swap = x; x = y; y = swap; } int[][] d = new int[3][y.length() + 1]; for (int j = 0; j <= y.length(); j++) { d[1][j] = j; } for (int i = 1; i <= x.length(); i++) { d[2][0] = i; for (int j = 1; j <= y.length(); j++) { int cost = x.charAt(i-1) == y.charAt(j-1) ? 0 : 1; d[2][j] = MathEx.min( d[1][j] + 1, d[2][j-1] + 1, d[1][j-1] + cost); if (i > 1 && j > 1) { if (x.charAt(i-1) == y.charAt(j-2) && x.charAt(i-2) == y.charAt(j-1)) d[2][j] = Math.min(d[2][j], d[0][j-2] + cost); } } int[] swap = d[0]; d[0] = d[1]; d[1] = d[2]; d[2] = swap; } return d[1][y.length()]; }
EditDistance implements Metric<String> { public static int damerau(String x, String y) { if (x.length() < y.length()) { String swap = x; x = y; y = swap; } int[][] d = new int[3][y.length() + 1]; for (int j = 0; j <= y.length(); j++) { d[1][j] = j; } for (int i = 1; i <= x.length(); i++) { d[2][0] = i; for (int j = 1; j <= y.length(); j++) { int cost = x.charAt(i-1) == y.charAt(j-1) ? 0 : 1; d[2][j] = MathEx.min( d[1][j] + 1, d[2][j-1] + 1, d[1][j-1] + cost); if (i > 1 && j > 1) { if (x.charAt(i-1) == y.charAt(j-2) && x.charAt(i-2) == y.charAt(j-1)) d[2][j] = Math.min(d[2][j], d[0][j-2] + cost); } } int[] swap = d[0]; d[0] = d[1]; d[1] = d[2]; d[2] = swap; } return d[1][y.length()]; } }
EditDistance implements Metric<String> { public static int damerau(String x, String y) { if (x.length() < y.length()) { String swap = x; x = y; y = swap; } int[][] d = new int[3][y.length() + 1]; for (int j = 0; j <= y.length(); j++) { d[1][j] = j; } for (int i = 1; i <= x.length(); i++) { d[2][0] = i; for (int j = 1; j <= y.length(); j++) { int cost = x.charAt(i-1) == y.charAt(j-1) ? 0 : 1; d[2][j] = MathEx.min( d[1][j] + 1, d[2][j-1] + 1, d[1][j-1] + cost); if (i > 1 && j > 1) { if (x.charAt(i-1) == y.charAt(j-2) && x.charAt(i-2) == y.charAt(j-1)) d[2][j] = Math.min(d[2][j], d[0][j-2] + cost); } } int[] swap = d[0]; d[0] = d[1]; d[1] = d[2]; d[2] = swap; } return d[1][y.length()]; } EditDistance();  EditDistance(boolean damerau);  EditDistance(int maxStringLength);  EditDistance(int maxStringLength, boolean damerau);  EditDistance(int[][] weight);  EditDistance(int[][] weight, double radius); }
EditDistance implements Metric<String> { public static int damerau(String x, String y) { if (x.length() < y.length()) { String swap = x; x = y; y = swap; } int[][] d = new int[3][y.length() + 1]; for (int j = 0; j <= y.length(); j++) { d[1][j] = j; } for (int i = 1; i <= x.length(); i++) { d[2][0] = i; for (int j = 1; j <= y.length(); j++) { int cost = x.charAt(i-1) == y.charAt(j-1) ? 0 : 1; d[2][j] = MathEx.min( d[1][j] + 1, d[2][j-1] + 1, d[1][j-1] + cost); if (i > 1 && j > 1) { if (x.charAt(i-1) == y.charAt(j-2) && x.charAt(i-2) == y.charAt(j-1)) d[2][j] = Math.min(d[2][j], d[0][j-2] + cost); } } int[] swap = d[0]; d[0] = d[1]; d[1] = d[2]; d[2] = swap; } return d[1][y.length()]; } EditDistance();  EditDistance(boolean damerau);  EditDistance(int maxStringLength);  EditDistance(int maxStringLength, boolean damerau);  EditDistance(int[][] weight);  EditDistance(int[][] weight, double radius); @Override String toString(); @Override double d(String x, String y); double d(char[] x, char[] y); static int levenshtein(String x, String y); static int levenshtein(char[] x, char[] y); static int damerau(String x, String y); static int damerau(char[] x, char[] y); }
EditDistance implements Metric<String> { public static int damerau(String x, String y) { if (x.length() < y.length()) { String swap = x; x = y; y = swap; } int[][] d = new int[3][y.length() + 1]; for (int j = 0; j <= y.length(); j++) { d[1][j] = j; } for (int i = 1; i <= x.length(); i++) { d[2][0] = i; for (int j = 1; j <= y.length(); j++) { int cost = x.charAt(i-1) == y.charAt(j-1) ? 0 : 1; d[2][j] = MathEx.min( d[1][j] + 1, d[2][j-1] + 1, d[1][j-1] + cost); if (i > 1 && j > 1) { if (x.charAt(i-1) == y.charAt(j-2) && x.charAt(i-2) == y.charAt(j-1)) d[2][j] = Math.min(d[2][j], d[0][j-2] + cost); } } int[] swap = d[0]; d[0] = d[1]; d[1] = d[2]; d[2] = swap; } return d[1][y.length()]; } EditDistance();  EditDistance(boolean damerau);  EditDistance(int maxStringLength);  EditDistance(int maxStringLength, boolean damerau);  EditDistance(int[][] weight);  EditDistance(int[][] weight, double radius); @Override String toString(); @Override double d(String x, String y); double d(char[] x, char[] y); static int levenshtein(String x, String y); static int levenshtein(char[] x, char[] y); static int damerau(String x, String y); static int damerau(char[] x, char[] y);  }