关于算法:如何在Delphi中实现Levenshtein距离?

关于算法:如何在Delphi中实现Levenshtein距离?

How do you implement Levenshtein distance in Delphi?

我本着回答您自己问题的精神发布此帖子。

我的问题是:如何在Delphi中实现Levenshtein算法来计算两个字符串之间的编辑距离?

只是关于性能的说明:
这个东西很快。 在我的台式机(2.33 Ghz双核,2GB内存,WinXP)上,我可以在不到一秒钟的时间内遍历100K字符串数组。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function EditDistance(s, t: string): integer;
var
  d : array of array of integer;
  i,j,cost : integer;
begin
  {
  Compute the edit-distance between two strings.
  Algorithm and description may be found at either of these two links:
  http://en.wikipedia.org/wiki/Levenshtein_distance
  http://www.google.com/search?q=Levenshtein+distance
  }


  //initialize our cost array
  SetLength(d,Length(s)+1);
  for i := Low(d) to High(d) do begin
    SetLength(d[i],Length(t)+1);
  end;

  for i := Low(d) to High(d) do begin
    d[i,0] := i;
    for j := Low(d[i]) to High(d[i]) do begin
      d[0,j] := j;
    end;
  end;

  //store our costs in a 2-d grid  
  for i := Low(d)+1 to High(d) do begin
    for j := Low(d[i])+1 to High(d[i]) do begin
      if s[i] = t[j] then begin
        cost := 0;
      end
      else begin
        cost := 1;
      end;

      //to use"Min", add"Math" to your uses clause!
      d[i,j] := Min(Min(
                 d[i-1,j]+1,      //deletion
                 d[i,j-1]+1),     //insertion
                 d[i-1,j-1]+cost  //substitution
                 );
    end;  //for j
  end;  //for i

  //now that we've stored the costs, return the final one
  Result := d[Length(s),Length(t)];

  //dynamic arrays are reference counted.
  //no need to deallocate them
end;

推荐阅读

    linux命令替换字符串?

    linux命令替换字符串?,字符串,文件,批量,首次,数据,命令,内容,方法,用字,结

    linux用计算器的命令?

    linux用计算器的命令?,代码,环境,情况,异常,工具,数据,位置,平台,精密,设计,

    linux拼接字符串命令?

    linux拼接字符串命令?,系统,工作,代码,工具,名称,信息,地址,时间,数据,命令,l

    linux的数学计算命令?

    linux的数学计算命令?,工作,系统,信息,地址,数字,目录,命令,百分比,情况,管

    添加字符串命令linux?

    添加字符串命令linux?,情况,名称,文件,位置,名字,地方,连续,信息,命令,内容,L

    linux云计算查看命令?

    linux云计算查看命令?,系统,信息,地址,工作,命令,情况,标准,服务,软件,网络,l

    linux打开计算器命令?

    linux打开计算器命令?,密码,电脑,工作,设备,数字,系统,手机,指数,情况,服务,

    linux命令输出计算?

    linux命令输出计算?,标准,地址,工作,信息,系统,命令,软件,数据,文件,控制台,l

    linux命令查找字符串?

    linux命令查找字符串?,工具,信息,命令,字符串,系统,工作,文件,范本,样式,文

    linux计算总数命令?

    linux计算总数命令?,系统,第一,情况,数据,信息,电脑,命令,百分比,单位,工作,l

    linux中计算器命令?

    linux中计算器命令?,地址,数据,位置,网络,设备,时间,环境,平台,软件,命令,说

    linux退出计算器命令?

    linux退出计算器命令?,工作,地址,系统,命令,通信,信息,电脑,目录,路径,操作,

    重启计算机linux命令?

    重启计算机linux命令?,系统,设备,工作,标准,名称,命令,状态,数据,服务,提示,L

    linux计算器打开命令?

    linux计算器打开命令?,工作,地址,命令,标准,管理,系统,目录,路径,管道,控制

    linux计算摘要的命令?

    linux计算摘要的命令?,数据,网络,数字,密码,工具,名称,正规,标准,代码,文件,l

    linux命令计算时间差?

    linux命令计算时间差?,时间,系统,标准,流程,状态,单位,名称,表达式,命令,时

    linux命令重启计算机?

    linux命令重启计算机?,服务,系统,工作,设备,标准,命令,名称,进程,级别,用户,l

    linux命令计算圆周率?

    linux命令计算圆周率?,代码,圆周率,无理数,直径,比值,表示,圆周,常量,常数,

    linux命令字符串匹配?

    linux命令字符串匹配?,系统,工具,命令,字符串,灵活,状态,文件,文本,模式,管

    python字符串截取?

    python字符串截取?,代码,步长,位置,分析,字符串,字符,信息,灵活,数字,表示,在