Java String Levenshtein Distance levenshteinEquals(double threshold, String dom1, String dom2)

Here you can find the source of levenshteinEquals(double threshold, String dom1, String dom2)

Description

This method implements the equality check between two strings using the Levenshtein Distance algorithm

License

Apache License

Parameter

Parameter Description
threshold a parameter
dom1 a parameter
dom2 a parameter

Return

true/false

Declaration

public static boolean levenshteinEquals(double threshold, String dom1, String dom2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//w w w. jav a2s.  co m
     * This method implements the equality check between
     * two strings using the Levenshtein Distance algorithm
     * @param threshold
     * @param dom1
     * @param dom2
     * @return true/false
     */
    public static boolean levenshteinEquals(double threshold, String dom1, String dom2) {

        double l = levenshteinDistance(dom1, dom2);

        if (l >= ((dom1.length() + dom2.length()) * (1.0 - threshold))) {

            return false;
        }

        return true;
    }

    /**
     * This method implements the Levenshtein Distance algorithm
     * between two strings
     * @param s0 first string to be compared
     * @param s1 second string to be compared
     * @return the cost to turn s0 into s1
     */
    public static int levenshteinDistance(String s0, String s1) {
        int len0 = s0.length() + 1;
        int len1 = s1.length() + 1;

        // the array of distances                                                       
        int[] cost = new int[len0];
        int[] newcost = new int[len0];

        // initial cost of skipping prefix in String s0                                 
        for (int i = 0; i < len0; i++)
            cost[i] = i;

        // dynamically computing the array of distances                                  

        // transformation cost for each letter in s1                                    
        for (int j = 1; j < len1; j++) {
            // initial cost of skipping prefix in String s1                             
            newcost[0] = j;

            // transformation cost for each letter in s0                                
            for (int i = 1; i < len0; i++) {
                // matching current letters in both strings                             
                int match = (s0.charAt(i - 1) == s1.charAt(j - 1)) ? 0 : 1;

                // computing cost for each transformation                               
                int cost_replace = cost[i - 1] + match;
                int cost_insert = cost[i] + 1;
                int cost_delete = newcost[i - 1] + 1;

                // keep minimum cost                                                    
                newcost[i] = Math.min(Math.min(cost_insert, cost_delete), cost_replace);
            }

            // swap cost/newcost arrays                                                 
            int[] swap = cost;
            cost = newcost;
            newcost = swap;
        }

        // the distance is the cost for transforming all letters in both strings        
        return cost[len0 - 1];
    }
}

Related

  1. levenshteinDistance(String string1, String string2)
  2. levenshteinDistance(String wordForm, String lemma)
  3. levenshteinDistance(String x, String y)
  4. levenshteinDistance(String[] a, String[] b)
  5. levenshteinDistanceRatio(CharSequence lhs, CharSequence rhs)
  6. levenshteinSimilarity(String string1, String string2)