-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalpaco.pl
2890 lines (2326 loc) · 83.3 KB
/
alpaco.pl
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
#!/usr/local/bin/perl
# alpaco.pl
# Program to take paralell texts in 2 languages and allow a user
# to manually align the words/phrases.
#
# Copyright (C) 2002
# Ted Pedersen, University of Minnesota, Duluth
# Brian Rassier, University of Minnesota, Duluth
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#-----------------------------------------------------------------------------
# Start of program
#-----------------------------------------------------------------------------
# use custom lib folder
use lib './lib';
#allows the use of Tk
use Tk;
use Tk::Markdown;
use Tk::DynaMouseWheelBind;
#this loads the HistEntry module if possible, otherwise just uses regular entry widgets
$Entry = "HistEntry";
eval {
#try loading the module, otherwise $Entry is left to the value "Entry"
require Tk::HistEntry;
$Entry = "HistEntry";
};
#Loads the SimpleFileSelect module if possible, otherwise defaults to a label
$SimpleFileSelect = "Label";
eval{
require Tk::SimpleFileSelect;
$SimpleFileSelect = "SimpleFileSelect";
};
#needed for the floor function, used to interpret Blinker files
use POSIX qw(ceil floor);
#-----------------------------------------------------------------------------
#MAKE THE INTERFACE/WIDGETS
#-----------------------------------------------------------------------------
#These are the colors for the word buttons. They can be changed here, then they are
#changed throughout the code.
$normal = "light gray";
$connected = "light blue";
$nullback = "black";
$nullfore = "orange";
#for indexes in my checkbutton widget arrays
$size1 = $size2 = $active1 = $active2 = 0;
#set these variables to Blinker as Default
$v = 10; #verses per file
$sam = 25; #samples of files
$an = 7; #annotators
#set flag for resizing to -1
$bopenflag = -1;
#set $blinkfile to "" so we can tell if there has been a blinker file opened
$blinkfile = "";
my $mw = MainWindow->new(-height,550,-width,660);
$mw->packPropagate(0);
$mw->title("Aligner for Parallel Corpora (Alpaco)");
#$mw->geometry("550x550");
# set up auto scroll binding to mouse wheel
$mw->DynaMouseWheelBind('Tk::Markdown', 'Tk::Text', 'Tk::Canvas');
#frame for entry and labels
$frametop = $mw->Frame->pack(-side,'top',-fill,'x');
#frame for canvas widget
$frameleft = $mw->Frame->pack(-side,'left',-fill,'y');
#frame for main buttons
$frameright = $frameleft->Frame->pack(-side,'right',-fill,'y');
#frame for menus
$frametoptop = $frametop->Frame->pack(-side,'top',-fill,'x');
$frametopbottom = $frametoptop->Frame->pack(-side,'bottom',-fill,'x');
#informational label button
$frametoptop->Label(-textvariable,\$info,-relief,'ridge',-width,70)->pack(-side,'right',
-fill,'x');
#label for entry1
$frametop->Label(-text,"Source File:")->pack(-side,'left',-anchor,'w');
#entry widget 1
$entry1_w = $frametop->$Entry(-textvariable,\$file1,
-background,"white",
-takefocus,1)->pack(-side,'left',
-anchor,'w');
#starts the cursor at the entry1 widget
$entry1_w->focus();
#label for entry2
$frametop->Label(-text,"Target File:")->pack(-side,'left',-anchor,'w');
#entry widget 2
$entry2_w = $frametop->$Entry(-textvariable,\$file2,
-background,"white")->pack(-side,'left',
-anchor,'w');
#checkbutton for line by line
$cb = $frametop->Checkbutton(-text,'Line-By-Line',-variable,\$cb_value,-relief,'ridge',
-command,\&lbyl)->pack(-side,'left');
#button for line-by-line, don't pack it until line-by-line is on
$nl = $frametopbottom->Button(-text,'Next Line',-command,\&load_files);
$pl = $frametopbottom->Button(-text,'Prev Line',
-command,sub{
#decrement the line number, then load the files
$linenum = $linenum - 2;
$linenum2 = $linenum2 - 2;
&load_files});
##########MENUS##########
#makes the file menu
$frametoptop->Menubutton(-text,"File",
-menuitems,[
#This option is pretty silly. It just simulates pressing the enter key for each entry field. I commented it out to try alpaco without it.
#['command',"Open Text Files",-command,\&load_files],
['command',"Find Text Files",-command,\&find],
"-",
['command',"Save Current Work to File",-command,\&save_blink],
"-",
['command',"Open an Alpaco File",-command,\&open_pop],
['command',"Save an Alpaco File",-command,\&save_pop],
"-",
['command',"Clear Connections",-command,\&clear_lines],
['command',"Clear All",-command,\&clear_tot],
"-",
['command',"Exit",-command,sub{
#removes the temporary files that were made if present
system("rm Blinktemp1 Blinktemp2") if(open(FP1,"Blinktemp1"));
system("rm sizetemp SizeVerse1 SizeVerse2") if(open(FP1,"sizetemp"));
exit;}]],
-tearoff,0)->pack(-side,'left',
-anchor,'w');
#makes the options menu
$frametoptop->Menubutton(-text,"Options",
-menuitems,[['command',"Connect",-command,\&con],
['command',"Null Connect",-command,\&nullcon],
['command',"Undo Connection",-command,\&undo],
['command',"Redraw Connect",-command,\&redo],
"-",
['command',"View Sentences",-command,\&show_sentences],
['command',"Change Mode",-command,\&chmode],
['command',"Change Data Limits",-command,\&data_pop]],
-tearoff,0)->pack(-side,'left',
-anchor,'w');
#makes the help menu widget
$frametoptop->Menubutton(-text,"Help",
-menuitems,[['command',"Help",-command,\&help_pop],
['command',"Keyboard Shortcuts",-command,\&kbd_pop],
['command',"About",-command,\&about_pop]],
-tearoff,0)->pack(-side,'left',
-anchor,'w');
##########MENUS END##########
##########MAIN BUTTONS##########
#connect button
$frametopbottom->Button(-text,"Connect", -command,\&con,
-background,$connected,
-activebackground,'blue'
)->pack(-side,'left',-anchor,'w',-fill,'x');
#null connect button
$frametopbottom->Button(-text,"Null Connect", -command,\&nullcon,
-activebackground,"blue",
-background,$nullback,
-foreground,$nullfore)->pack(-side,'left',-anchor,'w',-fill,'x');
#undo button
$frametopbottom->Button(-text,"Undo", -command,\&undo,
-activebackground,"blue"
)->pack(-side,'left',-anchor,'w',-fill,'x');
#redo button
$frametopbottom->Button(-text,"Redraw (one line)", -command,\&redo,
-activebackground,"blue"
)->pack(-side,'left',-anchor,'w',-fill,'x');
##########MAIN BUTTONS END##########
##########SIZING OPTION##########
#makes a frame for the sizing option
$sizeframe = $frameright->Frame->pack(-fill,'x');
#makes the space shrinking button
$minus = $sizeframe->Button(-text," - ",-command,sub{$minus->focus();
&resize;},
-activebackground,"blue",
#-background,"dark green",
#-foreground,"white",
-takefocus,1,
)->pack(-side,'left',-anchor,'w',-pady,5);
#makes the label for the word distance
$sizeframe->Label(-text," Resize ",-relief,'ridge')->pack(-side,'left',-anchor,'w');
#makes the space enlarging button
$plus = $sizeframe->Button(-text," + ",-command,sub{$plus->focus();
&resize;},
-activebackground,"blue",
#-background,"dark green",
#-foreground,"white",
-takefocus,1
)->pack(-side,'left',-anchor,'w',-pady,5);
##########SIZING OPTION END##########
##########NEXT/PREV BLINKER OPTION##########
#makes a frame for the next/previous buttons
$nextframe = $frameright->Frame->pack(-fill,'x');
#prev button
$prev = $nextframe->Button(-text,"<Prev", -command,sub{$prev->focus;&next},
-activebackground,"blue",
#-background,"dark green",
#-foreground,"white"
);
#next button
$next = $nextframe->Button(-text,"Next>", -command,sub{$next->focus;&next},
-activebackground,"blue",
#-background,"dark green",
#-foreground,"white"
);
##########NEXT/PREV BLINKER OPTION END##########
##########NEXT/PREV BLINKER ANNOTATOR OPTION##########
#makes a frame for the next/previous buttons for annotator
$nextanframe = $frameright->Frame->pack(-fill,'x');
#prev button
$prevan = $nextanframe->Button(-text," <-- ", -command,sub{$prevan->focus;&nextan},
-activebackground,"blue",
#-background,"dark green",
#-foreground,"white"
);
#next button
$nextan = $nextanframe->Button(-text," --> ", -command,sub{$nextan->focus;&nextan},
-activebackground,"blue",
#-background,"dark green",
#-foreground,"white"
);
##########NEXT/PREV BLINKER ANNOTATOR OPTION END##########
##########BROWSE/EDITR OPTION##########
#makes a frame for the radio buttons
$radioframe = $frameright->Frame->pack(-fill,'x');
#makes the browse/edit checkbuttons and stores their IDs
push @rb,$radioframe->Radiobutton(-text,'Browse',-value,'Browse',-variable,\$rb_value,
-background,'black',-foreground,'white',
-command,\&browse,-indicatoron,0);
push @rb,$radioframe->Radiobutton(-text,'Edit',-value,'Edit',-variable,\$rb_value,
-background,'black',-foreground,'white',
-command,\&browse,-indicatoron,0);
#draws the widgets
$rb[0]->pack(-side,'left',-padx,5,-pady,5);
$radioframe->Label(-text," Mode ",-relief,'ridge')->pack(-side,'left',-anchor,'w');
$rb[1]->pack(-side,'left',-padx,5,-pady,5);
#invokes the browse button as a default
$rb[0]->invoke;
##########BROWSE/EDITR OPTION END##########
#exit button
$frameright->Button(-text,"Exit", -command,sub{
#removes the temporary files that were made if present
system("rm Blinktemp1 Blinktemp2") if(open(FP1,"Blinktemp1"));
system("rm sizetemp SizeVerse1 SizeVerse2") if(open(FP2,"sizetemp"));
exit;},
-activebackground,"blue"
)->pack(-side,'top',-anchor,'w',-fill,'x',-pady,10);
#View Sentences button
#$frameright->Button(-text,"View Sentences", -command,\&show_sentences,
# -activebackground,"blue"
# )->pack(-side,'top',-anchor,'w',-fill,'x',pady,10);
#makes the canvas widget, with a parent for binding reasons
$parentcanvas = $frameleft->Scrolled("Canvas",-background,
'white',-takefocus,1,-width,500)->pack(-side,'bottom',
-fill,'both',-expand,1);
$canvas = $parentcanvas->Subwidget("canvas");
#adds a buffer in the top of the canvas
$canvas->createText(30,30,-text," ");
##########binding keyboard shortcuts############
#define enter callback for entry one
$entry1_w->bind("<Return>",\&load_1);
#define enter callback for entry one
$entry2_w->bind("<Return>",\&load_2);
#Control + s saves
$mw->bind("<Control-Key-s>", \&save_pop);
#control + o opens
$mw->bind("<Control-Key-o>", \&open_pop);
#Contryl + c quits
$mw->bind("<Control-Key-c>", \&con);
#binds Ctrl + n to null connect
$mw->bind("<Control-Key-n>",\&nullcon);
#binds Ctrl + c to connect when in list widgets
$mw->bind("<Button-3>",\&con);
#binds u to undo function when in list widgets
$mw->bind("<Control-Key-u>",\&undo);
$mw->bind("<Control-Key-r>",\&redo);
##########binding keyboard shortcuts end############
#sets the buffer between widgets (words) in the canvas
$buff = 30;
#Start the main loop
MainLoop;
#-----------------------------------------------------------------------------
#Sub-routines
#-----------------------------------------------------------------------------
#function that loads both files if the menu item is pressed.
#This function uses a sequence of flags from return values of the load_1 and load_2
#functions, in order to reuse code.
sub load_files{
#set variables to 0
$flag = $t1 = 0;
#load source file, get return value
$t1 = &load_1;
#set the flag if source file had errors
$flag = 1 if $t1 < 0;
$t1 = 0;
$t1 = &load_2;
#set the flag if target file had errors
$flag += 2 if $t1 < 0;
#check the results and let the user know if there were errors
$info = "Error opening file 1" if $flag eq 1;
$info = "Error opening file 2" if $flag eq 2;
$info = "Error opening file 1 and 2" if $flag eq 3;
$info = "Files Loaded" if $flag eq 0;
#set the scroll region now that items have been inserted
$canvas->configure(-scrollregion,[ $canvas->bbox("all") ]);
}
#-----------------------------------------------------------------------------
#load_files end
#function that loads the source file into the tool
sub load_1{
#enables the flag for the size option
$bopenflag = 0;
$info = "opening file '$file1'...";
#store file to see if a new one was entered
$holdfileold = $holdfile;
$holdfileold = "" if(!$holdfile);
$holdfile = $file1 unless($file1 eq "SizeVerse1");
#error checking for opening file 1
if(!open(FILE, "$file1")){
$info = "Error opening '$file1'";
$entry1_w->bell();
return(-1);
}
#clear out the old sentence
undef $sentence1;
#clears the previous connections
&clear_lines;
#remove the old words from the canvas
while(@del1){
$canvas->delete(pop @del1);
}
#clear the id's from the past file
undef @del1;
#clears the canvas text for file 1
while(@id1){
$d = (pop @id1);
$d->destroy();
}
#undefines the id's for file 1
undef @id1;
#reset the status array for list 1
$size1 = $active1 = 0;
undef @status1;
#This if statement takes care of the line-by-line loading if necessary
if($cb_value){
#reset the line number if a new file was loaded
$linenum = 0 unless($holdfileold eq $holdfile);
$linenum = 0 if($linenum < 0);
#load the next line
for my $i (0..$linenum){
$line = <FILE>;
}
#check if there were any lines left, let the user know
if(!$line){
$info = "No more lines in the file";
$linenum = 0;
return;
}
close (FILE);
#add the file to the history if that module is present
$entry1_w->historyAdd($file1) if($entry1_w->can('historyAdd'));
#set the yvaraiable to the proper screen distance
my $yvar = 50;
#keep the sentence for viewing later
$sentence1 = $line;
#parse the line
$line =~ s/[^\S ]//g; #remove all whitespace except spaces
$line =~ s/ +/ /g; #remove multiple spaces
#puts the line into an array for processing
my @temp = split / /, $line;
#process the line word-by-word
foreach my $word (@temp){
#make word labels
$word = " " x (10-length($word)) . $word;
my $label1 = $canvas->Checkbutton(-text,$word,-relief,'ridge',-justify,'right',-takefocus,1,
-selectcolor,'blue',-indicatoron,0,-variable,\$status1[$size1++],
-disabledforeground,'blue',-activebackground,'dark green',
-activeforeground,'white');
#keep track of $id for configuring the checkbuttons
push @id1,$label1;
#keep track of the actual object to delete it later
push @del1,$canvas->createWindow(150,$yvar,-anchor,'e',-window,$label1);
#increment the $yvar for the next object to insert
$yvar += $buff;
}
#change the scroll region because more objects were added
$canvas->configure(-scrollregion,[ $canvas->bbox("all") ]);
close(FILE);
#update informational label for user knowledge
$info = "Line $linenum from file '$file1' loaded";
#increment the line number
$linenum++;
#make sure the words are enabled/disabled as needed
&browse;
#do not continue loading, because line-by-line is finished
return;
}
#This section is executed on a regular loading of a file (not line-by-line)
#save filename for save function, to print to a savefile in case
#the user changes the entry1 widget entry.
$entry1_w->historyAdd($file1) if($entry1_w->can('historyAdd'));
$keepfile1 = $file1;
#set $yvar to 50 for inserting position
my $yvar = 50;
#This while loop seperates the words in the file, makes widgets for
#each word, and inserts them into the canvas. It also makes the null widgets.
while(<FILE>) {
#keep the sentence for viewing later
$sentence1 = $sentence1 . $_;
#parse the file
$_ =~ s/[^\S ]//g; #remove all whitespace except spaces
$_ =~ s/ +/ /g; #remove multiple spaces
#split up the line into an array
my @temp = split / /, $_;
#process the line word-by-word
foreach $word (@temp){
#make word labels
my $word = " " x (10-length($word)) . $word;
my $label1 = $canvas->Checkbutton(-text,$word,-relief,'ridge',-justify,'right',-takefocus,1,
-selectcolor,'blue',-indicatoron,0,-variable,\$status1[$size1++],
-disabledforeground,'blue',-activebackground,'dark green',
-activeforeground,'white');
#keep track of $id for configuring the checkbuttons
push @id1,$label1;
#keep track of the actual object to delete it later
push @del1,$canvas->createWindow(150,$yvar,-anchor,'e',-window,$label1);
#increment the $yvar for the next object to insert
$yvar += $buff;
}
}
#change the scroll region because more objects were added
$canvas->configure(-scrollregion,[ $canvas->bbox("all") ]);
close(FILE);
#update informational label for user knowledge
$info = "File '$file1' loaded";
&browse;
}
#-----------------------------------------------------------------------------
#load_1 end
#function that loads target file into the tool
sub load_2{
#enables the flag for the size option
$bopenflag = 0;
$info = "opening file '$file2'...";
#store file to see if a new one was entered
$holdfileold2 = $holdfile2;
$holdfileold2 = "" if(!$holdfile2);
$holdfile2 = $file2;
#error checking when file is opened
if(!open(FILE, "$file2")){
$info = "Error opening '$file2'";
$canvas->bell();
return(-2);
}
#clear out the old sentence
undef $sentence2;
#clears the previous connections
&clear_lines;
#remove the old words from the canvas
while(@del2){
$canvas->delete(pop @del2);
}
#clear the id's from the past file
undef @del2;
#clears the canvas text for file 2
while(@id2){
$d = (pop @id2);
$d->destroy();
}
#undefines the id's from file 2
undef @id2;
#clear the status array for the checkbuttons in list 2
$size2 = $active2 = 0;
undef @status2;
#This if statement takes care of the line-by-line loading
if($cb_value){
#reset the line number if a new file was opened
$linenum2 = 0 unless($holdfileold2 eq $holdfile2);
$linenum2 = 0 if($linenum2 < 0);
#find the needed line
for my $i (0..$linenum2){
$line = <FILE>;
}
#if there is no line, let the user know the file is finished
if(!$line){
$info = "No more lines in the file";
$linenum2 = 0;
return;
}
close (FILE);
#add the file to the history
$entry2_w->historyAdd($file2) if($entry2_w->can('historyAdd'));
#set the yvaraiable to the proper screen distance
my $yvar = 50;
#keep the sentence for viewing later
$sentence2 = $line;
#parse the line
$line =~ s/[^\S ]//g; #remove all whitespace except spaces
$line =~ s/ +/ /g; #remove multiple spaces
#split up the line into an array
my @temp = split / /, $line;
#process the line word-by-word
foreach $word (@temp){
#make word labels
my $word = $word . " " x (10-length($word));
my $label2 = $canvas->Checkbutton(-text,$word,-relief,'ridge',-justify,'left',-takefocus,1,
-selectcolor,'blue',-indicatoron,0,-variable,\$status2[$size2++],
-disabledforeground,'blue',-activebackground,'dark green',
-activeforeground,'white');
#keep track of $id for configuring the checkbuttons
push @id2,$label2;
#keep track of the actual object to delete it later
push @del2,$canvas->createWindow(300,$yvar,-anchor,'w',-window,$label2);
#increment the $yvar for the next object to insert
$yvar += $buff;
}
#change the scroll region because more objects were added
$canvas->configure(-scrollregion,[ $canvas->bbox("all") ]);
close(FILE);
#update informational label for user knowledge
$info = "Line $linenum2 from file '$file2' loaded";
#increment the line number
$linenum2++;
#make sure the words are enabled/disabled as needed
&browse;
#exit the function because it is finished loading
return;
}
#This section is executed on a regular loading of a file (not line-by-line)
#save filename for save function, to print to a savefile in case
#the user changes the entry1 widget entry.
$entry2_w->historyAdd($file2) if($entry2_w->can('historyAdd'));
$keepfile2 = $file2;
#set $yvar to 50 for inserting position
my $yvar = 50;
#This while loop seperates the words in the file, makes widgets for
#each word, and inserts them into the canvas. It also makes the null widgets.
while(<FILE>) {
#keep the sentence for viewing later
$sentence2 = $sentence2 . $_;
#parse the file
$_ =~ s/[^\S ]//g; #remove all whitespace except spaces
$_ =~ s/ +/ /g; #remove multiple spaces
#split up the line into an array
my @temp = split / /, $_;
#process the line word-by-word
foreach $word (@temp){
#make word labels
my $word = $word . " " x (10 - length($word));
my $label2 = $canvas->Checkbutton(-text,$word,-relief,'ridge',-justify,'left',-takefocus,1,
-selectcolor,'blue',-indicatoron,0,-variable,\$status2[$size2++],
-disabledforeground,'blue',-activebackground,'dark green',
-activeforeground,'white');
#keep track of id to configure the checkbuttons later
push @id2,$label2;
#keep track of the actual objects to delete them later
push @del2,$canvas->createWindow(300,$yvar,-anchor,'w',-window,$label2);
#increment the $yvar for the next insertion's position
$yvar += $buff;
}
}
#change the scroll region because new elements were inserted
$canvas->configure(-scrollregion,[ $canvas->bbox("all") ]);
close(FILE);
#update informational label for user knowledge
$info = "File '$file2' loaded";
&browse;
}
#-----------------------------------------------------------------------------
#load_2 end
#This function makes a "null connection" if a word in one file has no
#translation into the other file.
sub nullcon {
#clear out temp arrays @list1 and @list2 from past connections
undef @list1;
undef @list2;
#checks for browse mode
if($rb_value eq 'Browse'){
$info = "Cannot connect in browse mode!";
$canvas->bell();
return;
}
#These 2 while loops get the selected elements to connect
my $i = 0;
while($i < @status1){
if($status1[$i]){
push @list1,$i;
}
$i++;
}
$i = 0;
while($i < @status2){
if($status2[$i]){
push @list2,$i;
}
$i++;
}
#error checking if nothing was selected
if(!@list1 && !@list2){
$info = "ERROR: Nothing Selected For Null connect";
$entry1_w->bell();
return;
}
#error checking if items from both lists were selected
if(@list1 && @list2){
$info = "ERROR: Items Selected From Both Lists";
$entry1_w->bell();
return;
}
#This if statement is entered if there is something selected from the first
#text file to "null connect", and there is nothing selected from file 2
if(@list1){
#this for loop makes a null connection for each selected item in the first list
foreach (@list1) {
#save the indexes + 1 for the blinker-style file
$i = $_+1;
#looks through all the connections, and doesn't connect if the connection is
#already there
$foundflag = 0;
foreach(@blinkarray){
if($_ =~ /^$i 0$/){
$foundflag = 1;
$id1[$_]->deselect;
$info = "null connect to $_ has already been done";
last;
}
}
#skip this connection if it has already been done
next if($foundflag == 1);
#continue if connection hasn't been done yet
push @blinkarray, "$i 0";
#un-highlight the selected items,change to "null" color
$id1[$_]->deselect();
$id1[$_]->configure(-background, $nullback, -foreground, $nullfore,-disabledforeground,"white");
#keep last element for easier use of the arrow keys
$active1 = $_;
}
}
#This if statement is entered if there is something selected from the second
#text file to "null connect", and there is nothing selected from file 1
elsif(@list2){
#this for loop makes a null connection for each selected item in the second list
foreach (@list2) {
#save the indexes + 1 for the blinker-style file
$i = $_+1;
#looks through all the connections, and doesn't connect if the connection is
#already there
$foundflag = 0;
foreach(@blinkarray){
if($_ =~ /^0 $i$/){
$foundflag = 1;
$id2[$_]->deselect;
$info = "null connect to $_ has already been done";
last;
}
}
#skip this connection if it has already been done
next if($foundflag == 1);
#continue if connection hasn't been done yet
push @blinkarray, "0 $i";
#un-highlight the selected items
$id2[$_]->deselect();
$id2[$_]->configure(-background, $nullback, -foreground, $nullfore,-disabledforeground,"white");
#keep last element for easier use of the arrow keys
$active2 = $_;
}
}
}
#-----------------------------------------------------------------------------
#nullcon end
#This function makes a connection if there is something selected from the
#source file, as well as the target file
sub con {
#clears out the old lists from past connections
undef @list1;
undef @list2;
#checks for browse mode
if($rb_value eq 'Browse'){
$info = "Cannot connect in browse mode!";
$canvas->bell();
return;
}
#These 2 while loops get the selected elements to connect
my $i = 0;
while($i < @status1){
if($status1[$i]){
push @list1,$i;
}
$i++;
}
$i = 0;
while($i < @status2){
if($status2[$i]){
push @list2,$i;
}
$i++;
}
#error checking to make sure a word is selected from both lists
if(!@list1){
$info = "ERROR: Nothing Selected From The Source Text";
$entry1_w->bell();
return;
}
if(!@list2){
$info = "ERROR: Nothing Selected From The Target Text";
$entry1_w->bell();
return;
}
#These nested for loops do the actual connecting and drawing of connections
foreach $i (@list1){
#clear selection in the list
$id1[$i]->deselect;
#keep active item for easier use of arrow keys
$active1 = $i;
foreach $j (@list2){
#increment the indexes to store them in the blinker-style
my $temp1 = $i+1;
my $temp2 = $j+1;
#clear selection in the list
$id2[$j]->deselect;
#keep active item for easier use of arrow keys
$active2 = $j;
#looks through all the connections, and doesn't connect if the connection is
#already there
my $foundflag = 0;
foreach(@blinkarray){
if($_ =~ /^$temp1 $temp2$/){
$foundflag = 1;
$info = "connection $i to $j has already been done";
last;
}
}
#skip this connection if it has already been done
next if($foundflag == 1);
$id1[$temp1-1]->configure(-background, $connected);
$id2[$temp2-1]->configure(-background, $connected);
#find correct coordinates, and save indexes in blinker-style file
push @blinkarray, "$temp1 $temp2";
my $x1 = 150;
my $y1 = 50 + $buff * $i;
my $x2 = 300;
my $y2 = 50 + $buff * $j;
#keep track of lines for the undo function
push @lines, $canvas->createLine($x1,$y1,$x2,$y2);
}
}
}
#-----------------------------------------------------------------------------
#con end
#This function clears the connections, but leaves the files intact
sub clear_lines{
#puts into edit mode to load connections
my $flageroo = 0;
if($rb_value eq 'Browse'){
$rb[1]->invoke;
$flageroo = 1;
}
#undoes the null connections
while(@blinkarray){
&undo;
}
#put back into browse mode if necessary
if($flageroo){
$rb[0]->invoke;
}
#clear the arrays used for connections
undef @blinkarray;
undef @redo;
undef @lines;
$info = "Connections Clear";
&browse;
}
#-----------------------------------------------------------------------------
#clear_lines end
#This function clears the lists, entry widgets, and all the connections
sub clear_tot{
#enables the flag for the size option
$bopenflag = 0;
#clears out the arrays used for various purposes
undef @blinkarray;
undef @redo;