Posted By

Scooter on 10/18/08


Tagged

strings


Versions (?)

Levenshtein distance


 / Published in: ASP
 

URL: http://reusablecode.blogspot.com/2008/10/levenshtein-distance.html

Calculate the Levenshtein distance between two strings.

  1. <%
  2. ' Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  3. '
  4. ' This work is licensed under the Creative Commons Attribution License. To view
  5. ' a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  6. ' send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  7. ' 94305, USA.
  8.  
  9. ' Compute the Levenshtein distance between two strings.
  10. function levenshtein(byVal first, byVal second)
  11. dim distance
  12. dim truncateLength
  13.  
  14. if first = second then
  15. ' The distance is zero if the strings are identical.
  16. distance = 0
  17. else
  18. ' The distance is at least the difference of the lengths of the two strings.
  19. distance = abs(len(first) - len(second))
  20.  
  21. ' Force the strings to be the same length to prevent overflows.
  22. truncateLength = ((len(first) + len(second)) - distance) / 2
  23. first = Left(first, truncateLength)
  24. second = Left(second, truncateLength)
  25.  
  26. ' Compare the corresponding characters in each string.
  27. for i = 1 to truncateLength
  28. if Mid(first, i, 1) <> Mid(second, i, 1) then
  29. distance = distance + 1
  30. end if
  31. next
  32. end if
  33.  
  34. levenshtein = distance
  35. end function
  36. %>

Report this snippet  

You need to login to post a comment.