package com.test.study;
/** 
 * @filename package com.test.study; 
 * @author 赵岩
 * @version 2015年11月19日
 * @classDescription:Levenshtein Distance 算法实现 
 * 可以使用的地方:DNA分析   拼字检查   语音辨识   抄袭侦测 
 */
public class myLevenshtein {
	public static void main(String[] args) {  
        //要比较的两个字符串  
        String str1 = "今天星eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee期四";  
        String str2 = "今天是dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd星期五";  
        long start=System.currentTimeMillis();
        levenshtein(str1,str2);
        long time = System.currentTimeMillis() - start;
        System.out.println("运行耗时= "+time+" 毫秒");
    }  
  
    /** 
     *   DNA分析   拼字检查   语音辨识   抄袭侦测 
     *  
     * @createTime 2012-1-12 
     */  
    public static void levenshtein(String str1,String str2) {  
        //计算两个字符串的长度。  
        int len1 = str1.length();  
        int len2 = str2.length();  
        //建立上面说的数组,比字符长度大一个空间  
        int[][] dif = new int[len1 + 1][len2 + 1];  
        //赋初值,步骤B。  
        for (int a = 0; a <= len1; a++) {  
            dif[a][0] = a;  
        }  
        for (int a = 0; a <= len2; a++) {  
            dif[0][a] = a;  
        }  
        //计算两个字符是否一样,计算左上的值  
        int temp;  
        for (int i = 1; i <= len1; i++) {  
            for (int j = 1; j <= len2; j++) {  
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {  
                    temp = 0;  
                } else {  
                    temp = 1;  
                }  
                //取三个值中最小的  
                dif[i][j] = min(dif[i - 1][j - 1] + temp, dif[i][j - 1] + 1,  
                        dif[i - 1][j] + 1);  
            }  
        }  
        System.out.println("字符串\""+str1+"\"与\""+str2+"\"的比较");  
        //取数组右下角的值,同样不同位置代表不同字符串的比较  
        System.out.println("差异步骤:"+dif[len1][len2]);  
        //计算相似度  
        float similarity =1 - (float) dif[len1][len2] / Math.max(str1.length(), str2.length());  
        System.out.println("相似度:"+similarity);  
    }  
  
    //得到最小值  
    private static int min(int... is) {  
        int min = Integer.MAX_VALUE;  
        for (int i : is) {  
            if (min > i) {  
                min = i;  
            }  
        }  
        return min;  
    }  
}
 最近下载更多
最近下载更多
                
                2196316269     LV10
                2021年2月25日
            
            
        
                whfuai     LV14
                2021年2月1日
            
            
        
                yuqm     LV17
                2020年4月10日
            
            
        
                xuxiaojie     LV1
                2019年8月14日
            
            
        
                WASDZZ     LV13
                2018年12月29日
            
            
        
                848581720     LV10
                2018年4月16日
            
            
        
                ewfasdfa     LV1
                2017年11月29日
            
            
        
                rongshuang     LV2
                2017年5月22日
            
            
        
                zx6262509     LV1
                2017年5月3日
            
            
        
                1q1111     LV1
                2017年5月3日
            
            
         最近浏览更多
最近浏览更多
                
                炫瓶百事可乐     LV1
                2022年12月1日
            
            
        
                公共分类     LV1
                2022年10月18日
            
            
        
                zinshao     LV12
                2022年7月21日
            
            
        
                微信网友_5992582549164032     LV6
                2022年6月29日
            
            
        
                myh7719     LV2
                2022年6月13日
            
            
        
                张洪     LV1
                2021年11月25日
            
            
        
                xiaoluoaaa     LV8
                2021年10月13日
            
            
        
                happyYang    
                2021年7月15日
            
            
                    暂无贡献等级
            
        
                1005948011     LV7
                2021年5月10日
            
            
        
                482286353     LV3
                2021年4月3日
            
            
        
 
                 
                 
                