StrDistance

<< Click to Display Table of Contents >>

Navigation:  ThinBASIC Core Language > BuiltIn Functions > String functions >

StrDistance

 

Description

 

Levenshtein string distance algorithm (string similarity test)

Levenshtein distance is a measure of the similarity between two strings-

 

Syntax

 

n = StrDistance(Str1, Str2 [, CaseSensitive])

 

 

Returns

 

Number

 

Parameters

 

Name

Type

Optional

Meaning

Str1

String

No

String 1

Str2

String

No

String 1

CaseSensitive

Number

Yes

%TRUE or %FALSE if comparison has to consider letter case.

Default value is %FALSE.

 

Remarks

 

The distance is the number of deletions, insertions, or substitutions required to transform Str1 into Str2.

 

For example, If Str1 is "test" and t is "test", then StrDistance(s,t) = 0, because no transformations are needed. The strings are already identical.

If Str1 is "test" and Str2 is "tent", then StrDistance(s,t) = 1, because one substitution (change "s" to "n") is sufficient to transform Str1 into Str2.

 

The greater the Levenshtein distance, the more different the strings are.

 

Levenshtein distance name comes from the Russian scientist Vladimir Levenshtein, who devised the algorithm in 1965.

The metric is also sometimes called edit distance.

 

Restrictions

 

See also

 

https://en.wikipedia.org/wiki/Levenshtein_distance

 

Examples

 

 

Uses "Console"
 
'---Test the StrDistance function
Sub TestStrDistance()
  Dim str1 As String = "kitten"
  Dim str2 As String = "sitting"
  
  Integer distance = StrDistance(str1, str2)
  
  Printl $("Levenshtein distance between [{str1}] and [{str2}] is: "), distance
End Sub
 
'---Run the test
TestStrDistance()
WaitKey