Java Distance Calculate distanceDamerauLevenshtein(CharSequence source, CharSequence target)

Here you can find the source of distanceDamerauLevenshtein(CharSequence source, CharSequence target)

Description

distance Damerau Levenshtein

License

Apache License

Declaration

public static int distanceDamerauLevenshtein(CharSequence source, CharSequence target) 

Method Source Code

//package com.java2s;
/**/* ww  w .ja  va2s  .co  m*/
 * Copyright (C) 2017 Lucifer Wong
 *
 * Licensed under the Apache License, Version 2.0 (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-2.0
 *
 * 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 {

    public static int distanceDamerauLevenshtein(CharSequence source, CharSequence target) {
        if (source == null || "".equals(source)) {
            return target == null || "".equals(target) ? 0 : target.length();
        } else if (target == null || "".equals(target)) {
            return source.length();
        }

        int srcLen = source.length();
        int targetLen = target.length();
        int[][] distanceMatrix = new int[srcLen + 1][targetLen + 1];

        // We need indexers from 0 to the length of the source string.
        // This sequential set of numbers will be the row "headers"
        // in the matrix.
        for (int srcIndex = 0; srcIndex <= srcLen; srcIndex++) {
            distanceMatrix[srcIndex][0] = srcIndex;
        }

        // We need indexers from 0 to the length of the target string.
        // This sequential set of numbers will be the
        // column "headers" in the matrix.
        for (int targetIndex = 0; targetIndex <= targetLen; targetIndex++) {
            // Set the value of the first cell in the column
            // equivalent to the current value of the iterator
            distanceMatrix[0][targetIndex] = targetIndex;
        }

        for (int srcIndex = 1; srcIndex <= srcLen; srcIndex++) {
            for (int targetIndex = 1; targetIndex <= targetLen; targetIndex++) {
                // If the current characters in both strings are equal
                int cost = source.charAt(srcIndex - 1) == target.charAt(targetIndex - 1) ? 0 : 1;

                int[] values = new int[] {
                        // Character match between current character in
                        // source string and next character in target
                        distanceMatrix[srcIndex - 1][targetIndex] + 1,
                        // Character match between next character in
                        // source string and current character in target
                        distanceMatrix[srcIndex][targetIndex - 1] + 1,
                        // No match, at current, add cumulative penalty
                        distanceMatrix[srcIndex - 1][targetIndex - 1] + cost };
                int len = values.length;
                int current = values[0];

                for (int k = 1; k < len; k++) {
                    current = Math.min(values[k], current);
                }
                // Find the current distance by determining the shortest path to
                // a
                // match (hence the 'minimum' calculation on distances).
                distanceMatrix[srcIndex][targetIndex] = current;

                // We don't want to do the next series of calculations on
                // the first pass because we would get an index out of bounds
                // exception.
                if (srcIndex == 1 || targetIndex == 1) {
                    continue;
                }

                // transposition check (if the current and previous
                // character are switched around (e.g.: t[se]t and t[es]t)...
                if (source.charAt(srcIndex - 1) == target.charAt(targetIndex - 2)
                        && source.charAt(srcIndex - 2) == target.charAt(targetIndex - 1)) {

                    int[] values2 = new int[] {
                            // Current cost
                            distanceMatrix[srcIndex][targetIndex],
                            // Transposition
                            distanceMatrix[srcIndex - 2][targetIndex - 2] + cost };
                    int len2 = values2.length;
                    int current2 = values2[0];

                    for (int k = 1; k < len2; k++) {
                        current2 = Math.min(values2[k], current2);
                    }
                    // What's the minimum cost between the current distance
                    // and a transposition.
                    distanceMatrix[srcIndex][targetIndex] = current2;
                }
            }
        }

        return distanceMatrix[srcLen][targetLen];
    }

    public static boolean equals(String str1, String str2) {
        if (str1 == null || str2 == null) {
            return false;
        }
        return str1.equals(str2);
    }
}

Related

  1. distanceBetweenPoints(float vx, float vy, float wx, float wy)
  2. distanceBetweenPoints(float[] xyz1, float[] xyz2)
  3. distanceByLBS(double lo1, double la1, double lo2, double la2)
  4. distanceCircle(int i0, int i1, int dir, int size)
  5. distanceCorrelation(final double[][] x, final double[][] y)
  6. distanceFast(double lat1, double lon1, double lat2, double lon2)
  7. distanceFrom(double lat1, double long1, double lat2, double long2)
  8. DistanceFromLine(float cx, float cy, float ax, float ay, float bx, float by)
  9. distanceFromLineToPoint(double x1, double y1, double z1, double x2, double y2, double z2, double px, double py, double pz)