Jump to content

Levenshtein distance: Difference between revisions

imported>Lacika7
No edit summary
imported>Thebeez
Line 6,216:
</syntaxhighlight>
 
=={{header|uBasic/4tH}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="ubasic-4th">
' Uses the "iterative with two matrix rows" algorithm
' referred to in the Wikipedia article.
 
' create two integer arrays of distances
Dim @u(128) ' previous
Dim @v(128) ' current
 
Print "'kitten' to 'sitting' => ";
Print FUNC(_Levenshtein (Dup ("kitten"), Dup ("sitting")))
 
Print "'rosettacode' to 'raisethysword' => ";
Print FUNC(_Levenshtein (Dup ("rosettacode"), Dup ("raisethysword")))
 
Print "'sleep' to 'fleeting' => ";
Print FUNC(_Levenshtein (Dup ("sleep"), Dup ("fleeting")))
 
End
 
_Levenshtein
Param (2)
Local (3)
 
' degenerate cases
If Comp(a@, b@) = 0 Then Return (0)
If Len(a@) = 0 Then Return (Len(b@))
If Len(b@) = 0 Then Return (Len(a@))
 
' initialize v0
For c@ = 0 To Len(b@)
@u(c@) = c@
Next
 
For c@ = 0 To Len(a@) - 1
' calculate v1 from v0
@v(0) = c@ + 1
For d@ = 0 To Len(b@) - 1
e@ = IIf(Peek (a@, c@) = Peek (b@, d@), 0, 1)
@v(d@ + 1) = min(@v(d@) + 1, Min(@u(d@ + 1) + 1, @u(d@) + e@))
Next
 
' copy @v() to @u() for next iteration
For d@ = 0 To Len(b@)
@u(d@) = @v(d@)
Next
Next
 
Return (@v(Len(b@)))
</syntaxhighlight>
=={{header|Vala}}==
<syntaxhighlight lang="vala">class LevenshteinDistance : Object {
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.