Java String Levenshtein Distance levenshtein(String str1, String str2)

Here you can find the source of levenshtein(String str1, String str2)

Description

levenshtein

License

Open Source License

Parameter

Parameter Description
str1 a parameter
str2 a parameter

Declaration

public static double levenshtein(String str1, String str2) 

Method Source Code

//package com.java2s;
/*//from   w  w w.  j  a  v  a  2 s. c o m
Copyright (C) 2013     Enzo Seraphim
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
or visit <http://www.gnu.org/licenses/>
*/

public class Main {
    /**
     *
     * @param str1
     * @param str2
     * @return
     */
    public static double levenshtein(String str1, String str2) {
        byte[] bstr1 = str1.toLowerCase().getBytes();
        byte[] bstr2 = str2.toLowerCase().getBytes();

        int sizeI = bstr1.length + 1;
        int sizeJ = bstr2.length + 1;
        int align[][] = new int[sizeI][sizeJ];
        int i, j, cost, ins, upd, del, greaterLen;
        if (sizeI > sizeJ) {
            greaterLen = sizeI - 1;
        } else {
            greaterLen = sizeJ - 1;
        }
        align[0][0] = 0;
        for (i = 1; i < sizeI; i++) {
            align[i][0] = i;
        } //end for
        for (j = 1; j < sizeJ; j++) {
            align[0][j] = j;
        } //end for
        for (i = 1; i < sizeI; i++) {
            for (j = 1; j < sizeJ; j++) {
                // Cost
                if (bstr1[i - 1] == bstr2[j - 1]) {
                    cost = 0;
                } else {
                    cost = 1;
                }
                ins = align[i - 1][j] + 1;
                del = align[i][j - 1] + 1;
                upd = align[i - 1][j - 1] + cost;

                if ((ins < del) && (ins < upd)) {
                    align[i][j] = ins;
                } else if (del < upd) {
                    align[i][j] = del;
                } else {
                    align[i][j] = upd;
                }

            }
        }
        return ((double) align[sizeI - 1][sizeJ - 1]);
        //   return ((double) align[sizeI - 1][sizeJ - 1] / greaterLen);
    }
}

Related

  1. levenshtein(final String s1, final String s2)
  2. levenshtein(String s, String t)
  3. levenshteinDistance(CharSequence lhs, CharSequence rhs)
  4. levenshteinDistance(CharSequence lhs, CharSequence rhs)
  5. levenshteinDistance(CharSequence s, CharSequence t)
  6. levenshteinDistance(CharSequence str1, CharSequence str2)