zoukankan      html  css  js  c++  java
  • [转]Levenshtein Distance(LD)计算两字符串相似度算法

    两字符串相似度计算方法有好多,现对基于编距的算法的相似度计算自己总结下。

        简单介绍下Levenshtein Distance(LD):LD 可能衡量两字符串的相似性。它们的距离就是一个字符串转换成那一个字符串过程中的添加、删除、修改数值。

        举例:

    如果str1="test",str2="test",那么LD(str1,str2) = 0。没有经过转换。
    如果str1="test",str2="tent",那么LD(str1,str2) = 1。str1的"s"转换"n",转换了一个字符,所以是1。
    如果它们的距离越大,说明它们越是不同。

         Levenshtein distance最先是由俄国科学家Vladimir Levenshtein在1965年发明,用他的名字命名。不会拼读,可以叫它edit distance(编辑距离)。

        Levenshtein distance可以用来:

    Spell checking(拼写检查)
    Speech recognition(语句识别)
    DNA analysis(DNA分析)
    Plagiarism detection(抄袭检测)
    LD用m*n的矩阵存储距离值。算法大概过程:

    str1或str2的长度为0返回另一个字符串的长度。
    初始化(n+1)*(m+1)的矩阵d,并让第一行和列的值从0开始增长。
    扫描两字符串(n*m级的),如果:str1[i] == str2[j],用temp记录它,为0。否则temp记为1。然后在矩阵d[i][j]赋于d[i-1][j]+1 、d[i][j-1]+1、d[i-1][j-1]+temp三者的最小值。
    扫描完后,返回矩阵的最后一个值即d[n][m]
    最后返回的是它们的距离。怎么根据这个距离求出相似度呢?因为它们的最大距离就是两字符串长度的最大值。对字符串不是很敏感。现我把相似度计算公式定为1-它们的距离/字符串长度最大值。

        源码:

    /**
     * 编辑距离的两字符串相似度
      */
        public class Similarity
        {

            private int min(int one, int two, int three)
            {
                int min = one;
                if (two < min)
                {
                    min = two;
                }
                if (three < min)
                {
                    min = three;
                }
                return min;
            }

            public int LD(String str1, String str2)
            {
                int[,] d;     // 矩阵
                int n = str1.Length;
                int m = str2.Length;
                int i;     // 遍历str1的
                int j;     // 遍历str2的
                char ch1;     // str1的
                char ch2;     // str2的
                int temp;     // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
                if (n == 0)
                {
                    return m;
                }
                if (m == 0)
                {
                    return n;
                }

                d = new int[n + 1, m + 1];

                for (i = 0; i <= n; i++)
                {     // 初始化第一列
                    d[i, 0] = i;
                }
                for (j = 0; j <= m; j++)
                {     // 初始化第一行
                    d[0, j] = j;
                }
                for (i = 1; i <= n; i++)
                {     // 遍历str1
                    ch1 = str1[i - 1];
                    // 去匹配str2
                    for (j = 1; j <= m; j++)
                    {
                        ch2 = str2[j - 1];
                        if (ch1 == ch2)
                        {
                            temp = 0;
                        }
                        else
                        {
                            temp = 1;
                        }
                        // 左边+1,上边+1, 左上角+temp取最小
                        d[i, j] = min(d[i - 1, j] + 1, d[i, j - 1] + 1, d[i - 1, j - 1] + temp);
                    }
                }
                return d[n, m];
            }

            public double sim(String str1, String str2)
            {
                int ld = LD(str1, str2);
                return 1 - (double)ld / Math.Max(str1.Length, str2.Length);
            }

            public static void Main(String[] args)
            {
                Similarity s = new Similarity();
                String str1 = " abc ";
                String str2 = " bc";

                Console.WriteLine(" ld= " + s.LD(str1, str2));
                Console.WriteLine(" sim= " + s.sim(str1, str2));
                Console.ReadLine();
            }
        }

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lkf0217/archive/2009/08/20/4466952.aspx

  • 相关阅读:
    [PHP] laravel框架注意bootstrap/cache
    [git] git拉取远程分支代码
    Prometheus监控系统(4)pushgateway及自定义脚本
    k8s 学习
    pxc /Galera Cluster详解
    Percona Monitoring and Management
    etcd获取所有key
    kubernetes Pod驱逐迁移drain用法
    kubernetes etcd数据管理
    办公环境下k8s网络互通方案
  • 原文地址:https://www.cnblogs.com/anorthwolf/p/1693737.html
Copyright © 2011-2022 走看看