2 Jazzy - a Java library for Spell Checking
\r
3 Copyright (C) 2001 Mindaugas Idzelis
\r
4 Full text of license can be found in LICENSE.txt
\r
6 This library is free software; you can redistribute it and/or
\r
7 modify it under the terms of the GNU Lesser General Public
\r
8 License as published by the Free Software Foundation; either
\r
9 version 2.1 of the License, or (at your option) any later version.
\r
11 This library is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
14 Lesser General Public License for more details.
\r
16 You should have received a copy of the GNU Lesser General Public
\r
17 License along with this library; if not, write to the Free Software
\r
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
\r
20 package com.swabunga.spell.engine;
\r
22 import java.io.BufferedReader;
\r
23 import java.io.InputStreamReader;
\r
26 * This class is based on Levenshtein Distance algorithms, and it calculates how similar two words are.
\r
27 * If the words are identical, then the distance is 0. The more that the words have in common, the lower the distance value.
\r
28 * The distance value is based on how many operations it takes to get from one word to the other. Possible operations are
\r
29 * swapping characters, adding a character, deleting a character, and substituting a character.
\r
30 * The resulting distance is the sum of these operations weighted by their cost, which can be set in the Configuration object.
\r
31 * When there are multiple ways to convert one word into the other, the lowest cost distance is returned.
\r
33 * Another way to think about this: what are the cheapest operations that would have to be done on the "original" word to end up
\r
34 * with the "similar" word? Each operation has a cost, and these are added up to get the distance.
\r
37 * @see com.swabunga.spell.engine.Configuration#COST_REMOVE_CHAR
\r
38 * @see com.swabunga.spell.engine.Configuration#COST_INSERT_CHAR
\r
39 * @see com.swabunga.spell.engine.Configuration#COST_SUBST_CHARS
\r
40 * @see com.swabunga.spell.engine.Configuration#COST_SWAP_CHARS
\r
44 public class EditDistance {
\r
47 * Fetches the spell engine configuration properties.
\r
49 public static Configuration config = Configuration.getConfiguration();
\r
52 * get the weights for each possible operation
\r
54 static final int costOfDeletingSourceCharacter = config.getInteger(Configuration.COST_REMOVE_CHAR);
\r
55 static final int costOfInsertingSourceCharacter = config.getInteger(Configuration.COST_INSERT_CHAR);
\r
56 static final int costOfSubstitutingLetters = config.getInteger(Configuration.COST_SUBST_CHARS);
\r
57 static final int costOfSwappingLetters = config.getInteger(Configuration.COST_SWAP_CHARS);
\r
58 static final int costOfChangingCase = config.getInteger(Configuration.COST_CHANGE_CASE);
\r
61 * Evaluates the distance between two words.
\r
63 * @param word One word to evaluates
\r
64 * @param similar The other word to evaluates
\r
65 * @return a number representing how easy or complex it is to transform on
\r
66 * word into a similar one.
\r
68 public static final int getDistance(String word, String similar) {
\r
69 return getDistance(word,similar,null);
\r
73 * Evaluates the distance between two words.
\r
75 * @param word One word to evaluates
\r
76 * @param similar The other word to evaluates
\r
77 * @return a number representing how easy or complex it is to transform on
\r
78 * word into a similar one.
\r
80 public static final int getDistance(String word, String similar, int[][] matrix) {
\r
81 /* JMH Again, there is no need to have a global class matrix variable
\r
82 * in this class. I have removed it and made the getDistance static final
\r
83 * DMV: I refactored this method to make it more efficient, more readable, and simpler.
\r
84 * I also fixed a bug with how the distance was being calculated. You could get wrong
\r
85 * distances if you compared ("abc" to "ab") depending on what you had setup your
\r
86 * COST_REMOVE_CHAR and EDIT_INSERTION_COST values to - that is now fixed.
\r
87 * WRS: I added a distance for case comparison, so a misspelling of "i" would be closer to "I" than
\r
91 //Allocate memory outside of the loops.
\r
97 int costOfInsertion;
\r
98 int costOfCaseChange;
\r
101 char sourceChar = 0;
\r
102 char otherChar = 0;
\r
104 int a_size = word.length() + 1;
\r
105 int b_size = similar.length() + 1;
\r
108 //Only allocate new memory if we need a bigger matrix.
\r
109 if (matrix == null || matrix.length < a_size || matrix[0].length < b_size)
\r
110 matrix = new int[a_size][b_size];
\r
114 for (i = 1; i != a_size; ++i)
\r
115 matrix[i][0] = matrix[i - 1][0] + costOfInsertingSourceCharacter; //initialize the first column
\r
117 for (j = 1; j != b_size; ++j)
\r
118 matrix[0][j] = matrix[0][j - 1] + costOfDeletingSourceCharacter; //initalize the first row
\r
120 for (i = 1; i != a_size; ++i) {
\r
121 sourceChar = word.charAt(i-1);
\r
122 for (j = 1; j != b_size; ++j) {
\r
124 otherChar = similar.charAt(j-1);
\r
125 if (sourceChar == otherChar) {
\r
126 matrix[i][j] = matrix[i - 1][j - 1]; //no change required, so just carry the current cost up
\r
130 costOfSubst = costOfSubstitutingLetters + matrix[i - 1][j - 1];
\r
131 //if needed, add up the cost of doing a swap
\r
132 costOfSwap = Integer.MAX_VALUE;
\r
134 isSwap = (i != 1) && (j != 1) && sourceChar == similar.charAt(j - 2) && word.charAt(i - 2) == otherChar;
\r
136 costOfSwap = costOfSwappingLetters + matrix[i - 2][j - 2];
\r
138 costOfDelete = costOfDeletingSourceCharacter + matrix[i][j - 1];
\r
139 costOfInsertion = costOfInsertingSourceCharacter + matrix[i - 1][j];
\r
141 costOfCaseChange = Integer.MAX_VALUE;
\r
143 if (equalIgnoreCase(sourceChar, otherChar))
\r
144 costOfCaseChange = costOfChangingCase + matrix[i - 1][j - 1];
\r
146 matrix[i][j] = minimum(costOfSubst, costOfSwap, costOfDelete, costOfInsertion, costOfCaseChange);
\r
150 return matrix[a_size - 1][b_size - 1];
\r
154 * checks to see if the two charactors are equal ignoring case.
\r
159 private static boolean equalIgnoreCase(char ch1, char ch2) {
\r
166 return (Character.toLowerCase(ch1) == Character.toLowerCase(ch2));
\r
171 * For debugging, this creates a string that represents the matrix. To read the matrix, look at any square. That is the cost to get from
\r
172 * the partial letters along the top to the partial letters along the side.
\r
173 * @param src - the source string that the matrix columns are based on
\r
174 * @param dest - the dest string that the matrix rows are based on
\r
175 * @param matrix - a two dimensional array of costs (distances)
\r
178 @SuppressWarnings("unused")
\r
179 static private String dumpMatrix(String src, String dest, int matrix[][]) {
\r
180 StringBuffer s = new StringBuffer("");
\r
182 int cols = matrix.length -1;
\r
183 int rows = matrix[0].length -1;
\r
185 for (int i = 0; i < cols + 1; i++) {
\r
186 for (int j = 0; j < rows + 1; j++) {
\r
187 if (i == 0 && j == 0) {
\r
194 s.append(dest.charAt(j - 1));
\r
198 s.append(src.charAt(i - 1));
\r
201 String num = Integer.toString(matrix[i - 1][j - 1]);
\r
202 int padding = 4 - num.length();
\r
204 for (int k = 0; k < padding; k++)
\r
210 return s.toString();
\r
215 static private int minimum(int a, int b, int c, int d, int e) {
\r
230 * For testing edit distances
\r
231 * @param args an array of two strings we want to evaluate their distances.
\r
232 * @throws java.lang.Exception when problems occurs during reading args.
\r
234 public static void main(String[] args) throws Exception {
\r
235 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
\r
236 int[][] matrix = new int[0][0];
\r
239 String input1 = stdin.readLine();
\r
240 if (input1 == null || input1.length() == 0)
\r
243 String input2 = stdin.readLine();
\r
244 if (input2 == null || input2.length() == 0)
\r
247 System.out.println(EditDistance.getDistance(input1, input2,matrix));
\r
249 System.out.println("done");
\r