-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditDistance.bas
More file actions
303 lines (274 loc) · 6.42 KB
/
editDistance.bas
File metadata and controls
303 lines (274 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=StaticCode
Version=6.51
@EndOfDesignText@
'Static code module
Sub Process_Globals
Private fx As JFX
Private a(,) As Int
Private str1 As String
Private str2 As String
End Sub
Sub compareStrings
If str1.Length<str2.Length Then
Dim tmp As String
tmp=str1
str1=str2
str2=tmp
End If
End Sub
Sub showDiff(source1 As String,source2 As String,fontsize As Int) As String
str1=source1
str2=source2
calculateEditDistance
Dim result As Map=interpret(revealOperation)
Return genHtmlResult(TextInParts(result),fontsize)
End Sub
Sub getTextInParts(source1 As String,source2 As String) As Map
str1=source1
str2=source2
calculateEditDistance
Dim result As Map=interpret(revealOperation)
Return TextInParts(result)
End Sub
Public Sub getSimilarity(source1 As String,source2 As String) As Double
str1=source1
str2=source2
Dim result As Double
result=1-calculateEditDistance/Max(str1.Length,str2.Length)
Dim str As String
str=result
Dim su As ApacheSU
str=su.Left(str,4)
result=str
Return result
End Sub
Sub calculateEditDistance As Int
compareStrings
Dim maxLength As Int
maxLength=Max(str1.Length,str2.Length)
'int
Dim a(str1.Length+1,str2.Length+1) As Int 'str1是放在上面的,影响列
a(0,0)=0
For i=0 To str1.Length-1
a(i+1,0)=a(i,0)+1
Next
For i=0 To str2.Length-1
a(0,i+1)=a(0,i)+1
Next
'dp
Dim temp As Int
For j=1 To str2.Length
For i=1 To str1.Length
'Log(i&" "&j)
If str1.CharAt(i-1)<>str2.CharAt(j-1) Then
temp=1
Else
temp=0
End If
a(i,j)=Min(a(i-1,j-1)+temp,Min(a(i,j-1)+1,a(i-1,j)+1))
Next
Next
Dim content As String
For j=0 To str2.Length
Dim row As String
For i=0 To str1.Length
If i=0 Then
row=a(i,j)
Else
row=row&","&a(i,j)
End If
Next
content=content&row&CRLF
Next
Return a(str1.Length,str2.Length)
End Sub
Sub revealOperation As List
Dim maxLength As Int
maxLength=Max(str1.Length,str2.Length)
Dim list1 As List
list1.Initialize
Dim x,y As Int
x=str1.Length
y=str2.Length
For i=maxLength To 0 Step -1
Dim map1 As Map
map1=getWayAndPos(x,y)
list1.Add(map1)
x=map1.Get("x")
y=map1.Get("y")
Next
Return list1
End Sub
Sub getWayAndPos(x As Int,y As Int) As Map
Dim left,diagonal,up As Int
Dim way As String
Dim map1 As Map
map1.Initialize
If x-1=-1 And y-1<>-1 Then
way="up"
y=y-1
map1.Put("way",way)
map1.Put("x",x)
map1.Put("y",y)
Return map1
End If
If y-1=-1 And x-1<>-1 Then
way="left"
x=x-1
map1.Put("way",way)
map1.Put("x",x)
map1.Put("y",y)
Return map1
End If
If y-1=-1 And x-1=-1 Then
way="end"
map1.Put("way",way)
map1.Put("x",x)
map1.Put("y",y)
Return map1
End If
left=a(x-1,y)
diagonal=a(x-1,y-1)
up=a(x,y-1)
'优先级按照左上角、上边、左边的顺序
If left<up Then
If left<diagonal Then
way="left"
End If
End If
If up<left Then
If up<diagonal Then
way="up"
End If
End If
If left=up Then
way="up"
End If
If diagonal<=left And diagonal<=up Then
way="diagonal"
End If
Select way
Case "up"
y=y-1
Case "left"
x=x-1
Case "diagonal"
x=x-1
y=y-1
End Select
map1.Put("way",way)
map1.Put("x",x)
map1.Put("y",y)
Return map1
End Sub
Sub interpret(list1 As List) As Map
Dim add,del,substitute,diff As String
Dim diffList,diffPosList,addList,addPosList As List
diffList.Initialize
addList.Initialize
addPosList.Initialize
diffPosList.Initialize
For Each map1 As Map In list1
Dim text As String
If map1.Get("way")="up" Then
'Log("Add "&str2.CharAt(map1.Get("y")))
add=add&str2.CharAt(map1.Get("y"))
addPosList.InsertAt(0,map1.Get("y"))
text=str2.CharAt(map1.Get("y"))
addList.InsertAt(0,text)
Else if map1.Get("way")="left" Then
'Log("Del "&str1.CharAt(map1.Get("x")))
del=del&str1.CharAt(map1.Get("x"))
diff=diff&str1.CharAt(map1.Get("x"))
text=str1.CharAt(map1.Get("x"))
diffList.InsertAt(0,text)
diffPosList.InsertAt(0,map1.Get("x"))
Else if map1.Get("way")="diagonal" Then
If str1.CharAt(map1.Get("x"))<>str2.CharAt(map1.Get("y")) Then
'Log("substitute "&str1.CharAt(map1.Get("x")))
substitute=substitute&str1.CharAt(map1.Get("x"))
diff=diff&str1.CharAt(map1.Get("x"))
text=str1.CharAt(map1.Get("x"))
diffList.InsertAt(0,text)
diffPosList.InsertAt(0,map1.Get("x"))
'Log("Switch "&TextField2.Text.CharAt(map1.Get("y")))
End If
End If
Next
Dim su As ApacheSU
add=su.Reverse(add)
diff=su.Reverse(diff)
del=su.Reverse(del)
substitute=su.Reverse(substitute)
'Log(add&del&substitute)
Dim result As Map
result.Initialize
result.Put("addList",addList)
result.Put("diffList",diffList)
result.Put("addPosList",addPosList)
result.Put("diffPosList",diffPosList)
Return result
End Sub
Sub genHtmlResult(strs As Map,fontsize As Int) As String
Dim str1parts,str2parts As List
str1parts=strs.Get("str1")
str2parts=strs.Get("str2")
Dim sb As StringBuilder
sb.Initialize
sb.Append($"<!DOCTYPE HTML><html><head><style type="text/css">p {font-size: ${fontsize}px}</style></head><body>"$)
sb.Append("<p>")
For Each part As String In str1parts
sb.Append(part)
Next
sb.Append("</p><p>")
sb.Append("<p>")
For Each part As String In str2parts
sb.Append(part)
Next
sb.Append("</p>")
sb.Append("</body></html>")
Return sb.ToString
End Sub
Sub TextInParts(result As Map) As Map
Dim addList,diffList,addPosList,diffPosList As List
addList=result.Get("addList")
diffList=result.Get("diffList")
addPosList=result.Get("addPosList")
diffPosList=result.Get("diffPosList")
Dim str1Parts As List
str1Parts.Initialize
For i=0 To str1.Length-1
Dim Text As String
Text=str1.CharAt(i)
If diffList.IndexOf(Text)<>-1 And diffList.Size<>0 Then
If diffPosList.Get(diffList.IndexOf(Text))=i Then
diffPosList.RemoveAt(diffList.IndexOf(Text))
diffList.RemoveAt(diffList.IndexOf(Text))
Text="<font color="&Chr(34)&"red"&Chr(34)&">"&Text&"</font>"
End If
End If
str1Parts.add(Text)
Next
Dim str2Parts As List
str2Parts.Initialize
For i=0 To str2.Length-1
Dim Text As String
Text=str2.CharAt(i)
If addList.IndexOf(Text)<>-1 And addList.Size<>0 Then
If addPosList.Get(addList.IndexOf(Text))=i Then
addPosList.RemoveAt(addList.IndexOf(Text))
addList.RemoveAt(addList.IndexOf(Text))
Text="<font color="&Chr(34)&"green"&Chr(34)&">"&Text&"</font>"
End If
End If
str2Parts.Add(Text)
Next
Dim strs As Map
strs.Initialize
strs.Put("str1",str1Parts)
strs.Put("str2",str2Parts)
Return strs
End Sub