Java String Levenshtein Distance levenshteinDistance(final String string1, final String string2, int swap, int substitution, int insertion, int deletion)

Here you can find the source of levenshteinDistance(final String string1, final String string2, int swap, int substitution, int insertion, int deletion)

Description

levenshtein Distance

License

Open Source License

Declaration

public static int levenshteinDistance(final String string1, final String string2, int swap, int substitution,
            int insertion, int deletion) 

Method Source Code

//package com.java2s;
/*/*w ww .  j ava  2  s.  co  m*/
 * The MIT License
 *
 * Copyright (c) 2009 The Broad Institute
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    public static int levenshteinDistance(final String string1, final String string2, int swap, int substitution,
            int insertion, int deletion) {
        int i, j;

        int[] row0 = new int[(string2.length() + 1)];
        int[] row1 = new int[(string2.length() + 1)];
        int[] row2 = new int[(string2.length() + 1)];
        int[] dummy;

        final byte[] str1 = string1.getBytes();
        final byte[] str2 = string2.getBytes();

        for (j = 0; j < str2.length; j++) {
            row1[j] = j * insertion;
        }
        for (i = 0; i < str1.length; i++) {
            row2[0] = (i + 1) * deletion;
            for (j = 0; j < str2.length; j++) {
                /* substitution */
                row2[j + 1] = row1[j];
                if (str1[i] != str2[j]) {
                    row2[j + 1] += substitution;
                }
                /* swap */
                if (i > 0 && j > 0 && str1[i - 1] == str2[j] && str1[i] == str2[j - 1]
                        && row2[j + 1] > row0[j - 1] + swap) {
                    row2[j + 1] = row0[j - 1] + swap;
                }
                /* deletion */
                if (row2[j + 1] > row1[j + 1] + deletion) {
                    row2[j + 1] = row1[j + 1] + deletion;
                }
                /* insertion */
                if (row2[j + 1] > row2[j] + insertion) {
                    row2[j + 1] = row2[j] + insertion;
                }
            }

            dummy = row0;
            row0 = row1;
            row1 = row2;
            row2 = dummy;
        }

        i = row1[str2.length];

        return i;
    }
}

Related

  1. levenshtein(String str1, String str2)
  2. levenshteinDistance(CharSequence lhs, CharSequence rhs)
  3. levenshteinDistance(CharSequence lhs, CharSequence rhs)
  4. levenshteinDistance(CharSequence s, CharSequence t)
  5. levenshteinDistance(CharSequence str1, CharSequence str2)
  6. LevenshteinDistance(String actual, String expected)
  7. levenshteinDistance(String s, String t)
  8. levenshteinDistance(String s, String t)
  9. levenshteinDistance(String s, String t)