-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorthos.js
1690 lines (1568 loc) · 46.2 KB
/
orthos.js
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
/**
* orthos.js - Pattern Generator for hyphenation patterns in node.js
*
* USAGE
* (install node version 5.1.0 or newer)
* node orthos.js <wortliste> <pattern-in> <pattern-out>
*
* HISTORY
* patgen (PATtern GENeration program for the TEX82 hyphenator) was originally
* written by Frank Liang in PASCAL in 1983 and had revisions in 1991, 1992
* and 1996 by Peter Breitenloher and in 1996 by Karl Berry. It was originally
* ported to UNIX by Howard Trickey.
* patgen is a piece of very well documented and mature software and it is
* still used to produce new patterns for Frank Liangs hyphenation algorithm.
*
* Further reading about patgen and Liangs hyphenation algorithm:
* - patgen.web: the original patgen
* https://www.ctan.org/pkg/patgen
* (use weave and tangle to translate the literal programming WEB
* to TEX and PASCAL respectively)
* - Frank Liang, Word hy-phen-a-tion by com-puter, STAN-CS-83-977,
* Stanford University Ph.D. thesis, 1983
* http://tug.org/docs/liang
*
* A program called opatgen (with Unicode support and other improvements, by
* David Antos and Petr Sojka) is also mentioned on http://tug.org/docs/liang
* put the link is broken and I can't find this software anymore.
*
* So why a port of patgen to node.js? (and why JavaScript and not just C?)
*
* When patgen was originally written computers were very limited: the PDP-10
* mainframe computer used by Liang had 256 Kilowords – about 1MB – of memory
* and could average about 450 kilo instructions per second.
* Saving memory was critical, characters were encoded in ASCII, students where
* teached in structured programming using PASCAL.
*
* A lot has changed eversince – and many things haven't. Porting patgen to
* JavaScript/ES6/node.js is aimed at:
* - learning how patgen works
* - providing software that runs acceptably fast on current computers
* - providing a program that can be easily used to produce patterns
*
* But why JavaScript?
* - JavaScript runs on every computer that has at least a browser installed
* - JavaScript has native Unicode support
* - It's the language I know best
*
* Why the name orthos.js?
* There's no licence available for patgen. If there would it would most probably
* be LPPL (https://www.latex-project.org/lppl/). So just to make sure I had to
* give this another name than "patgen".
* Then, there is "hydra" (https://github.com/hyphenation/hydra) a Ruby port of
* patgen. Orthos was the brother of Hydra.
* And may be some day there will be a hero that kills both…
*
* CODING STANDARDS
* The first version of orthos.js was a more or less 1:1 port from PASCAL to JavaScript.
* The source is now in a refactoring process where it is step-by-step made
* more JavaScript-alike.
* Identifiers copied from the original typically have under_scores in their
* names, while refactored code uses camelCase.
* The source is continuously linted by JSLint.
* The API may change and is not intended to be the same as of the original.
* Resulting patterns have to be identical to the patterns computed by patgen
* given the same input (except for their ordering and encoding).
*
* ERRORS
* Please report errors on [githublink]
*
* LICENCE
* Copyright (c) <year> <copyright holders>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*jslint browser: false, node: true*/
"use strict";
/**
* Read the files given as arguments and create a `File`-Object
* `File` is a proxy that holds the content of the readed
* file in memory – here we trade memory for I/O
* (typically the dictionary files are < 10MB)
* Todo: Error handling
*/
var fs = require("fs");
function makeFile(content) {
var data = [0, content];
function eof() {
return data[0] === data[1].length;
}
function reset() {
data[0] = 0;
}
function read() {
var r = data[1].charAt(data[0]);
data[0] += 1;
return r;
}
function write(s) {
data[1] += s;
}
function save(path) {
return new Promise(function (resolve, reject) {
fs.writeFile(path, data[1], function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
return {
data: data,
eof: eof,
reset: reset,
read: read,
write: write,
save: save
};
}
function readFilePromise(path) {
return new Promise(function (resolve, reject) {
fs.readFile(path, "utf8", function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
/**
* Get promises for input files
*/
const dictionaryProm = readFilePromise(process.argv[2]);
const patternInProm = readFilePromise(process.argv[3]);
/**
* Create output files
*/
const patout = makeFile("");
/**
* variables for fullfilled file promises
*/
var dictionary;
var patterns;
/**
* Globally defined const and var/let
* Todo:
* [] Comment each var/const/let
* [] Check which vars/consts could be made local or eliminated
* [] Check if some var/let can be redefined as const
* [] use let instead of var (beware no-opt of compound let statements)
*/
/**
* First line printed out when orthos.js is run
*/
const banner = "This is orthos.js for node.js, Version 1.0b";
/**
* space for pattern trie
* originally this was 55000, but that's to small for the german dictionary
*/
const trie_size = 55000 * 80;
/**
* space for pattern count trie, must be less than trie_size and greater
* than the number of occurrences of any pattern in the dictionary
* originally this was 26000, but that's to small for the german dictionary
*/
const triec_size = 32000 * 80;
/**
* size of output hash table, should be a multiple of 510 (= 2*255)
*/
const max_ops = 510 * 10;
/**
* maximum number of levels+1, also used to denote bad patterns
*/
const max_val = 10;
/**
* maximum pattern length,
* also maximum length of external representation of a 'letter'
*/
const max_dot = 15;
/**
* maximum word length,
*/
const max_len = 50;
/**
* maximum length of input lines, at least max_len
* originaly this was const set to 80, but it's faster
* to dynamically assign the buf-array and push new values.
*/
var buf_len;
/**
* The following vars are the parameters for orthos.js
* They are prompted to the user for each run.
*/
var pat_start;
var pat_finish;
var hyph_start;
var hyph_finish;
var good_wt;
var bad_wt;
var thresh;
/**
* For the purpose of this program (see trie creation) each character
* is mapped to an internal code of type int without any relations to
* some encoding. The characters '0'...'9' and '.' have special meanings
* and are used in every language and thus always mapped to the same int:
* '0'->0 ... '9'->9 and '.'->10 (see function initialize())
* '.' marks the beginning an end of a word. A pattern like '.ab1' only matches
* at the beginning of a word that begins with 'ab' while a pattern like 'ab1'
* matches at every position in a word.
*/
var edge_of_word;
/**
* When reading the dictionary and patterns the characters have to be
* recognized as fast as possible. Reading the charCode and then decide
* what kind of character it is takes to much time.
* Thus we traverse the dictionary at the beginning and collect all characters,
* map them to a internalCharCode and assign them a class.
*
* xint: charCode -> internalCharCode
* xext: internalCharCode -> char
* xdig: value -> digit
* xclass: char -> one of the following classes:
*
* digit_class: chars '0'...'9' (hyphen values or weights)
* hyf_class: chars '.', '-' and '*' (bad, missed, good hyphens)
* letter_class: chars representing a letter
* invalid_class: chars that should not occur
*
* The only char that changes its class is '.':
* reading dictionary: invalid_class
* reading patterns: letter_class
* writing pattmp: hyf_class
*/
const digit_class = 1;
const hyf_class = 2;
const letter_class = 3;
const no_hyf = 10;// ''
const err_hyf = 11;// '#'
const is_hyf = 12;// '-'
const found_hyf = 13;// '*'
var xclass = {};
var xint = {};
var xdig = [];
var xext = [];
/**
* cmin...cmax is the range of internalCharCodes (cmax = xext.length - 1)
* We start at 1 since 0:'0' will never be used in dictionary or pattern
* cnum is the number of internalCharCodes (cnum = xext.length)
*/
const cmin = 1;
var cmax;
var cnum;
/**
* The trie is stored in separate typed arrays:
* trie_* (pattern trie)
* triec_* (pattern count trie)
* *_c (char as internalCharCode)
* *_l (link resp good count)
* *_r (back resp bad count or output)
* *_taken (1: triebase is taken, 0: triebase is free)
* ops: outputs (Hash-object with three properties)
*/
const trie_c = new Uint32Array(trie_size);
const trie_l = new Uint32Array(trie_size);
const trie_r = new Uint32Array(trie_size);
const trie_taken = new Uint8Array(trie_size);
const triec_c = new Uint32Array(triec_size);
const triec_l = new Uint32Array(triec_size);
const triec_r = new Uint32Array(triec_size);
const triec_taken = new Uint8Array(trie_size);
const ops = [];
/**
* When some trie state is being worked on, an unpacked version of the state
* is kept in position 1..qmax of trieq_*
* qmax_thresh controls the density of first-fit packing (0: sparse)
* Defaults to 5: After deleting outputs this is set to 7
* because the trie is sparser
* Todo: investigate this. Maybe space could be treated for speed
*/
const trieq_c = new Uint32Array(50);
const trieq_l = new Uint32Array(50);
const trieq_r = new Uint32Array(50);
var qmax;
var qmax_thresh;
var trie_max;//maximum occupied trie node
var trie_bmax;//maximum base of trie family
var trie_count;//number of occupied trie nodes, for space usage statistics
var op_count;//number of outpust in hash table
/**
* The trie_root is on position 1. A link to 0 marks the end of a pattern
*/
var trie_root = 1;
var triec_root = 1;
/**
* pat is the current pattern of length pat_len
*/
var pat = new Uint32Array(50);
var pat_len;
var triec_max;//maximum occupied trie node
var triec_bmax;//maximum base of trie family
var triec_count;//number of occupied trie nodes, for space usage statistics
var triec_kmax;//shows growth of trie during pass
var pat_count;//number of patterns in count trie
var word = new Uint8Array(max_len);//current word
var dots = new Uint8Array(max_len);//current hyphens
var dotw = new Uint8Array(max_len);//dot weights
var hval = new Uint8Array(max_len);//hyphenation values
var no_more = new Uint8Array(max_len);//positions 'knocked out'
var wlen;//length of current word
var word_wt;//the global word weight
var wt_chg;//indicates word_wt has changed
//block 55
var left_hyphen_min;//minimal length of string before first hyphenatiom
var right_hyphen_min;//minimal length of string after last hyphenatiom
//block 66
var good_pat_count;//number of good patterns added at end of pass
var bad_pat_count;//number of bad patterns added at end of pass
var good_count;//good hyphen count
var bad_count;//bad hyphen count
var miss_count;//missed hyphen coung
var level_pattern_count;//number of good patterns at level
var more_to_come;//set to true if the quality of a pattern is ambiguous
/**
* If hyphp is set to true, do_dictionary will write out a copy of the
* dictionary as hyphenated by the current set of patterns. If procesp is
* set to true, do_dictionary will collect pattern statistics for patterns
* with length pat_len and hyphen position pat_dot, at level hyph_level.
*/
var procesp;
var hyphp;
var pat_dot;
var hyph_level;
var filnam = "";
var hyf_min;//left_hyphen_min + 1
var hyf_max;//right_hyphen_min + 1
var hyf_len;//hyf_min + hyf_max
var good_dot;//is_hyf in odd (collecting) passes, err_hyf in even passes
var bad_dot;//no_hyf in odd (collecting) passes, found_hyf in even passes
var dot_min;//analoguos to hyf_min
var dot_max;//analoguos to hyf_max
var dot_len;//analoguos to hyf_len
//block 91
var max_pat;//largest hyphenation value found in any pattern
/**
* Some helper functions
*/
const cp = require("child_process");
const logger = cp.fork(require("path").resolve(__dirname, "logger.js"));
function print(s) {
logger.send(s);
//process.stdout.write(s);
}
function println(s) {
logger.send(s + "\n");
//process.stdout.write(s + "\n");
}
//block 10
function error(msg) {
println(msg);
process.exit(0);
}
function overflow(msg1) {
error("PATGEN capacity exceeded, sorry [" + msg1 + "].");
}
/**
* initialize() sets up xint, xext and xclass with characters that are language independent
*/
function initialize() {
//setup internal representation for digits
String("0123456789").split("").forEach(function (c, i) {
var cc = c.charCodeAt(0);
xext[i] = String.fromCharCode(cc);
xint[cc] = i;
xclass[cc] = digit_class;
});
//setup IR for ".": the edge_of_word marker (can occur in pattern files)
xext[10] = ".";
xint[46] = 10;
xclass[46] = letter_class;
edge_of_word = 10;
//setup IR for "#": the err_hyf marker
xext[11] = "#";
xint[35] = 11;
xclass[35] = hyf_class;
//setup IR for "-": the is_hyf marker (occurs in dictionary)
xext[12] = "-";
xint[45] = 12;
xclass[45] = hyf_class;
//setup IR for "*": the found_hyf marker
xext[13] = "*";
xint[42] = 13;
xclass[42] = hyf_class;
}
//block 35
function first_fit() {
var s;
var t;
var q;
var doSearchLoop = true;
var continueLoop = false;
//block 36
t = (qmax > qmax_thresh)
? trie_r[trie_max + 1]
: 0;
while (doSearchLoop) {
continueLoop = false;
t = trie_l[t];
s = t - trieq_c[1];
//block 37
if (s > trie_size - cnum) {
overflow(trie_size + " pattern trie nodes");
}
while (trie_bmax < s) {
trie_bmax += 1;
trie_taken[trie_bmax] = 0;
trie_c[trie_bmax + cmax] = 0;
trie_l[trie_bmax + cmax] = trie_bmax + cnum;
trie_r[trie_bmax + cnum] = trie_bmax + cmax;
}
// end block 37
if (trie_taken[s] === 0) {
q = qmax;
while (q >= 2) {
if (trie_c[s + trieq_c[q]] !== 0) {
continueLoop = true;
}
q -= 1;
}
} else {
continueLoop = true;
}
doSearchLoop = continueLoop;
}
//end block 36
q = 1;
while (q <= qmax) {
t = s + trieq_c[q];
trie_l[trie_r[t]] = trie_l[t];
trie_r[trie_l[t]] = trie_r[t];
trie_c[t] = trieq_c[q];
trie_l[t] = trieq_l[q];
trie_r[t] = trieq_r[q];
if (t > trie_max) {
trie_max = t;
}
q += 1;
}
trie_taken[s] = 1;
return s;
}
//block 38
function unpack(s) {
var c;
var t;
qmax = 1;
c = cmin;
while (c <= cmax) {
t = s + c;
if (trie_c[t] === c) {
trieq_c[qmax] = c;
trieq_l[qmax] = trie_l[t];
trieq_r[qmax] = trie_r[t];
qmax += 1;
trie_r[trie_l[0]] = t;
trie_l[t] = trie_l[0];
trie_l[0] = t;
trie_r[t] = 0;
trie_c[t] = 0;
}
c += 1;
}
trie_taken[s] = 0;
}
//block 34
function init_pattern_trie() {
var c = 0; //internal_code
var h = 1; //opt_type
while (c <= cmax) {
trie_c[trie_root + c] = c;
trie_l[trie_root + c] = 0;
trie_r[trie_root + c] = 0;
trie_taken[trie_root + c] = 0;
c += 1;
}
trie_taken[trie_root] = 1;
trie_bmax = trie_root;
trie_max = trie_root + cmax;
trie_count = cnum;
qmax_thresh = 5;
trie_l[0] = trie_max + 1;
trie_r[trie_max + 1] = 0;
while (h <= max_ops) {
ops[h] = {val: 0, dot: 0, op: 0};
h += 1;
}
op_count = 0;
}
//block 39
function new_trie_op(v, d, n) {
var h;
h = ((n + 313 * d + 361 * v) % max_ops) + 1;
while (true) {
if (ops[h].val === 0) {
op_count += 1;
if (op_count === max_ops) {
overflow(max_ops + " outputs");
}
ops[h].val = v;
ops[h].dot = d;
ops[h].op = n;
return h;
}
if (ops[h].val === v && ops[h].dot === d && ops[h].op === n) {
return h;
}
if (h > 1) {
h -= 1;
} else {
h = max_ops;
}
}
}
//block 41
function insert_pattern(val, dot) {
var i;
var s;
var t;
i = 1;
s = trie_root + pat[i];
t = trie_l[s];
while (t > 0 && i < pat_len) {
i += 1;
t += pat[i];
if (trie_c[t] !== pat[i]) {
//begin block 42
if (trie_c[t] === 0) {
trie_l[trie_r[t]] = trie_l[t];
trie_r[trie_l[t]] = trie_r[t];
trie_c[t] = pat[i];
trie_l[t] = 0;
trie_r[t] = 0;
if (t > trie_max) {
trie_max = t;
}
} else {
unpack(t - pat[i]);
trieq_c[qmax] = pat[i];
trieq_l[qmax] = 0;
trieq_r[qmax] = 0;
t = first_fit();
trie_l[s] = t;
t += pat[i];
}
trie_count += 1;
//end block 42
}
s = t;
t = trie_l[s];
}
trieq_l[1] = 0;
trieq_r[1] = 0;
qmax = 1;
while (i < pat_len) {
i += 1;
trieq_c[1] = pat[i];
t = first_fit();
trie_l[s] = t;
s = t + pat[i];
trie_count += 1;
}
trie_r[s] = new_trie_op(val, dot, trie_r[s]);
}
function init_count_trie() {
var c = 0;
while (c <= cmax) {
triec_c[triec_root + c] = c;
triec_l[triec_root + c] = 0;
triec_r[triec_root + c] = 0;
triec_taken[triec_root + c] = 0;
c += 1;
}
triec_taken[triec_root] = 1;
triec_bmax = triec_root;
triec_max = triec_root + cmax;
triec_count = cnum;
triec_kmax = 4096;
triec_l[0] = triec_max + 1;
triec_r[triec_max + 1] = 0;
pat_count = 0;
}
//block 45
function firstc_fit() {
var a;
var b;
var q;
var doSearchLoop = true;
var continueLoop = false;
//begin block 46
if (qmax > 3) {
a = triec_r[triec_max + 1];
} else {
a = 0;
}
while (doSearchLoop) {
continueLoop = false;
a = triec_l[a];
b = a - trieq_c[1];
//begin block 47
if (b > (triec_kmax - cnum)) {
if (triec_kmax === triec_size) {
overflow(triec_size + " count trie nodes");
}
print(~~(triec_kmax / 1024) + "K ");
if (triec_kmax > (triec_size - 4096)) {
triec_kmax = triec_size;
} else {
triec_kmax += 4096;
}
}
while (triec_bmax < b) {
triec_bmax += 1;
triec_taken[triec_bmax] = 0;
triec_c[triec_bmax + cmax] = 0;
triec_l[triec_bmax + cmax] = triec_bmax + cnum;
triec_r[triec_bmax + cnum] = triec_bmax + cmax;
}
//end block 47
if (triec_taken[b] === 0) {
q = qmax;
while (q >= 2) {
if (triec_c[b + trieq_c[q]] !== 0) {
continueLoop = true;
}
q -= 1;
}
} else {
continueLoop = true;
}
doSearchLoop = continueLoop;
}
//end block 46
q = 1;
while (q <= qmax) {
a = b + trieq_c[q];
triec_l[triec_r[a]] = triec_l[a];
triec_r[triec_l[a]] = triec_r[a];
triec_c[a] = trieq_c[q];
triec_l[a] = trieq_l[q];
triec_r[a] = trieq_r[q];
if (a > triec_max) {
triec_max = a;
}
q += 1;
}
triec_taken[b] = 1;
return b;
}
//block 48
function unpackc(b) {
var c = cmin;
var a;
qmax = 1;
while (c <= cmax) {
a = b + c;
if (triec_c[a] === c) {
trieq_c[qmax] = c;
trieq_l[qmax] = triec_l[a];
trieq_r[qmax] = triec_r[a];
qmax += 1;
triec_r[triec_l[0]] = a;
triec_l[a] = triec_l[0];
triec_l[0] = a;
triec_r[a] = 0;
triec_c[a] = 0;
}
c += 1;
}
triec_taken[b] = 0;
}
//block 49
function insertc_pat(fpos) {
var spos;
var a;
var b;
spos = fpos - pat_len;
spos += 1;
b = triec_root + word[spos];
a = triec_l[b];
while (a > 0 && spos < fpos) {
spos += 1;
a += word[spos];
if (triec_c[a] !== word[spos]) {
//begin block 50
if (triec_c[a] === 0) {
triec_l[triec_r[a]] = triec_l[a];
triec_r[triec_l[a]] = triec_r[a];
triec_c[a] = word[spos];
triec_l[a] = 0;
triec_r[a] = 0;
if (a > triec_max) {
triec_max = a;
}
} else {
unpackc(a - word[spos]);
trieq_c[qmax] = word[spos];
trieq_l[qmax] = 0;
trieq_r[qmax] = 0;
a = firstc_fit();
triec_l[b] = a;
a += word[spos];
}
triec_count += 1;
//end block 50
}
b = a;
a = triec_l[a];
}
trieq_l[1] = 0;
trieq_r[1] = 0;
qmax = 1;
while (spos < fpos) {
spos += 1;
trieq_c[1] = word[spos];
a = firstc_fit();
triec_l[b] = a;
b = a + word[spos];
triec_count += 1;
}
pat_count += 1;
return b;
}
//block 51
var pattmp = makeFile("");
//block 52
var buf = [];
var buf_ptr;
//block 53
function print_buf() {
println(buf.join(""));
}
function bad_input(msg) {
print_buf();
error(msg);
}
//block 52
function read_buf(file) {
buf = [];
do {
buf.push(file.data[1][file.data[0]]);
file.data[0] += 1;
} while (file.data[1][file.data[0]] !== "\n");
buf_len = buf.length;
file.data[0] += 1;
}
//block 61
function find_letters(b, i) {
var c = cmin;
var a;
var j;
var l;
if (i === 0) {
init_count_trie();
}
while (c <= cmax) {
a = b + c;
if (trie_c[a] === c) {
pat[i] = c;
if (trie_r[a] === 0) {
find_letters(trie_l[a], i + 1);
} else if (trie_l[a] === 0) {
//begin block 62
l = triec_root + trie_r[a];
j = 1;
while (j <= i - 1) {
if (triec_max === triec_size) {
overflow(triec_size + " count trie nodes (fl)");
}
triec_max += 1;
triec_l[l] = triec_max;
l = triec_max;
triec_c[l] = pat[j];
j += 1;
}
triec_l[l] = 0;
//end block 62
}
}
c += 1;
}
}
//block 64
function traverse_count_trie(b, i) {
var c = cmin;
var a;
while (c <= cmax) {
a = b + c;
if (triec_c[a] === c) {
pat[i] = c;
if (i < pat_len) {
traverse_count_trie(triec_l[a], i + 1);
} else {
//begin block 65
if ((good_wt * triec_l[a]) < thresh) { //hopeless pattern
//println("HOPELESS:", pat.map(v => xext[v]), triec_l[a], triec_r[a]);
insert_pattern(max_val, pat_dot);
bad_pat_count += 1;
} else if ((good_wt * triec_l[a] - bad_wt * triec_r[a]) >= thresh) { //good pattern
//println(`INSERT: ${pat.map(v => xext[v])}, ${triec_l[a]}, ${triec_r[a]}`);
insert_pattern(hyph_level, pat_dot);
good_pat_count += 1;
good_count += triec_l[a];
bad_count += triec_r[a];
} else {
//println("MORE2COME:", pat.map(v => xext[v]), triec_l[a], triec_r[a]); //can't decide yet
more_to_come = true;
}
//end block 65
}
}
c += 1;
}
}
//block 67
function collect_count_trie() {
good_pat_count = 0;
bad_pat_count = 0;
good_count = 0;
bad_count = 0;
more_to_come = false;
traverse_count_trie(triec_root, 1);
print(good_pat_count + " good and " + bad_pat_count + " bad patterns added");
level_pattern_count += good_pat_count;
if (more_to_come) {
println(" (more to come)");
} else {
println(" ");
}
print("finding " + good_count + " good and " + bad_count + " bad hyphens");
if (good_pat_count > 0) {
println(" efficiency = " + Number(good_count / (good_pat_count + bad_count / (thresh / good_wt))).toFixed(2));
} else {
println(" ");
}
println("pattern trie has " + trie_count + " nodes, trie_max = " + trie_max + ", " + op_count + " outputs");
}
//block 68
function delete_patterns(s) {
var c = cmin;
var t;
var all_freed;
var h;
var n;
all_freed = true;
while (c <= cmax) {
t = s + c;
if (trie_c[t] === c) {
//begin block 69
h = 0;
ops[0] = {op: trie_r[t]};
n = ops[0].op;
while (n > 0) {
if (ops[n].val === max_val) {
ops[h].op = ops[n].op;
} else {
h = n;
}
n = ops[h].op;
}
trie_r[t] = ops[0].op;
//end block 69
if (trie_l[t] > 0) {
trie_l[t] = delete_patterns(trie_l[t]);
}
if (trie_l[t] > 0 || trie_r[t] > 0 || s === trie_root) {
all_freed = false;
} else {
//begin block 70
trie_l[trie_r[trie_max + 1]] = t;
trie_r[t] = trie_r[trie_max + 1];
trie_l[t] = trie_max + 1;
trie_r[trie_max + 1] = t;
trie_c[t] = 0;
trie_count -= 1;
//end block 70
}
}
c += 1;
}
if (all_freed) {
trie_taken[s] = 0;
s = 0;
}
return s;
}
//block 71
function delete_bad_patterns() {
var old_op_count;
var old_trie_count;
var h = 1;
old_op_count = op_count;
old_trie_count = trie_count;