Stuart McLachlan
stuart at lexacorp.com.pg
Wed Oct 22 07:00:59 CDT 2003
On 21 Oct 2003 at 22:58, MartyConnelly wrote:
> Why not use a weighted checksum like the ISBN number, it will also
> indicated the position of a single substitution error and I think the
> location of a single transposition error.
>
Too many potential false matches
Combining HammingDistance with a straight Checksum seems to work
quite well though. Here's what I've come up with to test for a single
miskey or a transposition error:
Function HammingDistance(BaseString As String, TestString As String) As Long
Dim lngHamCount As Long
Dim lngLen As Long
lngLen = Len(BaseString)
If Len(TestString) <> lngLen Then
HammingDistance = -1
Exit Function
Else
For loopcount = 1 To lngLen
If Mid$(BaseString, loopcount, 1) <> Mid$(TestString, loopcount, 1) Then
lngHamCount = lngHamCount + 1
End If
Next
End If
HammingDistance = lngHamCount
End Function
Function CheckSum(TestString) As Long
Dim lngCheck As Long
For loopcount = 1 To Len(BaseString)
lngCheck = lngCheck + Asc(Mid$(TestString, loopcount, 1))
Next
End Function
Function PossibleMatch(BaseString As String, TestString As String) As Boolean
Select Case HammingDistance(BaseString, TestString)
Case 1 ' Miskey?
PossibleMatch = True
Case 2 'Possible transposition?
If CheckSum(BaseString) = CheckSum(TestString) Then
PossibleMatch = True
End If
Case Else
PossibleMatch = False
End Select
End Function
--
Lexacorp Ltd
http://www.lexacorp.com.pg
Information Technology Consultancy, Software Development,System Support.