Advertisement
Guest User

Untitled

a guest
Dec 26th, 2019
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. PRINT LevenshteinDistance("abc", "xyz")
  2. PRINT LevenshteinDistance("abc", "xyzd")
  3. PRINT LevenshteinDistance("abc", "abcdef")
  4. PRINT LevenshteinDistance("abc", "abcd")
  5. PRINT LevenshteinDistance("カラクリ", "ボンゴレ")
  6. PRINT LevenshteinDistance("テスト", "テストパターン")
  7.  
  8. // https://ohnishiakira.hatenadiary.org/entry/20090809/1249845529
  9. // をuwscに実装
  10. function min3(a, b, c)
  11. result = a
  12. if b<a then result = b
  13. if c<result then result = c
  14. fend
  15.  
  16. function max(a, b)
  17. result = a
  18. if b>a then result = b
  19. fend
  20.  
  21. const LD_STRMAX=100
  22. function LevenshteinDistance(s1, s2)
  23. DIM len1 = length(s1)+1
  24. DIM len2 = length(s2)+1
  25. DIM str1[0], str2[0]
  26. DIM array[LD_STRMAX+1][LD_STRMAX+1]
  27. result = -1;
  28. PRINT s1+"("+len1+") "+s2+"("+len2+")"
  29.  
  30. if len1>LD_STRMAX or len2>LD_STRMAX then exit
  31.  
  32. resize(str1, length(s1)-1)
  33. resize(str2, length(s2)-1)
  34. for i=0 to length(s1)-1
  35. str1[i] = copy(s1, i+1, 1)
  36. next
  37. for i=0 to length(s2)-1
  38. str2[i] = copy(s2, i+1, 1)
  39. next
  40. for i1=0 to len1-1
  41. array[i1][0] = i1
  42. next
  43. for i2=0 to len2-1
  44. array[0][i2] = i2
  45. next
  46.  
  47. for i1=1 to len1-1
  48. for i2=1 to len2-1
  49. cost = 0
  50. if str1[i1-1] <> str2[i2-1] then cost = 1
  51. array[i1][i2] = min3(array[i1-1][i2]+1, array[i1][i2-1]+1, array[i1-1][i2-1]+cost)
  52. next
  53. next
  54. result = array[len1-1][len2-1] / max(len1-1, len2-1)
  55. fend
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement