pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / spellchecker / src / java / org / apache / lucene / search / spell / LevensteinDistance.java
1 package org.apache.lucene.search.spell;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 /**
21  * Levenstein edit distance class.
22  */
23 public final class LevensteinDistance implements StringDistance {
24
25     /**
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.
28      */
29     public LevensteinDistance () {
30     }
31
32
33     //*****************************
34     // Compute Levenshtein distance: see org.apache.commons.lang.StringUtils#getLevenshteinDistance(String, String)
35     //*****************************
36     public float getDistance (String target, String other) {
37       char[] sa;
38       int n;
39       int p[]; //'previous' cost array, horizontally
40       int d[]; // cost array, horizontally
41       int _d[]; //placeholder to assist in swapping p and d
42       
43         /*
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.)
55
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.
58          */
59
60         sa = target.toCharArray();
61         n = sa.length;
62         p = new int[n+1]; 
63         d = new int[n+1]; 
64       
65         final int m = other.length();
66         if (n == 0 || m == 0) {
67           if (n == m) {
68             return 1;
69           }
70           else {
71             return 0;
72           }
73         } 
74
75
76         // indexes into strings s and t
77         int i; // iterates through s
78         int j; // iterates through t
79
80         char t_j; // jth character of t
81
82         int cost; // cost
83
84         for (i = 0; i<=n; i++) {
85             p[i] = i;
86         }
87
88         for (j = 1; j<=m; j++) {
89             t_j = other.charAt(j-1);
90             d[0] = j;
91
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);
96             }
97
98             // copy current distance counts to 'previous row' distance counts
99             _d = p;
100             p = d;
101             d = _d;
102         }
103
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));
107     }
108
109 }