Removed
Posted: Mon Feb 18, 2008 12:29 am
Removed
Code: Select all
Procedure.l Minimum(a.l,b.l,c.l)
;*******************************
;*** Get minimum of three values
;*******************************
mi.l=0
mi = a
If b < mi
mi = b
EndIf
If c < mi
mi = c
EndIf
ProcedureReturn mi
EndProcedure
Procedure.l Levenshtein(s$,t$)
;********************************
;*** Compute Levenshtein Distance
;********************************
m.l = 0; length of t
n.l = 0;length of s
i.l=0 ; iterates through s
j.l=0 ; iterates through t
s_i.s ="" ; ith character of s
t_j.s = ""; jth character of t
cost.l=0 ; cost
; Step 1
n = Len(s$)
m = Len(t$)
Dim d(n,m); matrix
If n = 0
LD = m
ProcedureReturn ld
EndIf
If m = 0
LD = n
ProcedureReturn ld
EndIf
; Step 2
For i = 0 To n
d(i, 0) = i
Next
For j = 0 To m
d(0, j) = j
Next
;Step 3
For i = 1 To n
s_i = Mid(s$, i, 1)
; Step 4
For j = 1 To m
t_j = Mid(t$, j, 1)
; Step 5
If s_i = t_j
cost = 0
Else
cost = 1
EndIf
; Step 6
d(i, j) = Minimum(d(i - 1, j) + 1, d(i, j - 1) + 1, d(i - 1, j - 1) + cost)
Next
Next
; Step 7
LD = d(n, m)
ProcedureReturn ld
EndProcedure
Debug Levenshtein("Robert","roberT")
Dr_Wildrick wrote:I have an ex-wife and two teenage daughters who makes sure I am aware of that fact on an a regular basis.)
Code: Select all
Procedure Distance(s$,t$)
m=Len(s$)
n=Len(t$)
If m And n
If m>#MaxWordLength
m=#maxWordLength
EndIf
If n>#MaxWordLength
n=#maxWordLength
EndIf
For i=0 To m
DistanceMatrix(i,0)=i
Next
For j=0 To n
DistanceMatrix(0,j)=j
Next
For i=1 To m
For j=1 To n
If Mid(s$,i,1)=Mid(t$,j,1)
cost=#False
Else
cost=#True
EndIf
a=DistanceMatrix(i-1,j)+1 ; deletion
b=DistanceMatrix(i,j-1)+1 ; insertion
c=DistanceMatrix(i-1,j-1)+cost ; substitution
If b<a
a=b
EndIf
If c<a
a=c
EndIf
DistanceMatrix(i,j)=a
Next
Next
result=DistanceMatrix(m,n)
Else
result=9999
EndIf
ProcedureReturn result
EndProcedure
Code: Select all
Procedure.s MidMem(*MainMem, StartPos.l, GetLen.l)
If *MainMem = 0 Or GetLen = 0
ProcedureReturn ""
EndIf
*RetMem = AllocateMemory(GetLen)
CopyMemory(*MainMem + StartPos -1, *RetMem, GetLen)
ReturnString.s = PeekS(*RetMem,GetLen)
FreeMemory(*RetMem)
ProcedureReturn ReturnString
EndProcedure
Procedure.l Minimum(a.l,b.l,c.l)
;*******************************
;*** Get minimum of three values
;*******************************
mi.l=0
mi = a
If b < mi
mi = b
EndIf
If c < mi
mi = c
EndIf
ProcedureReturn mi
EndProcedure
Procedure.l Levenshtein(pStr1.l, StrLen1.l, pStr2.l, StrLen2.l)
;********************************
;*** Compute Levenshtein Distance
;********************************
S1_i.s = "" ; ith character of s
S2_j.s = "" ; jth character of t
cost.l=0 ; cost
; Step 1
Dim d.l(StrLen1,StrLen2); matrix
If StrLen1 = 0
ProcedureReturn StrLen2
EndIf
If StrLen2 = 0
ProcedureReturn StrLen1
EndIf
; Step 2
For i = 0 To StrLen1
d(i, 0) = i
Next
For j = 0 To StrLen2
d(0, j) = j
Next
;Step 3
For i = 1 To StrLen1
S1_i = MidMem(pStr1, i, 1)
; Step 4
For j = 1 To StrLen2
S2_j = MidMem(pStr2, j, 1)
; Step 5
If S1_i = S2_j
cost = 0
Else
cost = 1
EndIf
; Step 6
d(i, j) = Minimum(d(i - 1, j) + 1, d(i, j - 1) + 1, d(i - 1, j - 1) + cost)
Next
Next
; Step 7
ProcedureReturn d(StrLen1, StrLen2)
EndProcedure
DisableDebugger
Str1.s = "Heello"
Str2.s = "hellddo"
Ans1 = Levenshtein(@Str1,Len(Str1),@Str2,Len(Str2))
File1.s = OpenFileRequester("File 1","","",0)
File2.s = OpenFileRequester("File 2","","",0)
If Len(File1) > 1 And Len(file2) > 1
OpenFile(1,File1)
FileLen1 = Lof(1)
pFile1Data = AllocateMemory(FileLen1)
ReadData(1, pFile1Data, FileLen1)
CloseFile(1)
OpenFile(2,File2)
FileLen2 = Lof(2)
pFile2Data = AllocateMemory(FileLen2)
ReadData(2, pFile1Data, FileLen2)
CloseFile(2)
Ans2 = Levenshtein(pFile1Data,FileLen1 ,pFile2Data,FileLen2)
EndIf
EnableDebugger
Debug ans1
Debug ans2
Please see...Doubledutch, you are missing
Dim DistanceMatrix(m,n)
and the #MaxWordLength
doubledutch wrote:Also note: DistanceMatrix[MaxWordLength,MaxWordLength] should be set up as a global array where MaxWordLength is the max word length!
Code: Select all
#MaxWordLength = 100
Procedure Distance(s$,t$)
m=Len(s$)
n=Len(t$)
Dim DistanceMatrix(m,n)
If m And n
If m>#MaxWordLength
m=#maxWordLength
EndIf
If n>#MaxWordLength
n=#maxWordLength
EndIf
For i=0 To m
DistanceMatrix(i,0)=i
Next
For j=0 To n
DistanceMatrix(0,j)=j
Next
For i=1 To m
For j=1 To n
If Mid(s$,i,1)=Mid(t$,j,1)
cost=#False
Else
cost=#True
EndIf
a=DistanceMatrix(i-1,j)+1 ; deletion
b=DistanceMatrix(i,j-1)+1 ; insertion
c=DistanceMatrix(i-1,j-1)+cost ; substitution
If b<a
a=b
EndIf
If c<a
a=c
EndIf
DistanceMatrix(i,j)=a
Next
Next
result=DistanceMatrix(m,n)
Else
result=9999
EndIf
ProcedureReturn result
EndProcedure
Debug distance("Paul"," Paul")