1 package org.apache.lucene.search.spell;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * Levenstein edit distance class.
23 public final class LevensteinDistance implements StringDistance {
26 * Optimized to run a bit faster than the static getDistance().
27 * In one benchmark times were 5.3sec using ctr vs 8.5sec w/ static method, thus 37% faster.
29 public LevensteinDistance () {
33 //*****************************
34 // Compute Levenshtein distance: see org.apache.commons.lang.StringUtils#getLevenshteinDistance(String, String)
35 //*****************************
36 public float getDistance (String target, String other) {
39 int p[]; //'previous' cost array, horizontally
40 int d[]; // cost array, horizontally
41 int _d[]; //placeholder to assist in swapping p and d
44 The difference between this impl. and the previous is that, rather
45 than creating and retaining a matrix of size s.length()+1 by t.length()+1,
46 we maintain two single-dimensional arrays of length s.length()+1. The first, d,
47 is the 'current working' distance array that maintains the newest distance cost
48 counts as we iterate through the characters of String s. Each time we increment
49 the index of String t we are comparing, d is copied to p, the second int[]. Doing so
50 allows us to retain the previous cost counts as required by the algorithm (taking
51 the minimum of the cost count to the left, up one, and diagonally up and to the left
52 of the current cost count being calculated). (Note that the arrays aren't really
53 copied anymore, just switched...this is clearly much better than cloning an array
54 or doing a System.arraycopy() each time through the outer loop.)
56 Effectively, the difference between the two implementations is this one does not
57 cause an out of memory condition when calculating the LD over two very large strings.
60 sa = target.toCharArray();
65 final int m = other.length();
66 if (n == 0 || m == 0) {
76 // indexes into strings s and t
77 int i; // iterates through s
78 int j; // iterates through t
80 char t_j; // jth character of t
84 for (i = 0; i<=n; i++) {
88 for (j = 1; j<=m; j++) {
89 t_j = other.charAt(j-1);
92 for (i=1; i<=n; i++) {
93 cost = sa[i-1]==t_j ? 0 : 1;
94 // minimum of cell to the left+1, to the top+1, diagonally left and up +cost
95 d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost);
98 // copy current distance counts to 'previous row' distance counts
104 // our last action in the above loop was to switch d and p, so p now
105 // actually has the most recent cost counts
106 return 1.0f - ((float) p[n] / Math.max(other.length(), sa.length));