-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTM.bas
573 lines (511 loc) · 15.8 KB
/
TM.bas
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=6.51
@EndOfDesignText@
Sub Class_Globals
Private fx As JFX
Public translationMemory As TMDB
Public externalTranslationMemory As TMDB
Private sharedTM As ClientKVS
Private similarityStore As Map
Public currentSource As String
Private projectName As String
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(projectPath As String,sourceLang As String,targetLang As String)
translationMemory.Initialize(File.Combine(projectPath,"TM"),"TM.db",sourceLang,targetLang)
externalTranslationMemory.Initialize(File.Combine(projectPath,"TM"),"externalTM.db",sourceLang,targetLang)
similarityStore.Initialize
initSharedTM(projectPath)
End Sub
Public Sub initSharedTM(projectPath As String)
If Main.currentProject.settings.GetDefault("sharingTM_enabled",False)=True Then
projectName=File.GetName(projectPath)
Log("projectName"&projectName)
Dim address As String=Main.currentProject.settings.GetDefault("server_address","http://127.0.0.1:51042")
Dim key As String
Dim configPath As String=File.Combine(projectPath,"config")
If File.Exists(configPath,"accesskey") Then
key=File.ReadString(configPath,"accesskey")
Else
key="put your key in this file"
End If
sharedTM.Initialize(Me, "sharedTM", address,File.Combine(projectPath,"TM"),"sharedTM.db",key)
sharedTM.SetAutoRefresh(Array(projectName&"TM"), 0.1) 'auto refresh every 0.1 minute
Dim job As HttpJob
job.Initialize("job",Me)
If address.EndsWith("/")=False Then
address=address&"/"
End If
job.Download(address&"getinfo?type=size&user="&projectName&"TM")
wait for (job) JobDone(job As HttpJob)
If job.Success Then
Try
Dim size As Int=job.GetString
If size=0 Then
fillSharedTM
End If
Catch
Log(LastException)
End Try
End If
job.Release
End If
End Sub
Public Sub changedRefreshStatus(status As Boolean)
If sharedTM.IsInitialized Then
sharedTM.changedRefreshStatus(status)
End If
End Sub
private Sub fillSharedTM
'progressDialog.Show("Filling SharedTM","sharedTM")
Dim tmmap As Map
tmmap=sharedTM.GetAll(projectName&"TM")
'Dim size As Int=translationMemory.ListKeys.Size
Dim index As Int=0
Dim toAddMap As Map
toAddMap.Initialize
For Each key As String In translationMemory.ListKeys
index=index+1
Sleep(0)
'progressDialog.update(index,size)
If tmmap.ContainsKey(key) Then
Dim localTargetMap As Map=translationMemory.Get(key)
Dim sharedTMTargetMap As Map=tmmap.Get(key)
Dim localCreatedTime,sharedTMCreatedTime As Long
localCreatedTime=localTargetMap.Get("createdTime")
sharedTMCreatedTime=sharedTMTargetMap.Get("createdTime")
If localTargetMap.Get("text")<>sharedTMTargetMap.Get("text") And sharedTMCreatedTime<localCreatedTime Then
toAddMap.Put(key,translationMemory.Get(key))
End If
Else
toAddMap.Put(key,translationMemory.Get(key))
End If
Next
fillALL(toAddMap)
'progressDialog.close
End Sub
private Sub fillALL(toAddMap As Map)
For Each key As String In toAddMap.Keys
Sleep(0)
sharedTM.Put(projectName&"TM",key,toAddMap.Get(key))
Next
End Sub
private Sub sharedTM_NewData(changedItems As List)
Log("changed"&changedItems)
Dim changedKeys As List
changedKeys.Initialize
Dim map1 As Map=sharedTM.GetAll(projectName&"TM")
For Each item1 As Item In changedItems
If item1.UserField=projectName&"TM" Then
If item1.ValueField=Null Then 'remove
sharedTM.removeLocal(item1.UserField,item1.KeyField)
If translationMemory.ContainsKey(item1.KeyField) Then
translationMemory.Remove(item1.KeyField)
End If
Continue
End If
If translationMemory.ContainsKey(item1.KeyField) Then
Dim previousTargetMap,newTargetMap As Map
previousTargetMap=translationMemory.Get(item1.KeyField)
newTargetMap=map1.Get(item1.KeyField)
Dim previousCreatedTime,newCreatedTime As Long
previousCreatedTime=previousTargetMap.Get("createdTime")
newCreatedTime=newTargetMap.Get("createdTime")
If newCreatedTime>previousCreatedTime Then
translationMemory.Put(item1.KeyField,map1.Get(item1.KeyField))
changedKeys.Add(item1.KeyField)
End If
Else
translationMemory.Put(item1.KeyField,map1.Get(item1.KeyField))
changedKeys.Add(item1.KeyField)
End If
End If
Next
Main.currentProject.saveNewDataToWorkfile(changedKeys)
End Sub
public Sub close
If translationMemory.IsInitialized Then
translationMemory.Close
externalTranslationMemory.Close
End If
End Sub
public Sub addPair(source As String,targetMap As Map)
Dim target As String
target=targetMap.Get("text")
Dim note As String
note=targetMap.Get("note")
Dim createdTime As Long
createdTime=targetMap.Get("createdTime")
If translationMemory.ContainsKey(source) Then
Dim previousTargetMap As Map
previousTargetMap=translationMemory.Get(source)
Dim previousCreatedTime As Long=previousTargetMap.GetDefault("createdTime",0)
If previousTargetMap.Get("text")=target Then
If previousTargetMap.GetDefault("note","")=note Then
Return
End If
End If
If previousCreatedTime>createdTime Then
Return
End If
End If
translationMemory.Put(source,targetMap)
addPairToSharedTM(source,targetMap)
End Sub
Public Sub addPairToSharedTM(source As String,targetMap As Map)
If Main.currentProject.settings.GetDefault("sharingTM_enabled",False)=True Then
sharedTM.Put(projectName&"TM",source,targetMap)
End If
End Sub
Public Sub removeFromSharedTM(source As String)
If Main.currentProject.settings.GetDefault("sharingTM_enabled",False)=True Then
sharedTM.Put(projectName&"TM", source, Null) ' this equals remove method
End If
End Sub
Public Sub deleteExternalTranslationMemory
externalTranslationMemory.DeleteAll
End Sub
Public Sub importExternalTranslationMemory(tmList As List,projectFile As Map,quickMode As Boolean) As ResumableSub
progressDialog.Show("Loading external memory","loadtm")
Dim segments As List
segments.Initialize
For Each tmfile As String In tmList
Dim tmfileLowercase As String
tmfileLowercase=tmfile.ToLowerCase
If tmfileLowercase.EndsWith(".txt") Then
segments.AddAll(importedTxt(tmfile))
Else if tmfileLowercase.EndsWith(".tmx") Then
Dim importer As TMXImporter
importer.Initialize
segments.AddAll(importer.importedList(File.Combine(Main.currentProject.path,"TM"),tmfile,projectFile.Get("source"),projectFile.Get("target"),quickMode))
else if tmfileLowercase.EndsWith(".xlsx") Then
segments.AddAll(importedXlsx(tmfile))
End If
Next
'Log(segments)
Dim tmToBeImported As Map
tmToBeImported.Initialize
If segments.Size<>0 Then
Dim index As Int=0
For Each bitext As List In segments
index=index+1
If index Mod 5 = 0 Then
progressDialog.update(index,segments.Size)
Sleep(0)
End If
Dim source,target,filename As String
Dim targetMap As Map
targetMap.Initialize
If bitext.Size>=3 Then
source=bitext.get(0)
target=bitext.Get(1)
filename=bitext.Get(2)
If bitext.Size=4 Then
targetMap=bitext.Get(3)
End If
Else
Continue
End If
targetMap.Put("text",target)
targetMap.Put("filename",filename)
tmToBeImported.put(source,targetMap)
Next
End If
'Log(externalTranslationMemory.ListKeys.Size)
progressDialog.update2("Inserting to database...")
wait for (externalTranslationMemory.PutMapAsync(tmToBeImported)) Complete (done As Object)
progressDialog.close
Return True
End Sub
private Sub importedTxt(filename As String) As List
Dim content As String
content=File.ReadString(File.Combine(Main.currentProject.path,"TM"),filename)
Dim segments As List
segments=Regex.Split(CRLF,content)
Dim result As List
result.Initialize
For Each line As String In segments
Dim bitext As List
bitext.Initialize
Dim source,target As String
source=Regex.Split(" ",line)(0)
target=Regex.Split(" ",line)(1)
bitext.Add(source)
bitext.Add(target)
Dim targetMap As Map
targetMap.Initialize
Try
Dim creator As String
creator=Regex.Split(" ",line)(2)
targetMap.Put("creator",creator)
Dim creationdate As String
creationdate=Regex.Split(" ",line)(3)
targetMap.Put("createdTime",creationdate)
Dim note As String
note=Regex.Split(" ",line)(4)
targetMap.Put("note",note)
Catch
Log(LastException)
End Try
bitext.Add(filename)
bitext.Add(targetMap)
result.Add(bitext)
Next
Return result
End Sub
private Sub importedXlsx(filename As String) As List
Dim result As List
result.Initialize
Dim wb As PoiWorkbook
wb.InitializeExisting(File.Combine(Main.currentProject.path,"TM"),filename,"")
Dim sheet1 As PoiSheet = wb.GetSheet(0)
For Each row As PoiRow In sheet1.rows
Dim bitext As List
bitext.Initialize
Dim source,target As String
source=row.GetCell(0).ValueString
target=row.GetCell(1).ValueString
bitext.Add(source)
bitext.Add(target)
Dim targetMap As Map
targetMap.Initialize
Try
Dim creator As String
creator=row.GetCell(2).ValueString
If creator<>"" Then
targetMap.Put("creator",creator)
End If
Dim creationdate As String
creationdate=row.GetCell(3).ValueString
If creationdate<>"" Then
targetMap.Put("createdTime",creationdate)
End If
Dim note As String
note=row.GetCell(4).ValueString
If note<>"" Then
targetMap.Put("note",note)
End If
Catch
Log(LastException)
End Try
bitext.Add(filename)
bitext.Add(targetMap)
result.Add(bitext)
Next
Return result
End Sub
Sub getMatchListMerged(source As String,matchrate As Double,getOne As Boolean,limit As Int) As ResumableSub
Dim result As List
result.Initialize
wait for (getMatchList(False,source,matchrate,getOne,limit)) Complete (matchList As List)
result.AddAll(matchList)
If getOne And matchList.Size>0 Then
Return result
End If
wait for (getMatchList(True,source,matchrate,getOne,limit)) Complete (matchList As List)
result.AddAll(matchList)
Return result
End Sub
Sub getMatchList(external As Boolean, source As String,matchrate As Double,getOne As Boolean,limit As Int) As ResumableSub
Dim matchList As List
matchList.Initialize
Dim kvs As TMDB
If external=False Then
kvs=translationMemory
Else
kvs=externalTranslationMemory
End If
Dim matchedMap As Map
If kvs.ContainsKey(source) And getOne Then
matchedMap.Initialize
matchedMap.Put(source,kvs.Get(source))
Else if matchrate<1 Then 'fuzzy match
wait for (kvs.GetMatchedMapAsync(source,True,False,limit)) Complete (resultMap As Map)
matchedMap=resultMap
Else
Return matchList
End If
'Log(source)
'Log(matchedMap)
source=source.ToLowerCase.Trim
For Each key As String In matchedMap.Keys
'Sleep(0)
Dim lowerCased As String=key.ToLowerCase.Trim
If basicCompare(source,lowerCased)=False Then
Continue
End If
Dim similarity As Double
If lowerCased=source Then 'exact match
similarity=1.0
Else
Dim joined As String=source&" "&lowerCased
If similarityStore.ContainsKey(joined) Then
similarity=similarityStore.Get(joined)
Else
wait for (getSimilarityFuzzyWuzzy(source,lowerCased)) Complete (Result As Double)
similarity=Result
similarityStore.Put(joined,similarity)
End If
End If
'Log(source)
'Log(lowerCased)
'Log(similarity)
'Log(matchrate)
If similarity>=matchrate Then
Dim tmPairList As List
tmPairList.Initialize
tmPairList.Add(similarity)
tmPairList.Add(key)
Dim target As String
Dim targetMap As Map
targetMap=kvs.Get(key)
target=targetMap.Get("text")
tmPairList.Add(target)
If external=False Then
tmPairList.Add(targetMap.GetDefault("creator","anonymous"))
Else
tmPairList.Add(targetMap.Get("filename")) ' external tm name
End If
'Log(tmPairList)
matchList.Add(tmPairList)
If getOne Then
Return matchList
End If
End If
Next
Return subtractedAndSortMatchList(matchList,4)
End Sub
Sub getMatchListOld(source As String) As ResumableSub
Dim matchList As List
matchList.Initialize
Dim matchrate As Double
If Main.currentProject.settings.ContainsKey("matchrate") Then
matchrate=Main.currentProject.settings.Get("matchrate")
Else
matchrate=0.5
End If
For i=0 To 1
If i=0 Then
Dim kvs As TMDB
kvs=translationMemory
Else
Dim kvs As TMDB
kvs=externalTranslationMemory
End If
For Each key As String In kvs.ListKeys
Sleep(0)
If basicCompare(source,key)=False Then
Continue
End If
Dim similarity As Double
If key=source Then 'exact match
similarity=1.0
Else
If similarityStore.ContainsKey(source&" "&key) Then
similarity=similarityStore.Get(source&" "&key)
Else
wait for (getSimilarityFuzzyWuzzy(source,key)) Complete (Result As Double)
similarity=Result
similarityStore.Put(source&" "&key,similarity)
End If
End If
If similarity>matchrate Then
Dim tmPairList As List
tmPairList.Initialize
tmPairList.Add(similarity)
tmPairList.Add(key)
Dim target As String
Dim targetMap As Map
targetMap=kvs.Get(key)
target=targetMap.Get("text")
tmPairList.Add(target)
If i=0 Then
tmPairList.Add(targetMap.GetDefault("creator","anonymous"))
Else
tmPairList.Add(targetMap.Get("filename")) ' external tm name
End If
'Log(tmPairList)
matchList.Add(tmPairList)
End If
Next
Next
Return subtractedAndSortMatchList(matchList,4)
End Sub
Sub getOneUseMemory(source As String,rate As Double,limit As Int) As ResumableSub
Dim onePairList As List
wait for (getMatchListMerged(source,rate,True,limit)) Complete (matchList As List)
If matchList.Size=0 Then
Return Null
End If
onePairList=matchList.Get(0)
Return onePairList
End Sub
Sub basicCompare(str1 As String,str2 As String) As Boolean
Dim temp As String
If str1.Length>str2.Length Then
temp=str1
str1=str2
str2=temp
End If
If str1.Length-str2.Length>str2.Length/2 Then
Return False
Else
Return True
End If
End Sub
Sub subtractedAndSortMatchList(matchList As List,maxNum As Int) As List
If matchList.Size<=1 Then
Return matchList
End If
Dim newlist As List
newlist.Initialize
Dim sortedList As List
sortedList=BubbleSort(matchList)
For i=0 To Min(maxNum,sortedList.Size-1)
newlist.Add(sortedList.Get(i))
Next
Return newlist
End Sub
Sub BubbleSort(matchList As List) As List
For j=0 To matchList.Size-1
For i = 1 To matchList.Size - 1
If NextIsMoreSimilar(matchList.Get(i),matchList.Get(i-1)) Then
matchList=Swap(matchList,i, i-1)
End If
Next
Next
Return matchList
End Sub
Sub Swap(matchList As List,index1 As Int, index2 As Int) As List
Dim temp As List
temp = matchList.Get(index1)
matchList.Set(index1,matchList.Get(index2))
matchList.Set(index2,temp)
Return matchList
End Sub
Sub NextIsMoreSimilar(list2 As List,list1 As List) As Boolean
'list2 is the next
If list2.Get(0)>list1.Get(0) Then
Return True
Else
Return False
End If
End Sub
Sub getSimilarityFuzzyWuzzy(str1 As String,str2 As String) As ResumableSub
str1=Regex.Replace2("<.*?>",32,str1," ") 'replace tags to space
str2=Regex.Replace2("<.*?>",32,str2," ") 'replace tags to space
Dim result As Double
Dim jo As JavaObject
result=jo.InitializeStatic("me.xdrop.fuzzywuzzy.FuzzySearch").RunMethod("ratio",Array As String(str1,str2))
result=result/100
Return result
End Sub
Sub getExternalMemorySize As Int
Return externalTranslationMemory.ListKeys.Size
End Sub
Sub getProjectMemorySize As Int
Return externalTranslationMemory.ListKeys.Size+translationMemory.ListKeys.Size
End Sub