1 / 17

编辑距离

编辑距离. 林彦彦 04-11-19. 1. 概念. 编辑距离( edit distance ) 两个字符串通过插入字符、删除字符、改写字符而变为相同字符串所需要的操作数 。 eg. d(“abc”,”abd”) = 1 d(“abc”,”ab”) = 1 d(“abc”, “abcdf”) = 2 d(“serverU”,” ser-u”)=4. 规则定义. 对字符串 s1 和 s2 的编辑距离 d(s1,s2) 定义: d('', '') = 0

ulla-sears
Télécharger la présentation

编辑距离

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 编辑距离 林彦彦 04-11-19

  2. 1. 概念 编辑距离(edit distance) 两个字符串通过插入字符、删除字符、改写字符而变为相同字符串所需要的操作数 。 eg. d(“abc”,”abd”) = 1 d(“abc”,”ab”) = 1 d(“abc”, “abcdf”) = 2 d(“serverU”,” ser-u”)=4

  3. 规则定义 对字符串s1和s2的编辑距离d(s1,s2) 定义: • d('', '') = 0 • d(s, '') = d('', s) = |s| • d(s1+ch1, s2+ch2) = min( d(s1, s2) + if ch1=ch2 then 0 else 1 fi, d(s1+ch1, s2) + 1, d(s1, s2+ch2) + 1 )

  4. 2.DPA实现 设二维矩阵 m[0..|s1|,0..|s2|] 其中,i=1..|s1|, j=1..|s2| • m[0, 0] = 0 • m[i, 0] = i, i=1..|s1| • m[0, j] = j, j=1..|s2| • m[i,j] = min( m[i-1,j-1] + if s1[i]=s2[j] then 0 else 1 fi, m[i-1, j] + 1, m[i, j-1] + 1)

  5. 复杂度分析 • 时间复杂性:O(|s1|*|s2|) 设|s1|和|s2|与n相当,则复杂性为O(n2) • 空间复杂性:O(n2) 改进: O(|s1|)也就是O(n)

  6. 3.路径方法 现有字符串X=abbab, Y=bbaba 横边——插入 斜边——替换 竖边——删除 从start到goal 的最短路径为 d(X,Y) ;

  7. 加速机制: • 相同替换操作优先 • 最小变换里头,删除和插入不可能成为连续操作

  8. 4.模糊统一问题 三个需求 • 编辑距离要拓展成能处理树结构的比较; • 与传统一致化问题的兼容性 • 字符串比较与长度无关;

  9. [i] 解决方法消除字符串长度对编辑距离值的影响 编辑距离标准化: 对两个字符串X、Y,计算 ne的值介于0与1之间

  10. d(address,adresse)=2 d(007,oo7)=2 ne(address,adresse)=2/7 ne(007,oo7)=2/3

  11. [ii] 解决方法对树结构的比较 • 定义(一): V为变量集 F为函数集 P为公式集 对 都可视x为一个项; 对 ,t1,t2,…,tn为项 f(t1,…tn)都为一个项; p(t1,…,tn)为一个原子式

  12. 定义(二) 设t=f(t1,..tn)和t’=ft(t1’,…tm’)为两个项或公式,x、y为变量,那么有 size(x)=size(ε)εεεε size(f)=|f| size(f(t1,…,tn))=|f|+∑i=1…nsize(ti)

  13. 定义(三) 树编辑距离et,它返回三个参数: 1st:错配数目;2nd:可置换的表示; 3rd:标准化因子(分母项)。 et(t, ε)=(size(t),[],size(t)) et(x,y)=(0,[x/y],0) et(f,f’)=(d(f,f’),[],max{|f|,|f’|}) et(t,t’)=et(f,f’) minv{ et((t2,..,tn),(t1’,..,tm’)) et(t1, ε), et((t1,..,tn),(t2’,..,tm’)) et(t1’, ε), et((t2,..,tn),(t2’,..,tm’)) et(t1,t1’) } 其中(v,s,n) (v’,s’,n’)=(v+v’,ss’,n+n’)

  14. 设et(t,t’)=(v,s,n) 则:标准化net(t,t’)=(v/n,s)——模糊一致 • t1=address(Northampton) t2=address(10,Northampton) et(t1,t2)=(0,[],7) (2,[],2+11) net(t1,t2)=2/(7+2+11)=1/10 • t1=address(Northampton) t2=adresse(Northampton) net(t1,t2)=2/(7+11)=1/9 • t1=address(Northampton) t2=address(Northhampton) net(t1,t2)=1/(7+12)=1/19

  15. 编辑距离标准化vs树编辑距离标准化 性质: 对于两个给定的项或公式t和t’,有 • ne(t,t’) net(t,t’) • 0 net(t,t’) 1 对于字符串A=CD, B=EF • d(A,B) d(C,E) + d(D,F)

  16. 4. 应用举例: DB中对H2O的处理

  17. 5. 编辑距离的应用 • 拼写纠错; • 文字匹配; • 数据分类整理; • 信息检索; • 语音识别; • DNA分析;

More Related