Return-Path: X-Original-To: apmail-commons-notifications-archive@minotaur.apache.org Delivered-To: apmail-commons-notifications-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2851F172DD for ; Sun, 26 Apr 2015 10:18:34 +0000 (UTC) Received: (qmail 52711 invoked by uid 500); 26 Apr 2015 10:18:34 -0000 Delivered-To: apmail-commons-notifications-archive@commons.apache.org Received: (qmail 52673 invoked by uid 500); 26 Apr 2015 10:18:34 -0000 Mailing-List: contact notifications-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list notifications@commons.apache.org Received: (qmail 52466 invoked by uid 99); 26 Apr 2015 10:18:33 -0000 Received: from eris.apache.org (HELO hades.apache.org) (140.211.11.105) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 26 Apr 2015 10:18:33 +0000 Received: from hades.apache.org (localhost [127.0.0.1]) by hades.apache.org (ASF Mail Server at hades.apache.org) with ESMTP id AF5B6AC1435 for ; Sun, 26 Apr 2015 10:18:33 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r949214 [34/35] - in /websites/production/commons/content/sandbox/commons-text: ./ apidocs/ apidocs/org/apache/commons/text/diff/ apidocs/org/apache/commons/text/diff/class-use/ apidocs/org/apache/commons/text/names/ apidocs/org/apache/comm... Date: Sun, 26 Apr 2015 10:18:28 -0000 To: notifications@commons.apache.org From: britter@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20150426101833.AF5B6AC1435@hades.apache.org> Modified: websites/production/commons/content/sandbox/commons-text/xref/org/apache/commons/text/similarity/LevenshteinDistance.html ============================================================================== --- websites/production/commons/content/sandbox/commons-text/xref/org/apache/commons/text/similarity/LevenshteinDistance.html (original) +++ websites/production/commons/content/sandbox/commons-text/xref/org/apache/commons/text/similarity/LevenshteinDistance.html Sun Apr 26 10:18:25 2015 @@ -38,391 +38,389 @@ 30 * <p> 31 * This code has been adapted from Apache Commons Lang 3.3. 32 * </p> -33 * -34 * @since 1.0 -35 */ -36 public class LevenshteinDistance implements EditDistance<Integer> { -37 -38 /** -39 * Default instance. -40 */ -41 private static final LevenshteinDistance DEFAULT_INSTANCE = new LevenshteinDistance(); -42 -43 /** -44 * Threshold. -45 */ -46 private final Integer threshold; -47 -48 /** -49 * <p> -50 * This returns the default instance that uses a version -51 * of the algorithm that does not use a threshold parameter. -52 * </p> -53 * -54 * @see LevenshteinDistance#getDefaultInstance() -55 */ -56 public LevenshteinDistance() { -57 this(null); -58 } -59 -60 /** -61 * <p> -62 * If the threshold is not null, distance calculations will be limited to a maximum length. -63 * If the threshold is null, the unlimited version of the algorithm will be used. -64 * </p> -65 * -66 * @param threshold -67 * If this is null then distances calculations will not be limited. -68 * This may not be negative. -69 */ -70 public LevenshteinDistance(final Integer threshold) { -71 if (threshold != null && threshold < 0) { -72 throw new IllegalArgumentException("Threshold must not be negative"); -73 } -74 this.threshold = threshold; -75 } -76 -77 /** -78 * <p>Find the Levenshtein distance between two Strings.</p> +33 */ +34 public class LevenshteinDistance implements EditDistance<Integer> { +35 +36 /** +37 * Default instance. +38 */ +39 private static final LevenshteinDistance DEFAULT_INSTANCE = new LevenshteinDistance(); +40 +41 /** +42 * Threshold. +43 */ +44 private final Integer threshold; +45 +46 /** +47 * <p> +48 * This returns the default instance that uses a version +49 * of the algorithm that does not use a threshold parameter. +50 * </p> +51 * +52 * @see LevenshteinDistance#getDefaultInstance() +53 */ +54 public LevenshteinDistance() { +55 this(null); +56 } +57 +58 /** +59 * <p> +60 * If the threshold is not null, distance calculations will be limited to a maximum length. +61 * If the threshold is null, the unlimited version of the algorithm will be used. +62 * </p> +63 * +64 * @param threshold +65 * If this is null then distances calculations will not be limited. +66 * This may not be negative. +67 */ +68 public LevenshteinDistance(final Integer threshold) { +69 if (threshold != null && threshold < 0) { +70 throw new IllegalArgumentException("Threshold must not be negative"); +71 } +72 this.threshold = threshold; +73 } +74 +75 /** +76 * <p>Find the Levenshtein distance between two Strings.</p> +77 * +78 * <p>A higher score indicates a greater distance.</p> 79 * -80 * <p>A higher score indicates a greater distance.</p> -81 * -82 * <p>The previous implementation of the Levenshtein distance algorithm -83 * was from <a href="http://www.merriampark.com/ld.htm">http://www.merriampark.com/ld.htm</a></p> -84 * -85 * <p>Chas Emerick has written an implementation in Java, which avoids an OutOfMemoryError -86 * which can occur when my Java implementation is used with very large strings.<br> -87 * This implementation of the Levenshtein distance algorithm -88 * is from <a href="http://www.merriampark.com/ldjava.htm">http://www.merriampark.com/ldjava.htm</a></p> -89 * -90 * <pre> -91 * distance.apply(null, *) = IllegalArgumentException -92 * distance.apply(*, null) = IllegalArgumentException -93 * distance.apply("","") = 0 -94 * distance.apply("","a") = 1 -95 * distance.apply("aaapppp", "") = 7 -96 * distance.apply("frog", "fog") = 1 -97 * distance.apply("fly", "ant") = 3 -98 * distance.apply("elephant", "hippo") = 7 -99 * distance.apply("hippo", "elephant") = 7 -100 * distance.apply("hippo", "zzzzzzzz") = 8 -101 * distance.apply("hello", "hallo") = 1 -102 * </pre> -103 * -104 * @param left the first string, must not be null -105 * @param right the second string, must not be null -106 * @return result distance, or -1 -107 * @throws IllegalArgumentException if either String input {@code null} -108 */ -109 public Integer apply(CharSequence left, CharSequence right) { -110 if (threshold != null) { -111 return limitedCompare(left, right, threshold); -112 } else { -113 return unlimitedCompare(left, right); -114 } -115 } -116 -117 /** -118 * Gets the default instance. -119 * -120 * @return the default instace -121 */ -122 public static LevenshteinDistance getDefaultInstance() { -123 return DEFAULT_INSTANCE; -124 } -125 -126 /** -127 * Gets the distance threshold. -128 * -129 * @return the distance threshold -130 */ -131 public Integer getThreshold() { -132 return threshold; -133 } -134 -135 /** -136 * Find the Levenshtein distance between two CharSequences if it's less than or -137 * equal to a given threshold. -138 * -139 * <p> -140 * This implementation follows from Algorithms on Strings, Trees and -141 * Sequences by Dan Gusfield and Chas Emerick's implementation of the -142 * Levenshtein distance algorithm from <a -143 * href="http://www.merriampark.com/ld.htm" -144 * >http://www.merriampark.com/ld.htm</a>; -145 * </p> -146 * -147 * <pre> -148 * limitedCompare(null, *, *) = IllegalArgumentException -149 * limitedCompare(*, null, *) = IllegalArgumentException -150 * limitedCompare(*, *, -1) = IllegalArgumentException -151 * limitedCompare("","", 0) = 0 -152 * limitedCompare("aaapppp", "", 8) = 7 -153 * limitedCompare("aaapppp", "", 7) = 7 -154 * limitedCompare("aaapppp", "", 6)) = -1 -155 * limitedCompare("elephant", "hippo", 7) = 7 -156 * limitedCompare("elephant", "hippo", 6) = -1 -157 * limitedCompare("hippo", "elephant", 7) = 7 -158 * limitedCompare("hippo", "elephant", 6) = -1 -159 * </pre> -160 * -161 * @param left the first string, must not be null -162 * @param right the second string, must not be null -163 * @param threshold the target threshold, must not be negative -164 * @return result distance, or -1 -165 */ -166 private static int limitedCompare(CharSequence left, CharSequence right, int threshold) { -167 if (left == null || right == null) { -168 throw new IllegalArgumentException("Strings must not be null"); -169 } -170 if (threshold < 0) { -171 throw new IllegalArgumentException("Threshold must not be negative"); -172 } -173 -174 /* -175 * This implementation only computes the distance if it's less than or -176 * equal to the threshold value, returning -1 if it's greater. The -177 * advantage is performance: unbounded distance is O(nm), but a bound of -178 * k allows us to reduce it to O(km) time by only computing a diagonal -179 * stripe of width 2k + 1 of the cost table. It is also possible to use -180 * this to compute the unbounded Levenshtein distance by starting the -181 * threshold at 1 and doubling each time until the distance is found; -182 * this is O(dm), where d is the distance. -183 * -184 * One subtlety comes from needing to ignore entries on the border of -185 * our stripe eg. p[] = |#|#|#|* d[] = *|#|#|#| We must ignore the entry -186 * to the left of the leftmost member We must ignore the entry above the -187 * rightmost member -188 * -189 * Another subtlety comes from our stripe running off the matrix if the -190 * strings aren't of the same size. Since string s is always swapped to -191 * be the shorter of the two, the stripe will always run off to the -192 * upper right instead of the lower left of the matrix. -193 * -194 * As a concrete example, suppose s is of length 5, t is of length 7, -195 * and our threshold is 1. In this case we're going to walk a stripe of -196 * length 3. The matrix would look like so: -197 * -198 * <pre> -199 * 1 2 3 4 5 -200 * 1 |#|#| | | | -201 * 2 |#|#|#| | | -202 * 3 | |#|#|#| | -203 * 4 | | |#|#|#| -204 * 5 | | | |#|#| -205 * 6 | | | | |#| -206 * 7 | | | | | | -207 * </pre> -208 * -209 * Note how the stripe leads off the table as there is no possible way -210 * to turn a string of length 5 into one of length 7 in edit distance of -211 * 1. -212 * -213 * Additionally, this implementation decreases memory usage by using two -214 * single-dimensional arrays and swapping them back and forth instead of -215 * allocating an entire n by m matrix. This requires a few minor -216 * changes, such as immediately returning when it's detected that the -217 * stripe has run off the matrix and initially filling the arrays with -218 * large values so that entries we don't compute are ignored. -219 * -220 * See Algorithms on Strings, Trees and Sequences by Dan Gusfield for -221 * some discussion. -222 */ -223 -224 int n = left.length(); // length of left -225 int m = right.length(); // length of right -226 -227 // if one string is empty, the edit distance is necessarily the length -228 // of the other -229 if (n == 0) { -230 return m <= threshold ? m : -1; -231 } else if (m == 0) { -232 return n <= threshold ? n : -1; -233 } -234 -235 if (n > m) { -236 // swap the two strings to consume less memory -237 final CharSequence tmp = left; -238 left = right; -239 right = tmp; -240 n = m; -241 m = right.length(); -242 } -243 -244 int[] p = new int[n + 1]; // 'previous' cost array, horizontally -245 int[] d = new int[n + 1]; // cost array, horizontally -246 int[] tempD; // placeholder to assist in swapping p and d -247 -248 // fill in starting table values -249 final int boundary = Math.min(n, threshold) + 1; -250 for (int i = 0; i < boundary; i++) { -251 p[i] = i; -252 } -253 // these fills ensure that the value above the rightmost entry of our -254 // stripe will be ignored in following loop iterations -255 Arrays.fill(p, boundary, p.length, Integer.MAX_VALUE); -256 Arrays.fill(d, Integer.MAX_VALUE); -257 -258 // iterates through t -259 for (int j = 1; j <= m; j++) { -260 final char rightJ = right.charAt(j - 1); // jth character of right -261 d[0] = j; -262 -263 // compute stripe indices, constrain to array size -264 final int min = Math.max(1, j - threshold); -265 final int max = j > Integer.MAX_VALUE - threshold ? n : Math.min( -266 n, j + threshold); -267 -268 // the stripe may lead off of the table if s and t are of different -269 // sizes -270 if (min > max) { -271 return -1; -272 } -273 -274 // ignore entry left of leftmost -275 if (min > 1) { -276 d[min - 1] = Integer.MAX_VALUE; -277 } -278 -279 // iterates through [min, max] in s -280 for (int i = min; i <= max; i++) { -281 if (left.charAt(i - 1) == rightJ) { -282 // diagonally left and up -283 d[i] = p[i - 1]; -284 } else { -285 // 1 + minimum of cell to the left, to the top, diagonally -286 // left and up -287 d[i] = 1 + Math.min(Math.min(d[i - 1], p[i]), p[i - 1]); -288 } -289 } -290 -291 // copy current distance counts to 'previous row' distance counts -292 tempD = p; -293 p = d; -294 d = tempD; -295 } -296 -297 // if p[n] is greater than the threshold, there's no guarantee on it -298 // being the correct -299 // distance -300 if (p[n] <= threshold) { -301 return p[n]; -302 } -303 return -1; -304 } -305 -306 /** -307 * <p>Find the Levenshtein distance between two Strings.</p> +80 * <p>The previous implementation of the Levenshtein distance algorithm +81 * was from <a href="http://www.merriampark.com/ld.htm">http://www.merriampark.com/ld.htm</a></p> +82 * +83 * <p>Chas Emerick has written an implementation in Java, which avoids an OutOfMemoryError +84 * which can occur when my Java implementation is used with very large strings.<br> +85 * This implementation of the Levenshtein distance algorithm +86 * is from <a href="http://www.merriampark.com/ldjava.htm">http://www.merriampark.com/ldjava.htm</a></p> +87 * +88 * <pre> +89 * distance.apply(null, *) = IllegalArgumentException +90 * distance.apply(*, null) = IllegalArgumentException +91 * distance.apply("","") = 0 +92 * distance.apply("","a") = 1 +93 * distance.apply("aaapppp", "") = 7 +94 * distance.apply("frog", "fog") = 1 +95 * distance.apply("fly", "ant") = 3 +96 * distance.apply("elephant", "hippo") = 7 +97 * distance.apply("hippo", "elephant") = 7 +98 * distance.apply("hippo", "zzzzzzzz") = 8 +99 * distance.apply("hello", "hallo") = 1 +100 * </pre> +101 * +102 * @param left the first string, must not be null +103 * @param right the second string, must not be null +104 * @return result distance, or -1 +105 * @throws IllegalArgumentException if either String input {@code null} +106 */ +107 public Integer apply(CharSequence left, CharSequence right) { +108 if (threshold != null) { +109 return limitedCompare(left, right, threshold); +110 } else { +111 return unlimitedCompare(left, right); +112 } +113 } +114 +115 /** +116 * Gets the default instance. +117 * +118 * @return the default instace +119 */ +120 public static LevenshteinDistance getDefaultInstance() { +121 return DEFAULT_INSTANCE; +122 } +123 +124 /** +125 * Gets the distance threshold. +126 * +127 * @return the distance threshold +128 */ +129 public Integer getThreshold() { +130 return threshold; +131 } +132 +133 /** +134 * Find the Levenshtein distance between two CharSequences if it's less than or +135 * equal to a given threshold. +136 * +137 * <p> +138 * This implementation follows from Algorithms on Strings, Trees and +139 * Sequences by Dan Gusfield and Chas Emerick's implementation of the +140 * Levenshtein distance algorithm from <a +141 * href="http://www.merriampark.com/ld.htm" +142 * >http://www.merriampark.com/ld.htm</a>; +143 * </p> +144 * +145 * <pre> +146 * limitedCompare(null, *, *) = IllegalArgumentException +147 * limitedCompare(*, null, *) = IllegalArgumentException +148 * limitedCompare(*, *, -1) = IllegalArgumentException +149 * limitedCompare("","", 0) = 0 +150 * limitedCompare("aaapppp", "", 8) = 7 +151 * limitedCompare("aaapppp", "", 7) = 7 +152 * limitedCompare("aaapppp", "", 6)) = -1 +153 * limitedCompare("elephant", "hippo", 7) = 7 +154 * limitedCompare("elephant", "hippo", 6) = -1 +155 * limitedCompare("hippo", "elephant", 7) = 7 +156 * limitedCompare("hippo", "elephant", 6) = -1 +157 * </pre> +158 * +159 * @param left the first string, must not be null +160 * @param right the second string, must not be null +161 * @param threshold the target threshold, must not be negative +162 * @return result distance, or -1 +163 */ +164 private static int limitedCompare(CharSequence left, CharSequence right, int threshold) { +165 if (left == null || right == null) { +166 throw new IllegalArgumentException("Strings must not be null"); +167 } +168 if (threshold < 0) { +169 throw new IllegalArgumentException("Threshold must not be negative"); +170 } +171 +172 /* +173 * This implementation only computes the distance if it's less than or +174 * equal to the threshold value, returning -1 if it's greater. The +175 * advantage is performance: unbounded distance is O(nm), but a bound of +176 * k allows us to reduce it to O(km) time by only computing a diagonal +177 * stripe of width 2k + 1 of the cost table. It is also possible to use +178 * this to compute the unbounded Levenshtein distance by starting the +179 * threshold at 1 and doubling each time until the distance is found; +180 * this is O(dm), where d is the distance. +181 * +182 * One subtlety comes from needing to ignore entries on the border of +183 * our stripe eg. p[] = |#|#|#|* d[] = *|#|#|#| We must ignore the entry +184 * to the left of the leftmost member We must ignore the entry above the +185 * rightmost member +186 * +187 * Another subtlety comes from our stripe running off the matrix if the +188 * strings aren't of the same size. Since string s is always swapped to +189 * be the shorter of the two, the stripe will always run off to the +190 * upper right instead of the lower left of the matrix. +191 * +192 * As a concrete example, suppose s is of length 5, t is of length 7, +193 * and our threshold is 1. In this case we're going to walk a stripe of +194 * length 3. The matrix would look like so: +195 * +196 * <pre> +197 * 1 2 3 4 5 +198 * 1 |#|#| | | | +199 * 2 |#|#|#| | | +200 * 3 | |#|#|#| | +201 * 4 | | |#|#|#| +202 * 5 | | | |#|#| +203 * 6 | | | | |#| +204 * 7 | | | | | | +205 * </pre> +206 * +207 * Note how the stripe leads off the table as there is no possible way +208 * to turn a string of length 5 into one of length 7 in edit distance of +209 * 1. +210 * +211 * Additionally, this implementation decreases memory usage by using two +212 * single-dimensional arrays and swapping them back and forth instead of +213 * allocating an entire n by m matrix. This requires a few minor +214 * changes, such as immediately returning when it's detected that the +215 * stripe has run off the matrix and initially filling the arrays with +216 * large values so that entries we don't compute are ignored. +217 * +218 * See Algorithms on Strings, Trees and Sequences by Dan Gusfield for +219 * some discussion. +220 */ +221 +222 int n = left.length(); // length of left +223 int m = right.length(); // length of right +224 +225 // if one string is empty, the edit distance is necessarily the length +226 // of the other +227 if (n == 0) { +228 return m <= threshold ? m : -1; +229 } else if (m == 0) { +230 return n <= threshold ? n : -1; +231 } +232 +233 if (n > m) { +234 // swap the two strings to consume less memory +235 final CharSequence tmp = left; +236 left = right; +237 right = tmp; +238 n = m; +239 m = right.length(); +240 } +241 +242 int[] p = new int[n + 1]; // 'previous' cost array, horizontally +243 int[] d = new int[n + 1]; // cost array, horizontally +244 int[] tempD; // placeholder to assist in swapping p and d +245 +246 // fill in starting table values +247 final int boundary = Math.min(n, threshold) + 1; +248 for (int i = 0; i < boundary; i++) { +249 p[i] = i; +250 } +251 // these fills ensure that the value above the rightmost entry of our +252 // stripe will be ignored in following loop iterations +253 Arrays.fill(p, boundary, p.length, Integer.MAX_VALUE); +254 Arrays.fill(d, Integer.MAX_VALUE); +255 +256 // iterates through t +257 for (int j = 1; j <= m; j++) { +258 final char rightJ = right.charAt(j - 1); // jth character of right +259 d[0] = j; +260 +261 // compute stripe indices, constrain to array size +262 final int min = Math.max(1, j - threshold); +263 final int max = j > Integer.MAX_VALUE - threshold ? n : Math.min( +264 n, j + threshold); +265 +266 // the stripe may lead off of the table if s and t are of different +267 // sizes +268 if (min > max) { +269 return -1; +270 } +271 +272 // ignore entry left of leftmost +273 if (min > 1) { +274 d[min - 1] = Integer.MAX_VALUE; +275 } +276 +277 // iterates through [min, max] in s +278 for (int i = min; i <= max; i++) { +279 if (left.charAt(i - 1) == rightJ) { +280 // diagonally left and up +281 d[i] = p[i - 1]; +282 } else { +283 // 1 + minimum of cell to the left, to the top, diagonally +284 // left and up +285 d[i] = 1 + Math.min(Math.min(d[i - 1], p[i]), p[i - 1]); +286 } +287 } +288 +289 // copy current distance counts to 'previous row' distance counts +290 tempD = p; +291 p = d; +292 d = tempD; +293 } +294 +295 // if p[n] is greater than the threshold, there's no guarantee on it +296 // being the correct +297 // distance +298 if (p[n] <= threshold) { +299 return p[n]; +300 } +301 return -1; +302 } +303 +304 /** +305 * <p>Find the Levenshtein distance between two Strings.</p> +306 * +307 * <p>A higher score indicates a greater distance.</p> 308 * -309 * <p>A higher score indicates a greater distance.</p> -310 * -311 * <p>The previous implementation of the Levenshtein distance algorithm -312 * was from <a href="http://www.merriampark.com/ld.htm">http://www.merriampark.com/ld.htm</a></p> -313 * -314 * <p>Chas Emerick has written an implementation in Java, which avoids an OutOfMemoryError -315 * which can occur when my Java implementation is used with very large strings.<br> -316 * This implementation of the Levenshtein distance algorithm -317 * is from <a href="http://www.merriampark.com/ldjava.htm">http://www.merriampark.com/ldjava.htm</a></p> -318 * -319 * <pre> -320 * unlimitedCompare(null, *) = IllegalArgumentException -321 * unlimitedCompare(*, null) = IllegalArgumentException -322 * unlimitedCompare("","") = 0 -323 * unlimitedCompare("","a") = 1 -324 * unlimitedCompare("aaapppp", "") = 7 -325 * unlimitedCompare("frog", "fog") = 1 -326 * unlimitedCompare("fly", "ant") = 3 -327 * unlimitedCompare("elephant", "hippo") = 7 -328 * unlimitedCompare("hippo", "elephant") = 7 -329 * unlimitedCompare("hippo", "zzzzzzzz") = 8 -330 * unlimitedCompare("hello", "hallo") = 1 -331 * </pre> -332 * -333 * @param left the first String, must not be null -334 * @param right the second String, must not be null -335 * @return result distance, or -1 -336 * @throws IllegalArgumentException if either String input {@code null} -337 */ -338 private static int unlimitedCompare(CharSequence left, CharSequence right) { -339 if (left == null || right == null) { -340 throw new IllegalArgumentException("Strings must not be null"); -341 } -342 -343 /* -344 The difference between this impl. and the previous is that, rather -345 than creating and retaining a matrix of size s.length() + 1 by t.length() + 1, -346 we maintain two single-dimensional arrays of length s.length() + 1. The first, d, -347 is the 'current working' distance array that maintains the newest distance cost -348 counts as we iterate through the characters of String s. Each time we increment -349 the index of String t we are comparing, d is copied to p, the second int[]. Doing so -350 allows us to retain the previous cost counts as required by the algorithm (taking -351 the minimum of the cost count to the left, up one, and diagonally up and to the left -352 of the current cost count being calculated). (Note that the arrays aren't really -353 copied anymore, just switched...this is clearly much better than cloning an array -354 or doing a System.arraycopy() each time through the outer loop.) -355 -356 Effectively, the difference between the two implementations is this one does not -357 cause an out of memory condition when calculating the LD over two very large strings. -358 */ -359 -360 int n = left.length(); // length of left -361 int m = right.length(); // length of right -362 -363 if (n == 0) { -364 return m; -365 } else if (m == 0) { -366 return n; -367 } -368 -369 if (n > m) { -370 // swap the input strings to consume less memory -371 final CharSequence tmp = left; -372 left = right; -373 right = tmp; -374 n = m; -375 m = right.length(); -376 } -377 -378 int[] p = new int[n + 1]; //'previous' cost array, horizontally -379 int[] d = new int[n + 1]; // cost array, horizontally -380 int[] tempD; //placeholder to assist in swapping p and d -381 -382 // indexes into strings left and right -383 int i; // iterates through left -384 int j; // iterates through right +309 * <p>The previous implementation of the Levenshtein distance algorithm +310 * was from <a href="http://www.merriampark.com/ld.htm">http://www.merriampark.com/ld.htm</a></p> +311 * +312 * <p>Chas Emerick has written an implementation in Java, which avoids an OutOfMemoryError +313 * which can occur when my Java implementation is used with very large strings.<br> +314 * This implementation of the Levenshtein distance algorithm +315 * is from <a href="http://www.merriampark.com/ldjava.htm">http://www.merriampark.com/ldjava.htm</a></p> +316 * +317 * <pre> +318 * unlimitedCompare(null, *) = IllegalArgumentException +319 * unlimitedCompare(*, null) = IllegalArgumentException +320 * unlimitedCompare("","") = 0 +321 * unlimitedCompare("","a") = 1 +322 * unlimitedCompare("aaapppp", "") = 7 +323 * unlimitedCompare("frog", "fog") = 1 +324 * unlimitedCompare("fly", "ant") = 3 +325 * unlimitedCompare("elephant", "hippo") = 7 +326 * unlimitedCompare("hippo", "elephant") = 7 +327 * unlimitedCompare("hippo", "zzzzzzzz") = 8 +328 * unlimitedCompare("hello", "hallo") = 1 +329 * </pre> +330 * +331 * @param left the first String, must not be null +332 * @param right the second String, must not be null +333 * @return result distance, or -1 [... 117 lines stripped ...]