Code archives/Algorithms/LevenshteinDistance
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
for an example of use see: http://blitzbasic.com/codearcs/codearcs.php?code=2880#comments | |||||
Function LevenshteinDistance(String1$, String2$) ;calculates the minimun amount of basic operations required to transmorm String1 into String2 ;3 basic operations are considered: Character Delet, Insert, Substitution Local Len1=Len(String1) Local Len2=Len(String2) Dim Ld(Len1, Len2) Local i, j, Cost, s1$, s2$ If Len1 = 0 Then Return Len2 If Len2 = 0 Then Return Len1 For i=0 To Len1 Ld(i,0)=i Next For j=0 To Len2 Ld(0,j)=j Next For i=1 To Len1 For j=1 To Len2 s1$ = Mid(String1,i,1) s2$ = Mid(String2,j,1) If s1 = s2 Then Cost=0 Else Cost=1 End If Ld(i,j) = Min( Min( Ld(i-1,j)+1 , Ld(i,j-1)+1 ), Ld(i-1,j-1)+Cost) ;deletion, insertion, substitution Next Next Return Ld(Len1, Len2) End Function |
Comments
| ||
the Min(float1, float2) is the classical one:Function Min#(a#, b#) If a<b Then Return a Else Return b End If End Function (forgot to include it, sorry) Juan |
| ||
Lovely application of dynamic programming. Well done! |
| ||
oops! didn't saw this one!: http://blitzbasic.com/codearcs/codearcs.php?code=2439 Juan |
| ||
I thought I'd already done this! Oh well, it's a fun programming exercise. |
Code Archives Forum