Similarity Score - Levenshtein

后端 未结 6 1701
有刺的猬
有刺的猬 2020-12-05 10:31

I implemented the Levenshtein algorithm in Java and am now getting the corrections made by the algorithm, a.k.a. the cost. This does help a little but not much since I want

6条回答
  •  不知归路
    2020-12-05 11:04

     // Refer This: 100% working
    
    public class demo 
    {
    public static void main(String[] args) 
    {
        String str1, str2;
    
        str1="12345";
        str2="122345";
    
    
        int re=pecentageOfTextMatch(str1, str2);
        System.out.println("Matching Percent"+re);
    }
    
    public static int pecentageOfTextMatch(String s0, String s1) 
    {                       // Trim and remove duplicate spaces
        int percentage = 0;
        s0 = s0.trim().replaceAll("\\s+", " ");
        s1 = s1.trim().replaceAll("\\s+", " ");
        percentage=(int) (100 - (float) LevenshteinDistance(s0, s1) * 100 / (float) (s0.length() + s1.length()));
        return percentage;
    }
    
    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 - 1;
    
            // 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];
    }
    
    }
    

提交回复
热议问题