Java Distance Calculate distanceLevenshtein(CharSequence s, CharSequence t)

Here you can find the source of distanceLevenshtein(CharSequence s, CharSequence t)

Description

distance Levenshtein

License

Apache License

Declaration

public static int distanceLevenshtein(CharSequence s, CharSequence t) 

Method Source Code

//package com.java2s;
/**/*from   ww  w.  ja v  a2 s. com*/
 * 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 distanceLevenshtein(CharSequence s, CharSequence t) {
        // degenerate cases s
        if (s == null || "".equals(s)) {
            return t == null || "".equals(t) ? 0 : t.length();
        } else if (t == null || "".equals(t)) {
            return s.length();
        }

        // create two work vectors of integer distances
        int[] v0 = new int[t.length() + 1];
        int[] v1 = new int[t.length() + 1];

        // initialize v0 (the previous row of distances)
        // this row is A[0][i]: edit distance for an empty s
        // the distance is just the number of characters to delete from t
        for (int i = 0; i < v0.length; i++) {
            v0[i] = i;
        }

        int sLen = s.length();
        int tLen = t.length();
        for (int i = 0; i < sLen; i++) {
            // calculate v1 (current row distances) from the previous row v0

            // first element of v1 is A[i+1][0]
            // edit distance is delete (i+1) chars from s to match empty t
            v1[0] = i + 1;

            // use formula to fill in the rest of the row
            for (int j = 0; j < tLen; j++) {
                int cost = (s.charAt(i) == t.charAt(j)) ? 0 : 1;

                int[] values = new int[] { v1[j] + 1, v0[j + 1] + 1, v0[j] + cost };
                int len = values.length;
                int current = values[0];

                for (int k = 1; k < len; k++) {
                    current = Math.min(values[k], current);
                }
                v1[j + 1] = current;
            }

            // copy v1 (current row) to v0 (previous row) for next iteration
            System.arraycopy(v1, 0, v0, 0, v0.length);
        }

        return v1[t.length()];
    }

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

Related

  1. distanceInMetersBetween(final double lat1, final double lng1, final double lat2, final double lng2)
  2. distanceInMilesBetween(float lat1, float lng1, float lat2, float lng2)
  3. distanceInMilesBetweenDouble(Float centerLat, Float centerLon, double lat, double lon)
  4. distanceInRadians(double latitude1, double longitude1, double latitude2, double longitude2)
  5. distanceKM(double lat1, double lng1, double lat2, double lng2)
  6. distanceLongitude(double latitude, double east, double west)
  7. distanceMat(double[][] m1, double[][] m2)
  8. distanceMeter(double prevLat, double prevLon, double currentLat, double currentLon)
  9. distanceNd(double[] p1, double[] p2, double[] scratchSpace)