Levenshtein distance: Difference between revisions
m
→{{header|FutureBasic}}
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 2,155:
=={{header|FutureBasic}}==
local fn LevenshteinDistance(
▲<syntaxhighlight lang="futurebasic">window 1
NSInteger result
▲local fn LevenshteinDistance( aStr as Str255, bStr as Str255 ) as long
// If strings are equal, Levenshtein distance is 0
if ( fn StringIsEqual( s1, s2 ) ) then result = 0 : exit fn
// If either string is empty, then distance is the length of the other string.
for i = 0 to m▼
if (
if ( len(s2) == 0) then result = len(s1) : exit fn
// The remaining recursive process uses first characters and remainder of each string.
CFStringRef s1First = fn StringSubstringToIndex( s1, 1 )
CFStringRef s2First = fn StringSubstringToIndex( s2, 1 )
CFStringRef s1Rest = mid( s1, 1, len(s1) -1 )
CFStringRef s2Rest = mid( s2, 1, len(s2) -1 )
// If leading characters are the same, then distance is that between the rest of the strings.
if fn StringIsEqual( s1First, s2First ) then result = fn LevenshteinDistance( s1Rest, s2Rest ) : exit fn
// Find the distances between sub strings.
NSInteger distA = fn LevenshteinDistance( s1Rest, s2 )
NSInteger distB = fn
NSInteger distC = fn LevenshteinDistance( s1Rest, s2Rest )
// Return the minimum distance between substrings.
NSInteger minDist = distA
if ( distB < minDist )
if ( distC < minDist ) then minDist = distC
result = minDist + 1 // Include change for the first character.
end fn = result
NSInteger i
testStr( 0, 0 ) = "kitten" : testStr( 0, 1 ) = "sitting"▼
testStr( 2, 0 ) = "Saturday" : testStr( 2, 1 ) = "Sunday"▼
testStr( 3, 0 ) = "FutureBasic" : testStr( 3, 1 ) = "FutureBasic"▼
▲testStr( 0, 0 ) = @"kitten" : testStr( 0, 1 ) = @"sitting"
testStr( 1,
testStr( 2,
testStr( 6, 0 ) = @"rave" : testStr( 6, 1 ) = @"grave"
print @"1st string = "; testStr( i, 0 )
print @"2nd string = "; testStr( i, 1 )
print @"Levenshtein distance = "; fn LevenshteinDistance( testStr( i, 0 ), testStr( i, 1 ) )
print
next
NSLog( @"%@", fn WindowPrintViewString( 1 ) )
HandleEvents</syntaxhighlight>▼
HandleEvents
{{output}}
<pre>
1st string = kitten
Line 2,226 ⟶ 2,229:
Levenshtein distance = 0
1st string =
2nd string =
Levenshtein distance =
1st string = black
2nd string = slack
Levenshtein distance = 1
1st string = rave
2nd string = grave
Levenshtein distance = 1
</pre>
|