Changeset 4364 for trunk/gli/src/org/greenstone/gatherer/util/MED.java
- Timestamp:
- 2003-05-27T15:40:47+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/util/MED.java
r4293 r4364 57 57 */ 58 58 public class MED { 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 59 //**************************** 60 // Get minimum of three values 61 //**************************** 62 static private int Minimum (int a, int b, int c) { 63 int mi = a; 64 if (b < mi) { 65 mi = b; 66 } 67 if (c < mi) { 68 mi = c; 69 } 70 return mi; 71 } 72 //***************************** 73 // Compute Levenshtein distance 74 //***************************** 75 static public int LD (String s, String t) { 76 int d[][]; // matrix 77 int n; // length of s 78 int m; // length of t 79 int i; // iterates through s 80 int j; // iterates through t 81 char s_i; // ith character of s 82 char t_j; // jth character of t 83 int cost; // cost 84 // Step 1 85 // Set n to be the length of s. 86 // Set m to be the length of t. 87 // If n = 0, return m and exit. 88 // If m = 0, return n and exit. 89 // Construct a matrix containing 0..m rows and 0..n columns. 90 n = s.length (); 91 m = t.length (); 92 if (n == 0) { 93 return m; 94 } 95 if (m == 0) { 96 return n; 97 } 98 d = new int[n+1][m+1]; 99 // Step 2 100 // Initialize the first row to 0..n. 101 // Initialize the first column to 0..m. 102 for (i = 0; i <= n; i++) { 103 d[i][0] = i; 104 } 105 for (j = 0; j <= m; j++) { 106 d[0][j] = j; 107 } 108 // Step 3 109 // Examine each character of s (i from 1 to n). 110 for (i = 1; i <= n; i++) { 111 s_i = s.charAt (i - 1); 112 112 // Step 4 113 113 // Examine each character of t (j from 1 to m). 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 114 for (j = 1; j <= m; j++) { 115 t_j = t.charAt (j - 1); 116 // Step 5 117 // If s[i] equals t[j], the cost is 0. 118 // If s[i] doesn't equal t[j], the cost is 1. 119 if (s_i == t_j) { 120 cost = 0; 121 } 122 else { 123 cost = 1; 124 } 125 // Step 6 126 // Set cell d[i,j] of the matrix equal to the minimum of: 127 // a. The cell immediately above plus 1: d[i-1,j] + 1. 128 // b. The cell immediately to the left plus 1: d[i,j-1] + 1. 129 // c. The cell diagonally above and to the left plus the cost: d[i-1,j-1] + cost. 130 d[i][j] = Minimum (d[i-1][j]+1, d[i][j-1]+1, d[i-1][j-1] + cost); 131 } 132 } 133 // Step 7 134 // After the iteration steps (3, 4, 5, 6) are complete, the distance is found in cell d[n,m]. 135 int result = d[n][m]; 136 d = null; 137 return result; 138 } 139 139 }
Note:
See TracChangeset
for help on using the changeset viewer.