1 | /**
|
---|
2 | *#########################################################################
|
---|
3 | *
|
---|
4 | * A component of the Gatherer application, part of the Greenstone digital
|
---|
5 | * library suite from the New Zealand Digital Library Project at the
|
---|
6 | * University of Waikato, New Zealand.
|
---|
7 | *
|
---|
8 | * <BR><BR>
|
---|
9 | *
|
---|
10 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
11 | *
|
---|
12 | * <BR><BR>
|
---|
13 | *
|
---|
14 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
15 | *
|
---|
16 | * <BR><BR>
|
---|
17 | *
|
---|
18 | * This program is free software; you can redistribute it and/or modify
|
---|
19 | * it under the terms of the GNU General Public License as published by
|
---|
20 | * the Free Software Foundation; either version 2 of the License, or
|
---|
21 | * (at your option) any later version.
|
---|
22 | *
|
---|
23 | * <BR><BR>
|
---|
24 | *
|
---|
25 | * This program is distributed in the hope that it will be useful,
|
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
28 | * GNU General Public License for more details.
|
---|
29 | *
|
---|
30 | * <BR><BR>
|
---|
31 | *
|
---|
32 | * You should have received a copy of the GNU General Public License
|
---|
33 | * along with this program; if not, write to the Free Software
|
---|
34 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
35 | *########################################################################
|
---|
36 | */
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | /* GPL_HEADER */
|
---|
44 | package org.greenstone.gatherer.util;
|
---|
45 | /**************************************************************************************
|
---|
46 | * Title: Gatherer
|
---|
47 | * Description: The Gatherer: a tool for gathering and enriching a digital collection.
|
---|
48 | * Company: The University of Waikato
|
---|
49 | * Written: 28/08/02
|
---|
50 | * Revised:
|
---|
51 | * @author John Thompson, 9826509
|
---|
52 | * @author Michael Gilleland, Merriam Park Software
|
---|
53 | * @version 2.3
|
---|
54 | **************************************************************************************/
|
---|
55 | /** Determines the MED between two metadata element names.<BR>
|
---|
56 | * Adapted from code by Michael Gilleland, Merriam Park Software, as detailed in a short essay called "Levenshtein Distance, in Three Flavors" available at http://www.merriampark.com/ld.htm.
|
---|
57 | */
|
---|
58 | public class MED {
|
---|
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 | // Step 4
|
---|
113 | // Examine each character of t (j from 1 to m).
|
---|
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 | }
|
---|