Java String Levenshtein Distance levenshteinDistance(String s, String t, int limit)

Here you can find the source of levenshteinDistance(String s, String t, int limit)

Description

Rank two strings similarity in terms of distance The lower the number, the more similar these strings are to each other See: http://en.wikipedia.org/wiki/Levenshtein_distance#Computing_Levenshtein_distance

License

Apache License

Parameter

Parameter Description
s a parameter
t a parameter
limit a parameter

Return

Distance (higher is better)

Declaration

public static int levenshteinDistance(String s, String t, int limit) 

Method Source Code

//package com.java2s;
/*/*  w  w  w  .  j av a 2s .c om*/
 * Copyright (C) 2015 The 8-Bit Bunch. Licensed under the Apache License, Version 1.1 
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-1.1>.
 * Unless required by applicable law or agreed to in writing, software distributed under 
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF 
 * ANY KIND, either express or implied. See the License for the specific language 
 * governing permissions and limitations under the License.
 */

public class Main {
    /**
     * Rank two strings similarity in terms of distance The lower the number,
     * the more similar these strings are to each other See:
     * http://en.wikipedia.org/wiki/Levenshtein_distance#Computing_Levenshtein_distance
     *
     * @param s
     * @param t
     * @param limit
     * @return Distance (higher is better)
     */
    public static int levenshteinDistance(String s, String t, int limit) {
        int sizeDiff = Math.abs(s.length() - t.length());
        if (sizeDiff > limit) {
            return sizeDiff;
        }

        s = s.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");
        t = t.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");
        int m = s.length();
        int n = t.length();
        int[][] dist = new int[m + 1][n + 1];
        for (int i = 1; i <= m; i++) {
            dist[i][0] = i;
        }
        for (int i = 1; i <= n; i++) {
            dist[0][i] = i;
        }
        for (int j = 1; j <= n; j++) {
            int min = 100;
            for (int i = 1; i <= m; i++) {
                if (s.charAt(i - 1) == t.charAt(j - 1)) {
                    dist[i][j] = dist[i - 1][j - 1];
                } else {
                    int del = dist[i - 1][j] + 1;
                    int insert = dist[i][j - 1] + 1;
                    int sub = dist[i - 1][j - 1] + 1;
                    dist[i][j] = Math.min(Math.min(del, insert), sub);
                }
                min = Math.min(min, dist[i][j]);
            }
            if (min > limit) {
                return min;
            }
        }
        return dist[m][n];
    }
}

Related

  1. levenshteinDistance(String s, String t)
  2. levenshteinDistance(String s, String t)
  3. LevenshteinDistance(String s, String t)
  4. levenshteinDistance(String s, String t)
  5. levenshteinDistance(String s, String t)
  6. levenshteinDistance(String s1, String s2)
  7. levenshteinDistance(String s1, String s2)
  8. levenshteinDistance(String st1, String st2)
  9. levenshteinDistance(String string1, String string2)