-
Notifications
You must be signed in to change notification settings - Fork 0
/
Aligner.b4j
1831 lines (1687 loc) · 49.3 KB
/
Aligner.b4j
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
AppType=JavaFX
Build1=Default,b4j.example
File1=aligner.png
File10=progress.bjl
File11=segment.bjl
File12=segmentationRules.srx
File13=SegmentsRemover.bjl
File14=splitterChooser.bjl
File15=srxPicker.bjl
File16=TextArea.bjl
File17=TwoTextArea.bjl
File2=AssPicker.bjl
File3=BiTextFilesPicker.bjl
File4=clipboardreader.bjl
File5=editorLV.bjl
File6=LangaugePairSelector.bjl
File7=langcodes.txt
File8=languagesWithoutSpace.txt
File9=main.bjl
FileGroup1=Default Group
FileGroup10=Default Group
FileGroup11=Default Group
FileGroup12=Default Group
FileGroup13=Default Group
FileGroup14=Default Group
FileGroup15=Default Group
FileGroup16=Default Group
FileGroup17=Default Group
FileGroup2=Default Group
FileGroup3=Default Group
FileGroup4=Default Group
FileGroup5=Default Group
FileGroup6=Default Group
FileGroup7=Default Group
FileGroup8=Default Group
FileGroup9=Default Group
Group=Default Group
Library1=jcore
Library10=jshell
Library11=byteconverter
Library12=cssutils
Library13=b4xcollections
Library2=jfx
Library3=javaobject
Library4=jxmlsax
Library5=xmlbuilder
Library6=json
Library7=jreflection
Library8=b4jdragtome
Library9=jpoi
Module1=Project
Module10=TMX
Module11=SegmentsViewer
Module12=SegmentsRemover
Module13=LFAlign
Module14=statistics
Module15=XmlParser
Module16=Bleualign
Module17=progressDialog
Module18=openxmlFilter
Module19=xliffFilter
Module2=BiTextFilesPicker
Module20=FileChooserUtils
Module21=workfileFilter
Module22=ClipboardReader
Module23=XmlNode
Module24=XMLUtils
Module25=SimpleTag
Module26=TagKind
Module27=XMLBuilder2
Module28=assFilter
Module29=AssPicker
Module3=SrxPicker
Module30=SplitterChooser
Module31=POIWord
Module32=Map2Xml
Module33=Xml2Map
Module4=LanguagePairSelector
Module5=Utils
Module6=txtFilter
Module7=icu4j
Module8=segmentation
Module9=SRX
NumberOfFiles=17
NumberOfLibraries=13
NumberOfModules=33
Version=7.8
@EndOfDesignText@
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
#AdditionalJar: icu4j
Sub Process_Globals
Private fx As JFX
Public MainForm As Form
Private editorLV As ListView
Private currentProject As Project
Type Range(firstIndex As Int,lastIndex As Int)
Private StatusLabel As Label
Private cursorReachEnd As Boolean=False
Private SegmentsLabel As Label
Private currentIndex As Int
Private previousSegments As List
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("main") 'Load the layout file.
MainForm.Show
addScrollChangedEvent(editorLV)
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub MainForm_Resize (Width As Double, Height As Double)
CallSubDelayed(Me,"ListViewParent_Resize")
End Sub
Sub MenuBar1_Action
Dim mi As MenuItem
mi=Sender
Select mi.Text
Case "_About"
fx.Msgbox(MainForm,"Aligner"&CRLF&"@xulihang 2020","About")
Case "_Exit"
ExitApplication
Case "_New"
If currentProject.IsInitialized Then
fx.Msgbox(MainForm,"Please first close this project","")
Return
End If
newProject
Case "_Open"
openProject
Case "_Save"
saveProject
Case "_Close"
closeProject
Case "separate source file and target file"
If currentProject.IsInitialized Then
Dim filesPicker As BiTextFilesPicker
filesPicker.Initialize
Dim result As Map=filesPicker.ShowAndWait(currentProject.getProjectFileValue("langPair"))
If result.ContainsKey("source")=False Then
Return
End If
currentProject.loadItemsToSegments(result)
loadSegmentsToListView(Null)
End If
Case "bilingual file"
If currentProject.IsInitialized Then
Dim fc As FileChooser
fc.Initialize
'fc.SetExtensionFilter("Bilingual Files",Array As String("*.docx","*.txt","*.tmx","*.xlf","*.json"))
Dim descriptionList,filterList As List
descriptionList.Initialize
filterList.Initialize
descriptionList.AddAll(Array As String("docx file","txt file","tmx file","BasicCAT workfile"))
filterList.AddAll(Array As String("*.docx","*.txt","*.tmx","*.json"))
FileChooserUtils.AddExtensionFilters4(fc,descriptionList,filterList,True,"All",True)
Dim path As String=fc.ShowOpen(MainForm)
Dim result As Map
result.Initialize
Dim langPair As Map
langPair=currentProject.getProjectFileValue("langPair")
If File.Exists(path,"") Then
If path.ToLowerCase.EndsWith(".docx") Then
result=openxmlFilter.getBitext(path,langPair,askHighPrecision)
Else if path.ToLowerCase.EndsWith(".txt") Then
result=txtFilter.getBitext(path,langPair,askHighPrecision)
Else if path.ToLowerCase.EndsWith(".tmx") Then
result=TMX.getBitext(path,langPair)
Else if path.ToLowerCase.EndsWith(".json") Then
result=workfileFilter.getBitext(path)
Else if path.ToLowerCase.EndsWith(".xlf") Then
result=xliffFilter.getBitext(path)
Else
fx.Msgbox(MainForm,"Not supported","")
Return
End If
currentProject.loadItemsToSegments(result)
loadSegmentsToListView(Null)
End If
End If
Case "bilingual ass file"
If currentProject.IsInitialized Then
Dim assFilePicker As AssPicker
assFilePicker.Initialize
Dim result As Map=assFilePicker.ShowAndWait
If result.ContainsKey("source")=False Then
Return
End If
currentProject.loadItemsToSegments(result)
loadSegmentsToListView(Null)
End If
Case "Aligner project"
If currentProject.IsInitialized Then
Dim fc As FileChooser
fc.Initialize
fc.SetExtensionFilter("Aligner projects",Array As String("*.alp"))
Dim path As String=fc.ShowOpen(MainForm)
If File.Exists(path,"") Then
Dim project1 As Project
project1.Initialize(path)
project1.readProjectFile
currentProject.segments.AddAll(project1.segments)
loadSegmentsToListView(Null)
End If
End If
Case "Read from _Clipboard"
Dim langPair As Map
langPair=currentProject.getProjectFileValue("langPair")
Dim creader As ClipboardReader
creader.Initialize(langPair)
Dim result As Map=creader.ShowAndWait
If result.ContainsKey("source")=False Then
Return
End If
currentProject.loadItemsToSegments(result)
loadSegmentsToListView(Null)
Case "DOCX"
exportToDocx
Case "TXT"
exportToTxt
Case "TMX"
exportToTMX
Case "XLSX"
exportToXLSX
Case "XLIFF"
exportToXLIFF
Case "_Segment into sentences"
If currentProject.IsInitialized Then
Dim previousSegs As List=getPreviousSegs
Dim srxfilePicker As SrxPicker
srxfilePicker.Initialize
Dim srxPath As String=srxfilePicker.ShowAndWait
wait for (currentProject.loadSegmentsInSentenceLevel(srxPath)) Complete (temp As Object)
loadSegmentsToListView(previousSegs)
End If
Case "Remove specific segments"
If currentProject.IsInitialized Then
Dim sr As SegmentsRemover
sr.Initialize
sr.Show
End If
Case "Go to the next _empty segment"
GotoTheNextEmptySegment
Case "Go to the next segment with _issues"
GotoTheNextSegmentWithIssues
Case "Count words"
Dim langPair As Map
langPair=currentProject.getProjectFileValue("langPair")
Dim s As statistics
s.Initialize(langPair)
s.MsgBox(MainForm,currentProject.segments)
Case "Clean tags"
CleanTags
Case "Align with LF Aligner"
If currentProject.IsInitialized Then
Dim previousSegs As List=getPreviousSegs
wait for (AutoAlign("lfalign")) Complete (temp As Object)
loadSegmentsToListView(previousSegs)
End If
Case "Align"
If currentProject.IsInitialized Then
Dim previousSegs As List=getPreviousSegs
wait for (AutoAlign("bleualign")) Complete (temp As Object)
loadSegmentsToListView(previousSegs)
End If
Case "Export Source to TXT for Translation"
exportSourceToTxt
End Select
End Sub
Sub GotoTheNextEmptySegment
For index=currentIndex To currentProject.segments.Size-1
Dim segment As Map = currentProject.segments.Get(index)
Dim source,target As String
source=segment.Get("source")
target=segment.Get("target")
If source="" Or target="" Then
editorLV.ScrollTo(Max(0,index-2))
If Abs(currentIndex-index)>20 Then
Sleep(500)
editorLV.ScrollTo(Max(0,index-2))
End If
Return
End If
Next
End Sub
Sub GotoTheNextSegmentWithIssues
For index=currentIndex To currentProject.segments.Size-1
Dim segment As Map = currentProject.segments.Get(index)
If segment.GetDefault("complete",True)=False Or segment.GetDefault("accurate",True)=False Then
editorLV.ScrollTo(Max(0,index-2))
If Abs(currentIndex-index)>20 Then
Sleep(500)
editorLV.ScrollTo(Max(0,index-2))
End If
Return
End If
Next
End Sub
Public Sub RemoveRange(startIndex As Int,EndIndex As Int)
Dim new As List
new.Initialize
For index=0 To currentProject.segments.Size-1
If index<startIndex Or index>EndIndex Then
new.Add(currentProject.segments.Get(index))
End If
Next
Dim previousSegs As List=getPreviousSegs
currentProject.segments.Clear
currentProject.segments.AddAll(new)
loadSegmentsToListView(previousSegs)
End Sub
Public Sub RemoveDuplicate
Dim new As List
new.Initialize
Dim map1 As Map
map1.Initialize
Dim sizeBefore,sizeAfter As Int
sizeBefore=currentProject.segments.Size
For Each segment As Map In currentProject.segments
Dim source As String = segment.Get("source")
Dim target As String = segment.Get("target")
If source="" Then
new.Add(segment)
Continue
End If
If map1.ContainsKey(source) Then
If map1.Get(source)=target Then 'duplicate
Continue
End If
Else
map1.Put(source,target)
End If
new.Add(segment)
Next
sizeAfter=new.Size
Dim previousSegs As List=getPreviousSegs
currentProject.segments.Clear
currentProject.segments.AddAll(new)
loadSegmentsToListView(previousSegs)
fx.Msgbox(MainForm,(sizeAfter-sizeBefore)&" segments removed","")
End Sub
Sub DuplicateRemovedSegments(segments As List) As List
Dim new As List
new.Initialize
Dim map1 As Map
map1.Initialize
For Each segment As Map In segments
Dim source,target As String
source=segment.Get("source")
target=segment.Get("target")
If map1.ContainsKey(source) Then
Continue
Else
new.Add(segment)
map1.Put(source,target)
End If
Next
Return new
End Sub
Public Sub CleanTags
Dim previousSegs As List=getPreviousSegs
For Each segment As Map In currentProject.segments
Dim source,target As String
source=segment.Get("source")
target=segment.Get("target")
segment.Put("source",Regex.Replace("<.*?>",source,""))
segment.Put("target",Regex.Replace("<.*?>",target,""))
Next
loadSegmentsToListView(previousSegs)
End Sub
Public Sub RemoveSegments(segmentsText As List,isSource As Boolean)
Dim sourceList,targetList As List
sourceList.Initialize
targetList.Initialize
For Each segment As Map In currentProject.segments
Dim source,target As String
source=segment.Get("source")
target=segment.Get("target")
If segmentsText.IndexOf(source)=-1 Then
sourceList.Add(source)
End If
If target<>"" Then
targetList.Add(target)
End If
Next
Dim result As Map
result.Initialize
result.Put("source",sourceList)
result.Put("target",targetList)
Dim previousSegs As List=getPreviousSegs
currentProject.loadItemsToSegments(result)
loadSegmentsToListView(previousSegs)
End Sub
Public Sub PreserveOnlyFirstOccurence
Dim sourceSet,targetSet As B4XSet
sourceSet.Initialize
targetSet.Initialize
Dim sizeBefore As Int=currentProject.segments.Size
For Each segment As Map In currentProject.segments
Dim source,target As String
source=segment.Get("source")
target=segment.Get("target")
sourceSet.Add(source.Trim)
targetSet.Add(target.Trim)
Next
Dim result As Map
result.Initialize
result.Put("source",sourceSet.AsList)
result.Put("target",targetSet.AsList)
Dim previousSegs As List=getPreviousSegs
currentProject.loadItemsToSegments(result)
loadSegmentsToListView(previousSegs)
Dim sb As StringBuilder
sb.Initialize
sb.Append(sizeBefore-sourceSet.Size).Append(" source segments removed.").Append(CRLF)
sb.Append(sizeBefore-targetSet.Size).Append(" target segments removed.")
fx.Msgbox(MainForm,sb.ToString,"")
End Sub
Public Sub RemoveEmptySegments
Dim new As List
new.Initialize
For Each segment As Map In currentProject.segments
Dim source,target As String
source=segment.Get("source")
target=segment.Get("target")
If source.Trim="" And target.Trim="" Then
Continue
Else
new.Add(segment)
End If
Next
Dim previousSegs As List=getPreviousSegs
currentProject.segments.Clear
currentProject.segments.AddAll(new)
loadSegmentsToListView(previousSegs)
End Sub
Sub askHighPrecision As Boolean
Dim response As Int=fx.Msgbox2(MainForm,"Use high precision mode for Chinese/English alignment?","","Yes","","No",fx.MSGBOX_CONFIRMATION)
Dim highPrecison As Boolean
If response=fx.DialogResponse.POSITIVE Then
highPrecison=True
Else
highPrecison=False
End If
Return highPrecison
End Sub
'bleualign, lfalign
Sub AutoAlign(method As String) As ResumableSub
Dim response As Int=fx.Msgbox2(MainForm,"Align paragraph by paragraph (works when segments are aligned in paragraph level)?","","Yes","Cancel","No",fx.MSGBOX_CONFIRMATION)
Dim AlignInParagraphs As Boolean
If response=fx.DialogResponse.CANCEL Then
Return ""
End If
Dim langPair As Map
langPair=currentProject.getProjectFileValue("langPair")
Dim s As statistics
s.Initialize(langPair)
Dim targetSourceRatio As Double=s.TargetSourceRatio(currentProject.segments)
If response=fx.DialogResponse.POSITIVE Then
AlignInParagraphs=True
Else
AlignInParagraphs=False
End If
If method="bleualign" Then
Dim fc As FileChooser
fc.Initialize
fc.SetExtensionFilter("txt", Array As String("*.txt"))
fc.Title="Please select the translation file"
Dim path As String=fc.ShowOpen(MainForm)
If File.Exists(path,"")=False Then
Return ""
End If
End If
Dim segments As List
segments.Initialize
segments.AddAll(currentProject.segments)
If method="bleualign" Then
Dim translationListALL As List=File.ReadList(path,"")
Dim appended As Boolean=appendTranslations(segments,translationListALL)
If appended=False Then
fx.Msgbox(MainForm,"Translation and source text are not aligned","")
Return ""
End If
End If
Dim newSegments As List
newSegments.Initialize
Dim paragraphs As List
If AlignInParagraphs Then
paragraphs=segmentsInParagraphs(segments)
Else
paragraphs.Initialize
paragraphs.Add(segments)
End If
progressDialog.Show("Aligning...","align")
Dim paragraphNum As Int
For Each paragraph As List In paragraphs
paragraphNum=paragraphNum+1
progressDialog.update(paragraphNum,paragraphs.Size)
If paragraph.Size<=1 Then
newSegments.AddAll(paragraph)
Continue
End If
Dim sourceList,targetList,translationList As List
sourceList.Initialize
targetList.Initialize
translationList.Initialize
Dim firstSegment As Map=paragraph.Get(0)
Dim paraID As Int=firstSegment.GetDefault("id",paragraphNum-1)
For Each segment As Map In paragraph
Dim source,target,translation As String
source=segment.Get("source")
target=segment.Get("target")
translation=segment.GetDefault("alt-translation","")
If source<>"" Then
sourceList.Add(source)
End If
If target<>"" Then
targetList.Add(target)
End If
If translation<>"" Then
translationList.Add(translation)
End If
Next
If method="bleualign" Then
Dim ba As Bleualign
ba.Initialize
wait for (ba.Align(sourceList,targetList,translationList)) complete (result As Map)
Else
Dim lfa As LFAlign
lfa.Initialize
wait for (lfa.Align(sourceList,targetList,langPair.Get("source"),langPair.Get("target"))) complete (result As Map)
End If
If result.Get("success") Then
Log("success")
Dim AlignedSources,AlignedTargets As List
AlignedSources=result.Get("sourceList")
AlignedTargets=result.Get("targetList")
Dim complete As Boolean=True
If TextIsIncomplete(sourceList,AlignedSources) Or TextIsIncomplete(targetList,AlignedTargets) Then
complete=False
End If
Dim index As Int=0
For Each item As String In AlignedSources
Dim segment As Map
segment.Initialize
segment.Put("source",item)
Dim alignedTarget As String=AlignedTargets.Get(index)
segment.Put("target",alignedTarget)
If AlignInParagraphs Then
segment.Put("id",(paraID))
End If
If complete=False Then
segment.Put("complete",complete)
End If
Dim ratio As Double=s.TargetSourceRatio2(item,alignedTarget)
If ratio<0.5*targetSourceRatio Or ratio>2*targetSourceRatio Then
segment.Put("accurate",False)
End If
index=index+1
newSegments.Add(segment)
Next
Else
Log("failed")
newSegments.AddAll(paragraph)
End If
Next
currentProject.segments.Clear
currentProject.segments.AddAll(newSegments)
progressDialog.close
Return ""
End Sub
Sub TextIsIncomplete(before As List,after As List) As Boolean
Dim sb As StringBuilder
sb.Initialize
For Each segment As String In after
sb.Append(segment)
Next
Dim textAfter As String=sb.ToString
For Each segment As String In before
segment=segment.Trim
If segment<>"" Then
If textAfter.Contains(segment)=False Then
Log(segment)
Log(textAfter)
Return True
End If
End If
Next
Return False
End Sub
Sub appendTranslations(segments As List,translationList As List) As Boolean
Dim new As List
new.Initialize
new.Addall(segments)
Dim initTranslationSize As Int=translationList.Size
Dim index As Int=0
For Each segment As Map In new
Dim source As String=segment.Get("source")
If segment.ContainsKey("id") Then
Dim id As String=segment.Get("id")
Dim para As Map=currentProject.paragraphs.Get(id)
If para.IsInitialized Then
If para.GetDefault("size",1)<=1 Then
Continue
End If
End If
End If
If source<>"" Then
If translationList.Size>0 Then
Dim translation As String=translationList.Get(0)
segment.Put("alt-translation",translation)
translationList.RemoveAt(0)
Else
Return False
End If
index=index+1
End If
Next
Log(index)
Log(initTranslationSize)
If index=initTranslationSize Then
segments.Clear
segments.AddAll(new)
Log("translatoin is correct")
Return True
End If
Return False
End Sub
Sub segmentsInParagraphs(segments As List) As List
Dim paragraphs As List
paragraphs.Initialize
If segments.Size>0 Then
Dim segment As Map =segments.Get(0)
If segment.ContainsKey("id")=False Then
paragraphs.add(segments)
Return paragraphs
End If
End If
Dim previousID As Int=0
Dim paragraphSegments As List
paragraphSegments.Initialize
For Each segment As Map In segments
If segment.Get("id")<>previousID Then
previousID=segment.Get("id")
Dim new As List
new.Initialize
new.AddAll(paragraphSegments)
paragraphs.Add(new)
paragraphSegments.Clear
End If
paragraphSegments.Add(segment)
Next
If paragraphSegments.Size<>0 Then ' last paragraph
paragraphs.Add(paragraphSegments)
End If
Return paragraphs
End Sub
Sub exportToDocx
If currentProject.IsInitialized Then
If currentProject.segments.Size<>0 Then
Dim fc As FileChooser
fc.Initialize
fc.SetExtensionFilter("docx",Array As String("*.docx"))
Dim path As String = fc.ShowSave(MainForm)
If path="" Then
Return
End If
Dim rows As List
rows.Initialize
For Each segment As Map In currentProject.segments
Dim source,target,note As String
source=segment.Get("source")
target=segment.Get("target")
note=segment.Get("note")
rows.Add(Array As String(source,target,note))
Next
Dim poiw As POIWord
poiw.Initialize("","write")
poiw.createTable(rows,path)
fx.Msgbox(MainForm,"Done.","")
End If
End If
End Sub
Sub exportToXLIFF
If currentProject.IsInitialized Then
Dim fc As FileChooser
fc.Initialize
fc.SetExtensionFilter("XLIFF",Array As String("*.xlf"))
Dim path As String = fc.ShowSave(MainForm)
If path="" Then
Return
End If
Dim langPair As Map=currentProject.getProjectFileValue("langPair")
xliffFilter.Export(currentProject.segments,path,langPair.Get("source"),langPair.Get("target"))
fx.Msgbox(MainForm,"Done","")
End If
End Sub
Sub exportToXLSX
If currentProject.IsInitialized Then
Dim fc As FileChooser
fc.Initialize
fc.SetExtensionFilter("XLSX",Array As String("*.xlsx"))
Dim path As String = fc.ShowSave(MainForm)
If path="" Then
Return
End If
Dim wb As PoiWorkbook
wb.InitializeNew(True)
Dim sheet1 As PoiSheet = wb.AddSheet("Sheet1",0)
Dim head As PoiRow=sheet1.CreateRow(0)
Dim langPair As Map=currentProject.getProjectFileValue("langPair")
head.CreateCellString(0,langPair.Get("source"))
head.CreateCellString(1,langPair.Get("target"))
For Each segment As Map In currentProject.segments
Dim source,target As String
source=segment.Get("source")
target=segment.Get("target")
Dim rowNumber As Int
rowNumber=sheet1.LastRowNumber+1
Dim row As PoiRow=sheet1.CreateRow(rowNumber)
row.CreateCellString(0,source)
row.CreateCellString(1,target)
Next
wb.Save(path,"")
fx.Msgbox(MainForm,"Done","")
End If
End Sub
Sub exportToTMX
If currentProject.IsInitialized Then
If currentProject.segments.Size<>0 Then
Dim fc As FileChooser
fc.Initialize
fc.SetExtensionFilter("TMX",Array As String("*.tmx"))
Dim path As String = fc.ShowSave(MainForm)
If path="" Then
Return
End If
Dim includeTags As Boolean=True
Dim useTMXTags As Boolean=True
Dim response As Int
response=fx.Msgbox2(MainForm,"include tags?","","Yes","Cancel","No",fx.MSGBOX_CONFIRMATION)
Select response
Case fx.DialogResponse.CANCEL
Return
Case fx.DialogResponse.POSITIVE
includeTags=True
Case fx.DialogResponse.NEGATIVE
includeTags=False
End Select
If includeTags Then
Dim result2 As Int
result2=fx.Msgbox2(MainForm,"How to handle tags?","","Keep the original format","Cancel","Conform to TMX Specification",fx.MSGBOX_CONFIRMATION)
Select result2
Case fx.DialogResponse.CANCEL
Return
Case fx.DialogResponse.POSITIVE
useTMXTags=False
Case fx.DialogResponse.NEGATIVE
useTMXTags=True
End Select
End If
Dim segments As List
segments.Initialize
For Each segment As Map In DuplicateRemovedSegments(currentProject.segments)
Dim source,target,note As String
source=segment.Get("source")
target=segment.Get("target")
note=segment.GetDefault("note","")
If source="" Or target="" Then
Continue
End If
Dim bitext As Map
bitext.Initialize
bitext.Put("source",source)
bitext.Put("target",target)
bitext.Put("note",note)
segments.Add(bitext)
Next
Dim langPair As Map
langPair=currentProject.getProjectFileValue("langPair")
'TMX.export(segments,langPair.Get("source"),langPair.Get("target"),path,False,False)
TMX.exportQuick(segments,langPair.Get("source"),langPair.Get("target"),path,includeTags,useTMXTags)
fx.Msgbox(MainForm,"Done.","")
End If
End If
End Sub
Sub exportSourceToTxt
If currentProject.IsInitialized Then
If currentProject.segments.Size<>0 Then
Dim fc As FileChooser
fc.Initialize
fc.SetExtensionFilter("Txt",Array As String("*.txt"))
Dim path As String = fc.ShowSave(MainForm)
If path="" Then
Return
End If
Dim sourceList As List
sourceList.Initialize
For Each segment As Map In currentProject.segments
Dim source As String=segment.Get("source")
If segment.ContainsKey("id") Then
Dim id As String=segment.Get("id")
Dim para As Map=currentProject.paragraphs.Get(id)
If para.Get("size")<=1 Then
Continue
End If
End If
If source<>"" Then
sourceList.Add(source)
End If
Next
File.WriteList(path,"",sourceList)
fx.Msgbox(MainForm,"Done.","")
End If
End If
End Sub
Sub exportToTxt
If currentProject.IsInitialized Then
If currentProject.segments.Size<>0 Then
Dim sc As SplitterChooser
sc.Initialize
sc.Title="请输入原文和译文的分隔符:"
Dim splitter As String
splitter=sc.showAndWait
Dim fc As FileChooser
fc.Initialize
fc.SetExtensionFilter("Txt",Array As String("*.txt"))
Dim path As String = fc.ShowSave(MainForm)
If path="" Then
Return
End If
Dim result As StringBuilder
result.Initialize
For Each segment As Map In currentProject.segments
Dim source,target,note As String
source=segment.Get("source")
target=segment.Get("target")
note=segment.Get("note")
result.Append(source).Append(splitter).Append(target).Append(splitter).Append(note.Replace(CRLF,"\n")).Append(CRLF)
Next
File.WriteString(path,"",result.ToString)
fx.Msgbox(MainForm,"Done.","")
End If
End If
End Sub
Sub newProject
Dim languageSelector As LanguagePairSelector
languageSelector.Initialize
Dim langPair As Map=languageSelector.ShowAndWait
Dim fc As FileChooser
fc.Initialize
fc.SetExtensionFilter("project file",Array As String("*.alp"))
Dim path As String=fc.ShowSave(MainForm)
If path<>"" Then
currentProject.Initialize(path)
currentProject.setProjectFileValue("langPair",langPair)
currentProject.saveProjectFile
End If
End Sub
Sub closeProject
Dim currentProject As Project
editorLV.Items.Clear
End Sub
Sub openProject
Dim fc As FileChooser
fc.Initialize
fc.SetExtensionFilter("project file",Array As String("*.alp"))
Dim path As String=fc.ShowOpen(MainForm)
If path<>"" Then
currentProject.Initialize(path)
currentProject.readProjectFile
loadSegmentsToListView(Null)
End If
End Sub
Public Sub loadSegmentsToListView(previousSegsList As List)
If currentProject.segments<>Null And currentProject.segments.Size<>0 Then
If previousSegsList<>Null Then
previousSegments=previousSegsList
End If
editorLV.Items.Clear
For i=0 To currentProject.segments.Size-1
editorLV.Items.Add("")
Next
Dim currentVisibleRange As Range
currentVisibleRange=getVisibleRange(editorLV)
fillPane(currentVisibleRange.firstIndex,currentVisibleRange.lastIndex)
CallSubDelayed(Me,"ListViewParent_Resize")
End If
End Sub
Sub saveProject
If currentProject.IsInitialized Then
currentProject.save
StatusLabel.Text="Project saved at "&DateTime.Time(DateTime.Now)
End If
End Sub
Public Sub createEmptyPane As Pane
Dim segmentPane As Pane
segmentPane.Initialize("segmentPane")
segmentPane.SetSize(editorLV.Width,50dip)
Return segmentPane
End Sub
Public Sub addTextAreaToSegmentPane(segmentpane As Pane,source As String,target As String)
segmentpane.LoadLayout("segment")
segmentpane.SetSize(editorLV.Width,50dip)
Dim sourceTextArea As TextArea
sourceTextArea=segmentpane.GetNode(0)
sourceTextArea.Text=source
addContextMenu(sourceTextArea,True)
addKeyEvent(sourceTextArea,"sourceTextArea")
addSelectionChangedEvent(sourceTextArea,"sourceTextAreaSelection")
Dim targetTextArea As TextArea
targetTextArea=segmentpane.GetNode(1)
targetTextArea.Text=target
addContextMenu(targetTextArea,False)
addKeyEvent(targetTextArea,"targetTextArea")
addSelectionChangedEvent(targetTextArea,"targetTextAreaSelection")
sourceTextArea.Left=0
sourceTextArea.SetSize(editorLV.Width/2-20dip,50dip)
targetTextArea.Left=sourceTextArea.Left+sourceTextArea.Width
targetTextArea.SetSize(editorLV.Width/2-20dip,50dip)
End Sub
Sub addContextMenu(ta As TextArea,isSource As Boolean)
Dim cm As ContextMenu
cm.Initialize("cm")
Dim delMi As MenuItem
delMi.Initialize("DELETE","delmi")
Dim upMi As MenuItem
upMi.Initialize("UP","upmi")
Dim downMi As MenuItem
downMi.Initialize("DOWN","downmi")
Dim viewNoteMi As MenuItem
viewNoteMi.Initialize("View Note","viewnotemi")
Dim tagList As List
tagList.Initialize
tagList.Add(ta)
tagList.Add(isSource)
delMi.Tag=tagList
upMi.Tag=tagList
downMi.Tag=tagList
viewNoteMi.Tag=tagList
cm.MenuItems.Add(upMi)
cm.MenuItems.Add(downMi)