-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
1406 lines (1203 loc) · 43 KB
/
parser.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
/**
* This is SamJs.js v0.1.3
*
* A Javascript port of "SAM Software Automatic Mouth".
*
* (c) 2017-2021 Christian Schiffler
*
* @link(https://github.com/discordier/sam)
*
* @author 2017 Christian Schiffler <[email protected]>
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Parser = factory());
})(this, (function () { 'use strict';
let StressTable = '*12345678'.split('');
let PhonemeNameTable = (' *' + // 00
'.*' + // 01
'?*' + // 02
',*' + // 03
'-*' + // 04
'IY' + // 05
'IH' + // 06
'EH' + // 07
'AE' + // 08
'AA' + // 09
'AH' + // 10
'AO' + // 11
'UH' + // 12
'AX' + // 13
'IX' + // 14
'ER' + // 15
'UX' + // 16
'OH' + // 17
'RX' + // 18
'LX' + // 19
'WX' + // 20
'YX' + // 21
'WH' + // 22
'R*' + // 23
'L*' + // 24
'W*' + // 25
'Y*' + // 26
'M*' + // 27
'N*' + // 28
'NX' + // 29
'DX' + // 30
'Q*' + // 31
'S*' + // 32
'SH' + // 33
'F*' + // 34
'TH' + // 35
'/H' + // 36
'/X' + // 37
'Z*' + // 38
'ZH' + // 39
'V*' + // 40
'DH' + // 41
'CH' + // 42
'**' + // 43
'J*' + // 44
'**' + // 45
'**' + // 46
'**' + // 47
'EY' + // 48
'AY' + // 49
'OY' + // 50
'AW' + // 51
'OW' + // 52
'UW' + // 53
'B*' + // 54
'**' + // 55
'**' + // 56
'D*' + // 57
'**' + // 58
'**' + // 59
'G*' + // 60
'**' + // 61
'**' + // 62
'GX' + // 63
'**' + // 64
'**' + // 65
'P*' + // 66
'**' + // 67
'**' + // 68
'T*' + // 69
'**' + // 70
'**' + // 71
'K*' + // 72
'**' + // 73
'**' + // 74
'KX' + // 75
'**' + // 76
'**' + // 77
'UL' + // 78
'UM' + // 79
'UN' // 80
).match(/.{1,2}/g);
/**
* Flags for phoneme names.
*
* Merged from the original two tables via: oldFlags[i] | (oldFlags2[i] << 8)
*
* 0x8000
* ' *', '.*', '?*', ',*', '-*'
* 0x4000
* '.*', '?*', ',*', '-*', 'Q*'
* 0x2000 FLAG_FRICATIVE
* 'S*', 'SH', 'F*', 'TH', 'Z*', 'ZH', 'V*', 'DH', 'CH', '**', '**'
* 0x1000 FLAG_LIQUIC
* 'R*', 'L*', 'W*', 'Y*'
* 0x0800 FLAG_NASAL
* 'M*', 'N*', 'NX'
* 0x0400 FLAG_ALVEOLAR
* 'N*', 'DX', 'S*', 'TH', 'Z*', 'DH', 'D*', '**', '**', 'T*', '**',
* '**'
* 0x0200
* --- not used ---
* 0x0100 FLAG_PUNCT
* '.*', '?*', ',*', '-*'
* 0x0080 FLAG_VOWEL
* 'IY', 'IH', 'EH', 'AE', 'AA', 'AH', 'AO', 'UH', 'AX', 'IX', 'ER',
* 'UX', 'OH', 'RX', 'LX', 'WX', 'YX', 'EY', 'AY', 'OY', 'AW', 'OW',
* 'UW', 'UL', 'UM', 'UN'
* 0x0040 FLAG_CONSONANT
* 'WH', 'R*', 'L*', 'W*', 'Y*', 'M*', 'N*', 'NX', 'DX', 'Q*', 'S*',
* 'SH', 'F*', 'TH', '/H', '/X', 'Z*', 'ZH', 'V*', 'DH', 'CH', '**',
* 'J*', '**', 'B*', '**', '**', 'D*', '**', '**', 'G*', '**', '**',
* 'GX', '**', '**', 'P*', '**', '**', 'T*', '**', '**', 'K*', '**',
* '**', 'KX', '**', '**', 'UM', 'UN'
* 0x0020 FLAG_DIP_YX but looks like front vowels
* 'IY', 'IH', 'EH', 'AE', 'AA', 'AH', 'AX', 'IX', 'EY', 'AY', 'OY'
* 0x0010 FLAG_DIPTHONG
* 'EY', 'AY', 'OY', 'AW', 'OW', 'UW'
* 0x0008
* 'M*', 'N*', 'NX', 'DX', 'Q*', 'CH', 'J*', 'B*', '**', '**', 'D*',
* '**', '**', 'G*', '**', '**', 'GX', '**', '**', 'P*', '**', '**',
* 'T*', '**', '**', 'K*', '**', '**', 'KX', '**', '**'
* 0x0004 FLAG_VOICED
* 'IY', 'IH', 'EH', 'AE', 'AA', 'AH', 'AO', 'UH', 'AX', 'IX', 'ER',
* 'UX', 'OH', 'RX', 'LX', 'WX', 'YX', 'WH', 'R*', 'L*', 'W*', 'Y*',
* 'M*', 'N*', 'NX', 'Q*', 'Z*', 'ZH', 'V*', 'DH', 'J*', '**', 'EY',
* 'AY', 'OY', 'AW', 'OW', 'UW', 'B*', '**', '**', 'D*', '**', '**',
* 'G*', '**', '**', 'GX', '**', '**'
* 0x0002 FLAG_STOPCONS
* 'B*', '**', '**', 'D*', '**', '**', 'G*', '**', '**', 'GX', '**',
* '**', 'P*', '**', '**', 'T*', '**', '**', 'K*', '**', '**', 'KX',
* '**', '**'
* 0x0001 FLAG_UNVOICED_STOPCONS
* 'P*', '**', '**', 'T*', '**', '**', 'K*', '**', '**', 'KX', '**',
* '**', 'UM', 'UN'
*/
let phonemeFlags = [0 | 0x8000, // ' *' 00
0 | 0x8000 | 0x4000 | 0x0100, // '.*' 01
0 | 0x8000 | 0x4000 | 0x0100, // '?*' 02
0 | 0x8000 | 0x4000 | 0x0100, // ',*' 03
0 | 0x8000 | 0x4000 | 0x0100, // '-*' 04
0 | 0x0080 | 0x0020 | 0x0004, // 'IY' 05
0 | 0x0080 | 0x0020 | 0x0004, // 'IH' 06
0 | 0x0080 | 0x0020 | 0x0004, // 'EH' 07
0 | 0x0080 | 0x0020 | 0x0004, // 'AE' 08
0 | 0x0080 | 0x0020 | 0x0004, // 'AA' 09
0 | 0x0080 | 0x0020 | 0x0004, // 'AH' 10
0 | 0x0080 | 0x0004, // 'AO' 11
0 | 0x0080 | 0x0004, // 'UH' 12
0 | 0x0080 | 0x0020 | 0x0004, // 'AX' 13
0 | 0x0080 | 0x0020 | 0x0004, // 'IX' 14
0 | 0x0080 | 0x0004, // 'ER' 15
0 | 0x0080 | 0x0004, // 'UX' 16
0 | 0x0080 | 0x0004, // 'OH' 17
0 | 0x0080 | 0x0004, // 'RX' 18
0 | 0x0080 | 0x0004, // 'LX' 19
0 | 0x0080 | 0x0004, // 'WX' 20
0 | 0x0080 | 0x0004, // 'YX' 21
0 | 0x0040 | 0x0004, // 'WH' 22
0 | 0x1000 | 0x0040 | 0x0004, // 'R*' 23
0 | 0x1000 | 0x0040 | 0x0004, // 'L*' 24
0 | 0x1000 | 0x0040 | 0x0004, // 'W*' 25
0 | 0x1000 | 0x0040 | 0x0004, // 'Y*' 26
0 | 0x0800 | 0x0040 | 0x0008 | 0x0004, // 'M*' 27
0 | 0x0800 | 0x0400 | 0x0040 | 0x0008 | 0x0004, // 'N*' 28
0 | 0x0800 | 0x0040 | 0x0008 | 0x0004, // 'NX' 29
0 | 0x0400 | 0x0040 | 0x0008, // 'DX' 30
0 | 0x4000 | 0x0040 | 0x0008 | 0x0004, // 'Q*' 31
0 | 0x2000 | 0x0400 | 0x0040, // 'S*' 32
0 | 0x2000 | 0x0040, // 'SH' 33
0 | 0x2000 | 0x0040, // 'F*' 34
0 | 0x2000 | 0x0400 | 0x0040, // 'TH' 35
0 | 0x0040, // '/H' 36
0 | 0x0040, // '/X' 37
0 | 0x2000 | 0x0400 | 0x0040 | 0x0004, // 'Z*' 38
0 | 0x2000 | 0x0040 | 0x0004, // 'ZH' 39
0 | 0x2000 | 0x0040 | 0x0004, // 'V*' 40
0 | 0x2000 | 0x0400 | 0x0040 | 0x0004, // 'DH' 41
0 | 0x2000 | 0x0040 | 0x0008, // 'CH' 42
0 | 0x2000 | 0x0040, // '**' 43
0 | 0x0040 | 0x0008 | 0x0004, // 'J*' 44
0 | 0x2000 | 0x0040 | 0x0004, // '**' 45
0, // '**' 46
0, // '**' 47
0 | 0x0080 | 0x0020 | 0x0010 | 0x0004, // 'EY' 48
0 | 0x0080 | 0x0020 | 0x0010 | 0x0004, // 'AY' 49
0 | 0x0080 | 0x0020 | 0x0010 | 0x0004, // 'OY' 50
0 | 0x0080 | 0x0010 | 0x0004, // 'AW' 51
0 | 0x0080 | 0x0010 | 0x0004, // 'OW' 52
0 | 0x0080 | 0x0010 | 0x0004, // 'UW' 53
0 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // 'B*' 54
0 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // '**' 55
0 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // '**' 56
0 | 0x0400 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // 'D*' 57
0 | 0x0400 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // '**' 58
0 | 0x0400 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // '**' 59
0 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // 'G*' 60
0 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // '**' 61
0 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // '**' 62
0 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // 'GX' 63
0 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // '**' 64
0 | 0x0040 | 0x0008 | 0x0004 | 0x0002, // '**' 65
0 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // 'P*' 66
0 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // '**' 67
0 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // '**' 68
0 | 0x0400 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // 'T*' 69
0 | 0x0400 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // '**' 70
0 | 0x0400 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // '**' 71
0 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // 'K*' 72
0 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // '**' 73
0 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // '**' 74
0 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // 'KX' 75
0 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // '**' 76
0 | 0x0040 | 0x0008 | 0x0002 | 0x0001, // '**' 77
0 | 0x0080, // 'UL' 78
0 | 0x0080 | 0x0040 | 0x0001, // 'UM' 79
0 | 0x0080 | 0x0040 | 0x0001 // 'UN' 80
];
/**
* Combined table of phoneme length.
*
* Merged from the original two tables via: phonemeLengthTable[i] | (phonemeStressedLengthTable[i] << 8)
*
* Use via:
* phonemeLengthTable[i] = combinedPhonemeLengthTable[i] & 0xFF
* phonemeStressedLengthTable[i] = combinedPhonemeLengthTable[i] >> 8
*/
let combinedPhonemeLengthTable = [0x0000 | 0x0000, // ' *' 00
0x0012 | 0x1200, // '.*' 01
0x0012 | 0x1200, // '?*' 02
0x0012 | 0x1200, // ',*' 03
0x0008 | 0x0800, // '-*' 04
0x0008 | 0x0B00, // 'IY' 05
0x0008 | 0x0900, // 'IH' 06
0x0008 | 0x0B00, // 'EH' 07
0x0008 | 0x0E00, // 'AE' 08
0x000B | 0x0F00, // 'AA' 09
0x0006 | 0x0B00, // 'AH' 10
0x000C | 0x1000, // 'AO' 11
0x000A | 0x0C00, // 'UH' 12
0x0005 | 0x0600, // 'AX' 13
0x0005 | 0x0600, // 'IX' 14
0x000B | 0x0E00, // 'ER' 15
0x000A | 0x0C00, // 'UX' 16
0x000A | 0x0E00, // 'OH' 17
0x000A | 0x0C00, // 'RX' 18
0x0009 | 0x0B00, // 'LX' 19
0x0008 | 0x0800, // 'WX' 20
0x0007 | 0x0800, // 'YX' 21
0x0009 | 0x0B00, // 'WH' 22
0x0007 | 0x0A00, // 'R*' 23
0x0006 | 0x0900, // 'L*' 24
0x0008 | 0x0800, // 'W*' 25
0x0006 | 0x0800, // 'Y*' 26
0x0007 | 0x0800, // 'M*' 27
0x0007 | 0x0800, // 'N*' 28
0x0007 | 0x0800, // 'NX' 29
0x0002 | 0x0300, // 'DX' 30
0x0005 | 0x0500, // 'Q*' 31
0x0002 | 0x0200, // 'S*' 32
0x0002 | 0x0200, // 'SH' 33
0x0002 | 0x0200, // 'F*' 34
0x0002 | 0x0200, // 'TH' 35
0x0002 | 0x0200, // '/H' 36
0x0002 | 0x0200, // '/X' 37
0x0006 | 0x0600, // 'Z*' 38
0x0006 | 0x0600, // 'ZH' 39
0x0007 | 0x0800, // 'V*' 40
0x0006 | 0x0600, // 'DH' 41
0x0006 | 0x0600, // 'CH' 42
0x0002 | 0x0200, // '**' 43
0x0008 | 0x0900, // 'J*' 44
0x0003 | 0x0400, // '**' 45
0x0001 | 0x0200, // '**' 46
0x001E | 0x0100, // '**' 47
0x000D | 0x0E00, // 'EY' 48
0x000C | 0x0F00, // 'AY' 49
0x000C | 0x0F00, // 'OY' 50
0x000C | 0x0F00, // 'AW' 51
0x000E | 0x0E00, // 'OW' 52
0x0009 | 0x0E00, // 'UW' 53
0x0006 | 0x0800, // 'B*' 54
0x0001 | 0x0200, // '**' 55
0x0002 | 0x0200, // '**' 56
0x0005 | 0x0700, // 'D*' 57
0x0001 | 0x0200, // '**' 58
0x0001 | 0x0100, // '**' 59
0x0006 | 0x0700, // 'G*' 60
0x0001 | 0x0200, // '**' 61
0x0002 | 0x0200, // '**' 62
0x0006 | 0x0700, // 'GX' 63
0x0001 | 0x0200, // '**' 64
0x0002 | 0x0200, // '**' 65
0x0008 | 0x0800, // 'P*' 66
0x0002 | 0x0200, // '**' 67
0x0002 | 0x0200, // '**' 68
0x0004 | 0x0600, // 'T*' 69
0x0002 | 0x0200, // '**' 70
0x0002 | 0x0200, // '**' 71
0x0006 | 0x0700, // 'K*' 72
0x0001 | 0x0200, // '**' 73
0x0004 | 0x0400, // '**' 74
0x0006 | 0x0700, // 'KX' 75
0x0001 | 0x0100, // '**' 76
0x0004 | 0x0400, // '**' 77
0x00C7 | 0x0500, // 'UL' 78
0x00FF | 0x0500 // 'UM' 79
];
/*
Ind | phoneme | flags |
-----|---------|----------|
0 | * | 00000000 |
1 | .* | 00000000 |
2 | ?* | 00000000 |
3 | ,* | 00000000 |
4 | -* | 00000000 |
VOWELS
5 | IY | 10100100 |
6 | IH | 10100100 |
7 | EH | 10100100 |
8 | AE | 10100100 |
9 | AA | 10100100 |
10 | AH | 10100100 |
11 | AO | 10000100 |
17 | OH | 10000100 |
12 | UH | 10000100 |
16 | UX | 10000100 |
15 | ER | 10000100 |
13 | AX | 10100100 |
14 | IX | 10100100 |
DIPHTONGS
48 | EY | 10110100 |
49 | AY | 10110100 |
50 | OY | 10110100 |
51 | AW | 10010100 |
52 | OW | 10010100 |
53 | UW | 10010100 |
21 | YX | 10000100 |
20 | WX | 10000100 |
18 | RX | 10000100 |
19 | LX | 10000100 |
37 | /X | 01000000 |
30 | DX | 01001000 |
22 | WH | 01000100 |
VOICED CONSONANTS
23 | R* | 01000100 |
24 | L* | 01000100 |
25 | W* | 01000100 |
26 | Y* | 01000100 |
27 | M* | 01001100 |
28 | N* | 01001100 |
29 | NX | 01001100 |
54 | B* | 01001110 |
57 | D* | 01001110 |
60 | G* | 01001110 |
44 | J* | 01001100 |
38 | Z* | 01000100 |
39 | ZH | 01000100 |
40 | V* | 01000100 |
41 | DH | 01000100 |
unvoiced CONSONANTS
32 | S* | 01000000 |
33 | SH | 01000000 |
34 | F* | 01000000 |
35 | TH | 01000000 |
66 | P* | 01001011 |
69 | T* | 01001011 |
72 | K* | 01001011 |
42 | CH | 01001000 |
36 | /H | 01000000 |
43 | ** | 01000000 |
45 | ** | 01000100 |
46 | ** | 00000000 |
47 | ** | 00000000 |
55 | ** | 01001110 |
56 | ** | 01001110 |
58 | ** | 01001110 |
59 | ** | 01001110 |
61 | ** | 01001110 |
62 | ** | 01001110 |
63 | GX | 01001110 |
64 | ** | 01001110 |
65 | ** | 01001110 |
67 | ** | 01001011 |
68 | ** | 01001011 |
70 | ** | 01001011 |
71 | ** | 01001011 |
73 | ** | 01001011 |
74 | ** | 01001011 |
75 | KX | 01001011 |
76 | ** | 01001011 |
77 | ** | 01001011 |
SPECIAL
78 | UL | 10000000 |
79 | UM | 11000001 |
80 | UN | 11000001 |
31 | Q* | 01001100 |
*/
/**
* Match both characters but not with wildcards.
*
* @param {string} sign1
* @param {string} sign2
* @return {boolean|Number}
*/
let full_match = (sign1, sign2) => {
let index = PhonemeNameTable.findIndex(value => {
return value === sign1 + sign2 && value[1] !== '*';
});
return index !== -1 ? index : false;
};
/**
* Match character with wildcard.
*
* @param {string} sign1
* @return {boolean|Number}
*/
let wild_match = sign1 => {
let index = PhonemeNameTable.findIndex(value => {
return value === sign1 + '*';
});
return index !== -1 ? index : false;
};
/**
* The input[] buffer contains a string of phonemes and stress markers along
* the lines of:
*
* DHAX KAET IHZ AH5GLIY.
*
* Some phonemes are 2 bytes long, such as "DH" and "AX".
* Others are 1 byte long, such as "T" and "Z".
* There are also stress markers, such as "5" and ".".
*
* The characters of the phonemes are stored in the table PhonemeNameTable.
* The stress characters are arranged in low to high stress order in StressTable[].
*
* The following process is used to parse the input buffer:
*
* Repeat until the end is reached:
* 1. First, a search is made for a 2 character match for phonemes that do not
* end with the '*' (wildcard) character. On a match, the index of the phoneme
* is added to the result and the buffer position is advanced 2 bytes.
*
* 2. If this fails, a search is made for a 1 character match against all
* phoneme names ending with a '*' (wildcard). If this succeeds, the
* phoneme is added to result and the buffer position is advanced
* 1 byte.
*
* 3. If this fails, search for a 1 character match in the stressInputTable[].
* If this succeeds, the stress value is placed in the last stress[] table
* at the same index of the last added phoneme, and the buffer position is
* advanced by 1 byte.
*
* If this fails, return false.
*
* On success:
*
* 1. phonemeIndex[] will contain the index of all the phonemes.
* 2. The last index in phonemeIndex[] will be 255.
* 3. stress[] will contain the stress value for each phoneme
*
* input holds the string of phonemes, each two bytes wide
* signInputTable1[] holds the first character of each phoneme
* signInputTable2[] holds the second character of each phoneme
* phonemeIndex[] holds the indexes of the phonemes after parsing input[]
*
* The parser scans through the input[], finding the names of the phonemes
* by searching signInputTable1[] and signInputTable2[]. On a match, it
* copies the index of the phoneme into the phonemeIndexTable[].
*
* @param {string} input Holds the string of phonemes, each two bytes wide.
* @param {function} addPhoneme The callback to use to store phoneme index values.
* @param {function} addStress The callback to use to store stress index values.
*
* @return {undefined}
*/
var Parser1 = ((input, addPhoneme, addStress) => {
for (let srcPos = 0; srcPos < input.length; srcPos++) {
{
let tmp = input.toLowerCase();
console.log("processing \"".concat(tmp.substr(0, srcPos), "%c").concat(tmp.substr(srcPos, 2).toUpperCase(), "%c").concat(tmp.substr(srcPos + 2), "\""), 'color: red;', 'color:normal;');
}
let sign1 = input[srcPos];
let sign2 = input[srcPos + 1] || '';
let match;
if ((match = full_match(sign1, sign2)) !== false) {
// Matched both characters (no wildcards)
srcPos++; // Skip the second character of the input as we've matched it
addPhoneme(match);
continue;
}
if ((match = wild_match(sign1)) !== false) {
// Matched just the first character (with second character matching '*'
addPhoneme(match);
continue;
} // Should be a stress character. Search through the stress table backwards.
match = StressTable.length;
while (sign1 !== StressTable[match] && match > 0) {
--match;
}
if (match === 0) {
{
throw Error("Could not parse char ".concat(sign1));
}
}
addStress(match); // Set stress for prior phoneme
}
});
/**
* Test if a bit is set.
* @param {Number} bits The bits.
* @param {Number} mask The mask to test.
* @return {boolean}
*/
let matchesBitmask = (bits, mask) => {
return (bits & mask) !== 0;
};
/**
* Test if a phoneme has the given flag.
*
* @param {Number} phoneme The phoneme to test.
* @param {Number} flag The flag to test (see constants.es6)
*
* @return {boolean}
*/
let phonemeHasFlag = (phoneme, flag) => {
return matchesBitmask(phonemeFlags[phoneme], flag);
};
let pR = 23;
let pD = 57;
let pT = 69;
let FLAG_FRICATIVE = 0x2000;
/**
* liquic consonant
*/
let FLAG_LIQUIC = 0x1000;
let FLAG_NASAL = 0x0800;
let FLAG_ALVEOLAR = 0x0400;
let FLAG_PUNCT = 0x0100;
let FLAG_VOWEL = 0x0080;
let FLAG_CONSONANT = 0x0040;
/**
* dipthong ending with YX
*
*/
let FLAG_DIP_YX = 0x0020;
let FLAG_DIPTHONG = 0x0010;
/** unknown:
* 'M*', 'N*', 'NX', 'DX', 'Q*', 'CH', 'J*', 'B*', '**', '**', 'D*',
* '**', '**', 'G*', '**', '**', 'GX', '**', '**', 'P*', '**', '**',
* 'T*', '**', '**', 'K*', '**', '**', 'KX', '**', '**'
*/
let FLAG_0008 = 0x0008;
let FLAG_VOICED = 0x0004;
/**
* stop consonant
*/
let FLAG_STOPCONS = 0x0002;
let FLAG_UNVOICED_STOPCONS = 0x0001;
/**
* Rewrites the phonemes using the following rules:
*
* <DIPHTHONG ENDING WITH WX> -> <DIPHTHONG ENDING WITH WX> WX
* <DIPHTHONG NOT ENDING WITH WX> -> <DIPHTHONG NOT ENDING WITH WX> YX
* UL -> AX L
* UM -> AX M
* UN -> AX N
* <STRESSED VOWEL> <SILENCE> <STRESSED VOWEL> -> <STRESSED VOWEL> <SILENCE> Q <VOWEL>
* T R -> CH R
* D R -> J R
* <VOWEL> R -> <VOWEL> RX
* <VOWEL> L -> <VOWEL> LX
* G S -> G Z
* K <VOWEL OR DIPHTHONG NOT ENDING WITH IY> -> KX <VOWEL OR DIPHTHONG NOT ENDING WITH IY>
* G <VOWEL OR DIPHTHONG NOT ENDING WITH IY> -> GX <VOWEL OR DIPHTHONG NOT ENDING WITH IY>
* S P -> S B
* S T -> S D
* S K -> S G
* S KX -> S GX
* <ALVEOLAR> UW -> <ALVEOLAR> UX
* CH -> CH CH' (CH requires two phonemes to represent it)
* J -> J J' (J requires two phonemes to represent it)
* <UNSTRESSED VOWEL> T <PAUSE> -> <UNSTRESSED VOWEL> DX <PAUSE>
* <UNSTRESSED VOWEL> D <PAUSE> -> <UNSTRESSED VOWEL> DX <PAUSE>
*
* @param {insertPhoneme} insertPhoneme
* @param {setPhoneme} setPhoneme
* @param {getPhoneme} getPhoneme
* @param {getPhonemeStress} getStress
*
* @return undefined
*/
var Parser2 = ((insertPhoneme, setPhoneme, getPhoneme, getStress) => {
/**
* Rewrites:
* 'UW' => 'UX' if alveolar flag set on previous phoneme.
* 'CH' => 'CH' '**'(43)
* 'J*' => 'J*' '**'(45)
* @param phoneme
* @param pos
*/
let handleUW_CH_J = (phoneme, pos) => {
switch (phoneme) {
// 'UW' Example: NEW, DEW, SUE, ZOO, THOO, TOO
case 53:
{
// ALVEOLAR flag set?
if (phonemeHasFlag(getPhoneme(pos - 1), FLAG_ALVEOLAR)) {
{
console.log("".concat(pos, " RULE: <ALVEOLAR> UW -> <ALVEOLAR> UX"));
}
setPhoneme(pos, 16); // UX
}
break;
}
// 'CH' Example: CHEW
case 42:
{
{
console.log("".concat(pos, " RULE: CH -> CH CH+1"));
}
insertPhoneme(pos + 1, 43, getStress(pos)); // '**'
break;
}
// 'J*' Example: JAY
case 44:
{
{
console.log("".concat(pos, " RULE: J -> J J+1"));
}
insertPhoneme(pos + 1, 45, getStress(pos)); // '**'
break;
}
}
};
let changeAX = (position, suffix) => {
{
console.log("".concat(position, " RULE: ").concat(PhonemeNameTable[getPhoneme(position)], " -> AX ").concat(PhonemeNameTable[suffix]));
}
setPhoneme(position, 13); // 'AX'
insertPhoneme(position + 1, suffix, getStress(position));
};
let pos = -1;
let phoneme;
while ((phoneme = getPhoneme(++pos)) !== null) {
// Is phoneme pause?
if (phoneme === 0) {
continue;
}
if (phonemeHasFlag(phoneme, FLAG_DIPTHONG)) {
// <DIPHTHONG ENDING WITH WX> -> <DIPHTHONG ENDING WITH WX> WX
// <DIPHTHONG NOT ENDING WITH WX> -> <DIPHTHONG NOT ENDING WITH WX> YX
// Example: OIL, COW
{
console.log(!phonemeHasFlag(phoneme, FLAG_DIP_YX) ? "".concat(pos, " RULE: insert WX following diphthong NOT ending in IY sound") : "".concat(pos, " RULE: insert YX following diphthong ending in IY sound"));
} // If ends with IY, use YX, else use WX
// Insert at WX or YX following, copying the stress
// 'WX' = 20 'YX' = 21
insertPhoneme(pos + 1, phonemeHasFlag(phoneme, FLAG_DIP_YX) ? 21 : 20, getStress(pos));
handleUW_CH_J(phoneme, pos);
continue;
}
if (phoneme === 78) {
// 'UL' => 'AX' 'L*'
// Example: MEDDLE
changeAX(pos, 24);
continue;
}
if (phoneme === 79) {
// 'UM' => 'AX' 'M*'
// Example: ASTRONOMY
changeAX(pos, 27);
continue;
}
if (phoneme === 80) {
// 'UN' => 'AX' 'N*'
changeAX(pos, 28);
continue;
}
if (phonemeHasFlag(phoneme, FLAG_VOWEL) && getStress(pos)) {
// Example: FUNCTION
// RULE:
// <STRESSED VOWEL> <SILENCE> <STRESSED VOWEL> -> <STRESSED VOWEL> <SILENCE> Q <VOWEL>
// EXAMPLE: AWAY EIGHT
if (getPhoneme(pos + 1) === 0) {
// If following phoneme is a pause, get next
phoneme = getPhoneme(pos + 2);
if (phoneme !== null && phonemeHasFlag(phoneme, FLAG_VOWEL) && getStress(pos + 2)) {
{
console.log("".concat(pos + 2, " RULE: Insert glottal stop between two stressed vowels with space between them"));
}
insertPhoneme(pos + 2, 31, 0); // 31 = 'Q'
}
}
continue;
}
let priorPhoneme = pos === 0 ? null : getPhoneme(pos - 1);
if (phoneme === pR) {
// RULES FOR PHONEMES BEFORE R
switch (priorPhoneme) {
case pT:
{
// Example: TRACK
{
console.log("".concat(pos, " RULE: T* R* -> CH R*"));
}
setPhoneme(pos - 1, 42); // 'T*' 'R*' -> 'CH' 'R*'
break;
}
case pD:
{
// Example: DRY
{
console.log("".concat(pos, " RULE: D* R* -> J* R*"));
}
setPhoneme(pos - 1, 44); // 'J*'
break;
}
default:
{
if (phonemeHasFlag(priorPhoneme, FLAG_VOWEL)) {
// Example: ART
{
console.log("".concat(pos, " <VOWEL> R* -> <VOWEL> RX"));
}
setPhoneme(pos, 18); // 'RX'
}
}
}
continue;
} // 'L*'
if (phoneme === 24 && phonemeHasFlag(priorPhoneme, FLAG_VOWEL)) {
// Example: ALL
{
console.log("".concat(pos, " <VOWEL> L* -> <VOWEL> LX"));
}
setPhoneme(pos, 19); // 'LX'
continue;
} // 'G*' 'S*'
if (priorPhoneme === 60 && phoneme === 32) {
// Can't get to fire -
// 1. The G -> GX rule intervenes
// 2. Reciter already replaces GS -> GZ
{
console.log("".concat(pos, " G S -> G Z"));
}
setPhoneme(pos, 38);
continue;
} // 'G*'
if (phoneme === 60) {
// G <VOWEL OR DIPHTHONG NOT ENDING WITH IY> -> GX <VOWEL OR DIPHTHONG NOT ENDING WITH IY>
// Example: GO
let phoneme = getPhoneme(pos + 1); // If diphthong ending with YX, move continue processing next phoneme
if (!phonemeHasFlag(phoneme, FLAG_DIP_YX) && phoneme !== null) {
// replace G with GX and continue processing next phoneme
{
console.log("".concat(pos, " RULE: G <VOWEL OR DIPTHONG NOT ENDING WITH IY> -> GX <VOWEL OR DIPTHONG NOT ENDING WITH IY>"));
}
setPhoneme(pos, 63); // 'GX'
}
continue;
} // 'K*'
if (phoneme === 72) {
// K <VOWEL OR DIPHTHONG NOT ENDING WITH IY> -> KX <VOWEL OR DIPHTHONG NOT ENDING WITH IY>
// Example: COW
let Y = getPhoneme(pos + 1); // If at end, replace current phoneme with KX
if (!phonemeHasFlag(Y, FLAG_DIP_YX) || Y === null) {
// VOWELS AND DIPHTHONGS ENDING WITH IY SOUND flag set?
{
console.log("".concat(pos, " K <VOWEL OR DIPTHONG NOT ENDING WITH IY> -> KX <VOWEL OR DIPTHONG NOT ENDING WITH IY>"));
}
setPhoneme(pos, 75);
phoneme = 75;
}
} // Replace with softer version?
if (phonemeHasFlag(phoneme, FLAG_UNVOICED_STOPCONS) && priorPhoneme === 32) {
// 'S*'
// RULE:
// 'S*' 'P*' -> 'S*' 'B*'
// 'S*' 'T*' -> 'S*' 'D*'
// 'S*' 'K*' -> 'S*' 'G*'
// 'S*' 'KX' -> 'S*' 'GX'
// 'S*' 'UM' -> 'S*' '**'
// 'S*' 'UN' -> 'S*' '**'
// Examples: SPY, STY, SKY, SCOWL
{
console.log("".concat(pos, " RULE: S* ").concat(PhonemeNameTable[phoneme], " -> S* ").concat(PhonemeNameTable[phoneme - 12]));
}
setPhoneme(pos, phoneme - 12);
} else if (!phonemeHasFlag(phoneme, FLAG_UNVOICED_STOPCONS)) {
handleUW_CH_J(phoneme, pos);
} // 'T*', 'D*'
if (phoneme === 69 || phoneme === 57) {
// RULE: Soften T following vowel
// NOTE: This rule fails for cases such as "ODD"
// <UNSTRESSED VOWEL> T <PAUSE> -> <UNSTRESSED VOWEL> DX <PAUSE>
// <UNSTRESSED VOWEL> D <PAUSE> -> <UNSTRESSED VOWEL> DX <PAUSE>
// Example: PARTY, TARDY
if (pos > 0 && phonemeHasFlag(getPhoneme(pos - 1), FLAG_VOWEL)) {
phoneme = getPhoneme(pos + 1);
if (phoneme === 0) {
phoneme = getPhoneme(pos + 2);
}
if (phonemeHasFlag(phoneme, FLAG_VOWEL) && !getStress(pos + 1)) {
{
console.log("".concat(pos, " Soften T or D following vowel or ER and preceding a pause -> DX"));
}
setPhoneme(pos, 30);
}
}
continue;
}
{
console.log("".concat(pos, ": ").concat(PhonemeNameTable[phoneme]));
}
} // while
});
/**
* Applies various rules that adjust the lengths of phonemes
*
* Lengthen <!FRICATIVE> or <VOICED> between <VOWEL> and <PUNCTUATION> by 1.5
* <VOWEL> <RX | LX> <CONSONANT> - decrease <VOWEL> length by 1
* <VOWEL> <UNVOICED PLOSIVE> - decrease vowel by 1/8th
* <VOWEL> <VOICED CONSONANT> - increase vowel by 1/4 + 1
* <NASAL> <STOP CONSONANT> - set nasal = 5, consonant = 6
* <STOP CONSONANT> {optional silence} <STOP CONSONANT> - shorten both to 1/2 + 1
* <STOP CONSONANT> <LIQUID> - decrease <LIQUID> by 2
*
* @param {getPhoneme} getPhoneme Callback for retrieving phonemes.
* @param {setPhonemeLength} setLength Callback for setting phoneme length.
* @param {getPhonemeLength} getLength Callback for retrieving phoneme length.
*
* @return undefined
*/
var AdjustLengths = ((getPhoneme, setLength, getLength) => {
{
console.log("AdjustLengths()");
} // LENGTHEN VOWELS PRECEDING PUNCTUATION
//
// Search for punctuation. If found, back up to the first vowel, then
// process all phonemes between there and up to (but not including) the punctuation.
// If any phoneme is found that is a either a fricative or voiced, the duration is
// increased by (length * 1.5) + 1
// loop index
for (let position = 0; getPhoneme(position) !== null; position++) {
// not punctuation?
if (!phonemeHasFlag(getPhoneme(position), FLAG_PUNCT)) {
continue;
}
let loopIndex = position;
while (--position > 1 && !phonemeHasFlag(getPhoneme(position), FLAG_VOWEL)) {
/* back up while not a vowel */
} // If beginning of phonemes, exit loop.
if (position === 0) {
break;
} // Now handle everything between position and loopIndex
for (let vowel = position; position < loopIndex; position++) {
// test for not fricative/unvoiced or not voiced
if (!phonemeHasFlag(getPhoneme(position), FLAG_FRICATIVE) || phonemeHasFlag(getPhoneme(position), FLAG_VOICED)) {
let A = getLength(position); // change phoneme length to (length * 1.5) + 1
{
console.log(position + ' RULE: Lengthen <!FRICATIVE> or <VOICED> ' + PhonemeNameTable[getPhoneme(position)] + ' between VOWEL:' + PhonemeNameTable[getPhoneme(vowel)] + ' and PUNCTUATION:' + PhonemeNameTable[getPhoneme(position)] + ' by 1.5');
}
setLength(position, (A >> 1) + A + 1);
}
}
} // Similar to the above routine, but shorten vowels under some circumstances