-
Notifications
You must be signed in to change notification settings - Fork 0
/
spot.js
9355 lines (8393 loc) · 298 KB
/
spot.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
/* For the specified lexical item(s), which are assumed to be clitics (category is not checked),
* assign a violation for every terminal that intervenes between the right edge of the tree
* and the lexical item.
*/
function alignMorpheme(stree, ptree, clitic, direction){
if(ptree.cat !== "i" && ptree.cat !== 'iota'){
displayWarning("You are calling alignMorpheme on a tree that is not rooted in i");
}
clitic = clitic.split(';');
var leaves = getLeaves(ptree);
var cliticPos = leaves.findIndex(function(element){return clitic.indexOf(element.id) >= 0;});
if(cliticPos < 0){
console.warn("alignMorpheme(): The specified morpheme "+clitic+" was not found in this tree");
cliticPos = 0;
}
if (direction == "left"){
return cliticPos;
}
else{
return leaves.length - cliticPos - 1;
}
}
/* For the specified lexical item(s), which are assumed to be clitics (category is not checked),
* assign a violation for every terminal that intervenes between the left edge of the tree
* and the lexical item.
*/
function alignLeftMorpheme(stree, ptree, clitic){
return alignMorpheme(stree,ptree,clitic,"left");
}
/* For the specified lexical item(s), which are assumed to be clitics (category is not checked),
* assign a violation for every terminal that intervenes between the right edge of the tree
* and the lexical item.
*/
function alignRightMorpheme(stree, ptree, clitic){
return alignMorpheme(stree, ptree, clitic,"right");
}
/* Assign a violation for every node in sTree of category sCat
whose d edge is not aligned with the d edge of a node in pTree
of the prosodic category corresponding to s
For every sCat node s in sTree, find a node p in pTree of the proper category
such that the first (for align-left) leaf dominated by s has the same id as
the first leaf dominated by p.
*/
/*
* Options (all boolean):
* requireLexical: To ignore non-lexical XPs give them an attribute func: true.
* requireOvertHead: To ignore silently-headed XPs, give them an attribute silentHead: true
* maxSyntax: If true, ignore non-maximal syntactic nodes (nodes of category c that are
* dominated by another node of category c)
* minSyntax: If true, ignore non-minimal syntactic nodes (nodes of category c that dominate
* another node of category c)
* nonMaxSyntax: If true, only look at non-maximal syntactic nodes
* nonMinSyntax: If true, only look at non-minimal syntactic nodes
* maxProsody: If true, the prosodic match needs to be maximal. Passed to hasMatch.
* minProsody: If true, the prosodic match needs to be minimal. Passed to hasMatch.
* nonMaxProsody: If true, the prosodic match must be non-maximal. Passed to hasMatch.
* nonMinProsody: If true, the prosodic match must be non-minimal. Passed to hasMatch.
* customPairings: A mapping of custom pairings. Passed to catsMatch.
* */
function alignSP(sTree, pTree, sCat, d, options){
options = options || {};
var getEdge = (d==="left") ? getLeftEdge : getRightEdge;
var vCount = 0;
walkTree(sTree, function(sNode){
markMinMax(sNode);
if((sNode.cat !== sCat)
|| (options.requireLexical && sNode.func)
|| (options.requireOvertHead && sNode.silentHead)
|| (options.maxSyntax && !sNode.isMax)
|| (options.minSyntax && !sNode.isMin)
|| (options.nonMaxSyntax && sNode.isMax)
|| (options.nonMinSyntax && sNode.isMin)) // only go further if sNode has the category we're interested in
return;
var sEdge = getEdge(sNode);
if(!sEdge)
sEdge = sNode; // If sNode is a leaf (which it probably shouldn't be but depending on the tree might be),
// then look for a p-node that matches sNode itself. TODO is this a good idea?
var noMatch = true;
markMinMax(pTree);
walkTree(pTree, function(pNode){
if(!catsMatch(sCat, pNode.cat)
|| (options.maxProsody && !pNode.isMax)
|| (options.minProsody && !pNode.isMin)
|| (options.nonMaxProsody && pNode.isMax)
|| (options.nonMinProsody && pNode.isMin))
return;
var pEdge = getEdge(pNode);
if(!pEdge)
pEdge = pNode; //I'm assuming the leaves are words...
if(sEdge.id === pEdge.id){
noMatch = false;
return DONT_WALK_SUBTREES;
}
});
if(noMatch){
vCount++;
}
});
return vCount;
}
function getLeftEdge(node){
return getLeaves(node)[0];
}
function getRightEdge(node){
var leaves = getLeaves(node);
return leaves[leaves.length-1];
}
function alignPS(sTree, pTree, cat, d, options){
options = options || {};
var flippedOptions = {};
flippedOptions.maxSyntax = options.maxProsody || false;
flippedOptions.nonMaxSyntax = options.nonMaxProsody || false;
flippedOptions.minSyntax = options.minProsody || false;
flippedOptions.nonMinSyntax = options.nonMinProsody || false;
flippedOptions.maxProsody = options.maxSyntax || false;
flippedOptions.nonMaxProsody = options.nonMaxSyntax || false;
flippedOptions.minProsody = options.minSyntax || false;
flippedOptions.nonMinProsody = options.nonMinSyntax || false;
flippedOptions.requireLexical = options.requireLexical || false;
flippedOptions.requireOvertHead = options.requireOvertHead || false;
return alignSP(pTree, sTree, cat, d, flippedOptions);
}
function alignLeft(sTree, pTree, sCat, options){
options = options || {};
return alignSP(sTree, pTree, sCat, 'left', options);
}
function alignRight(sTree, pTree, sCat, options){
options = options || {};
return alignSP(sTree, pTree, sCat, 'right', options);
}
function alignLeftPS(sTree, pTree, cat, options){
options = options || {};
return alignPS(sTree, pTree, cat, 'left', options);
}
function alignRightPS(sTree, pTree, cat, options){
options = options || {};
return alignPS(sTree, pTree, cat, 'right', options);
}
// custom align functions
function alignLeftCustom(sTree, pTree, cat, options){
return alignSP(sTree, pTree, cat, 'left', options);
}
function alignRightCustom(sTree, pTree, cat, options){
return alignSP(sTree, pTree, cat, 'right', options);
}
function alignLeftPSCustom(sTree, pTree, cat, options){
return alignPS(sTree, pTree, cat, 'left', options);
}
function alignRightPSCustom(sTree, pTree, cat, options){
return alignPS(sTree, pTree, cat, 'right', options);
}
function alignFocus(sTree, pTree, cat, d){
var getEdge = (d==="left") ? getLeftEdge : getRightEdge;
var vCount = 0;
walkTree(sTree, function(sNode){
if(!sNode.foc) // only go further if sNode is a focus node
return;
var sEdge = getEdge(sNode);
if(!sEdge)
sEdge = sNode; // If sNode is a leaf (which it probably shouldn't be but depending on the tree might be),
// then look for a p-node that matches sNode itself. TODO is this a good idea?
var noMatch = true;
walkTree(pTree, function(pNode){
//!catsMatch(sCat, pNode.cat)
if(pNode.cat !== cat)
return;
var pEdge = getEdge(pNode);
if(!pEdge)
pEdge = pNode; //I'm assuming the leaves are words...
if(sEdge.id === pEdge.id){
noMatch = false;
return false;
}
});
if(noMatch)
vCount++;
});
return vCount;
}
function alignFocLeft(sTree, pTree, cat){
return alignFocus(sTree, pTree, cat, 'left');
}
function alignFocRight(sTree, pTree, cat){
return alignFocus(sTree, pTree, cat, 'right');
}
function wrap(sTree, pTree, cat){
//options = options || {};
var vCount = 0;
walkTree(sTree, function(sNode){
if(sNode.cat !== cat)
return;
var noMatch = true;
sLeaves = getLeaves(sNode);
walkTree(pTree, function(pNode){
if(!catsMatch(cat, pNode.cat))
return;
if(containsIds(getLeaves(pNode), sLeaves)){ // if the current pNode wraps our sNode
noMatch = false;
return false; // stop looking for a match
}
});
if(noMatch)
vCount++;
});
return vCount;
}
function wrapPS(sTree, pTree, cat){
return wrap(pTree, sTree, cat);
}
// Returns true if a contains b
// More precisely, if a contains a set of nodes whose ids are identical to the ids of the nodes in b.
function containsIds(a, b){
for(var i=0; i<=(a.length-b.length); i++){
var j=0;
while((j<b.length)&&(a[i+j].id === b[j].id))
j++;
if(j===b.length)
return true;
}
return false;
}
/* Function that takes a prosodic tree and returns a version annotated it with the phonological tones that it would have in Japanese or Lekeitio Basque.
Tones:
A -> H*L
left edge of phi -> LH on initial word. NB Here if there are multiple left-aligned phis, only one LH is annotated.
H following H*L within a maximal phi -> !H (downstep)
Arguments:
ptree = a prosodic tree
parentCat = prosodic category of ptree's parent
afterA = is there a preceding accent in this phi?
*/
function addJapaneseTones(ptree){
function addJapaneseTonesInner(ptree, parentCat, afterA, firstInPhi){
//Iota: No tonal diagnostics; just call recursively on the children
if(ptree.cat==='i'){
if(ptree.children && ptree.children.length){
for(var child in ptree.children)
{
child = addJapaneseTonesInner(ptree.children[child], ptree.cat, false)[0];
}
}
}
//Phi: domain for downstep
else if(ptree.cat==='phi'){
//Non-maximal phi following a pitch-drop is assigned a downstepped LH
if(parentCat === 'phi' && afterA && !firstInPhi){
ptree.tones = 'L!H';
}
//Otherwise, LH is not downstepped
else if(!firstInPhi){
ptree.tones = 'LH';
}
if(ptree.children && ptree.children.length){
for(var child in ptree.children)
{
outputs = addJapaneseTonesInner(ptree.children[child], ptree.cat, afterA, child==0);
child = outputs[0];
afterA = outputs[1];
}
}
}
else if(ptree.cat === 'w'){
//Unaccented w
if(!ptree.hasOwnProperty('accent')){
//ptree.accent = ptree.id.split('_')[0];
//accentFromId() is defined in japaneseAccent.js
ptree = accentFromId(ptree);
}
if(ptree.accent){
ptree.tones = 'H*L';
if(afterA)
ptree.tones = '!H*L';
afterA = true;
}
//Accented w
else{
ptree.tones = '-';
}
//this is only necessary if we have recursive prosodic words...
// if(
// outputs = addJapaneseTonesInner(child, ptree.cat, afterA);
// child = outputs[0];
// afterA = outputs[1];
}
else{
console.log("Unrecognized prosodic category"+ptree.cat);
ptree.tones = '-';
}
return [ptree, afterA];
}
return addJapaneseTonesInner(ptree)[0];
}
/* Function that takes a prosodic tree and returns a version annotated it with the phonological tones that it would have in Irish, according to Elfner (2012)'s diagnostics.
Tones:
left edge of non-minimal phi: LH
right edge of any phi: HL
Arguments:
ptree = a prosodic tree
parentCat = prosodic category of ptree's parent
afterA = is there a preceding accent in this phi?
*/
function addIrishTones_Elfner(ptree){
function addIrishTones_Elfner_Inner(ptree, getsRise, getsFall){
//Iota: No tonal diagnostics; just call recursively on the children
if(ptree.cat==='i'){
if(ptree.children && ptree.children.length){
for(var child in ptree.children)
{
addIrishTones_Elfner_Inner(ptree.children[child], false, false);
}
}
}
//Phi: domain for downstep
else if(ptree.cat==='phi'){
if(ptree.children && ptree.children.length){
for(var child = 0; child < ptree.children.length; child++)
{
var firstInNonMinPhi = (child === 0 && !isMinimal(ptree));
var lastInPhi = (child == (ptree.children.length-1));
//console.log(firstInNonMinPhi);
addIrishTones_Elfner_Inner(ptree.children[child], (child===0 && (getsRise || firstInNonMinPhi)), lastInPhi);
}
}
}
else if(ptree.cat === 'w'){
ptree.tones = '';
if(getsRise){
ptree.tones += 'LH';
}
if(getsFall){
ptree.tones += 'HL';
}
else if(!getsRise && !getsFall){
ptree.tones = '-';
}
}
else{
console.log("Unrecognized prosodic category"+ptree.cat);
ptree.tones = '-';
}
return ptree;
}
return addIrishTones_Elfner_Inner(ptree);
}
/* Function that takes a prosodic tree and returns a version annotated it with the phonological tones that it would have in Irish, according to our revised diagnostics.
Tones:
left edge of any phi: LH
right edge of any phi: HL
Arguments:
ptree = a prosodic tree
parentCat = prosodic category of ptree's parent
afterA = is there a preceding accent in this phi?
*/
function addIrishTones_Kalivoda(ptree){
function addIrishTones_Kalivoda_Inner(ptree, getsRise, getsFall){
//Iota: No tonal diagnostics; just call recursively on the children
if(ptree.cat==='i'){
if(ptree.children && ptree.children.length){
for(var child in ptree.children)
{
child = addIrishTones_Kalivoda_Inner(ptree.children[child], false, false);
}
}
}
else if(ptree.cat==='phi'){
if(ptree.children && ptree.children.length){
for(var child in ptree.children)
{
var firstInPhi = (child == 0);
var lastInPhi = (child == (ptree.children.length-1));
child = addIrishTones_Kalivoda_Inner(ptree.children[child], firstInPhi, lastInPhi);
}
}
}
else if(ptree.cat === 'w'){
ptree.tones = '';
if(getsRise){
ptree.tones += 'LH';
}
if(getsFall){
ptree.tones += 'HL';
}
if(!getsRise && !getsFall){
ptree.tones = '-';
}
}
else{
console.log("Unrecognized prosodic category"+ptree.cat);
ptree.tones = '-';
}
return ptree;
}
return addIrishTones_Kalivoda_Inner(ptree);
}
/* Assign a violation for every pair of adjacent sisters
with a parent of category cat
that do not have the same number of children.
*/
function balancedSistersAdj(stree, ptree, cat){
var vcount = 0;
if ((!ptree.children) || ptree.children.length === 0){
return vcount;
}
else{
if(ptree.cat===cat){
for(var i = 0; i < ptree.children.length-1; i++){
var sister1 = ptree.children[i];
var sister2 = ptree.children[i+1];
//Make sure there is a defined children array for each sister under consideration
if(!sister1.children){
sister1.children = [];
}
if(!sister2.children){
sister2.children = [];
}
//Assign a violation if the sisters do not have the same number of children
if(sister1.children.length != sister2.children.length){
vcount++;
}
}
}
for(var j = 0; j<ptree.children.length; j++){
vcount += balancedSistersAdj(stree, ptree.children[j], cat);
}
return vcount;
}
}
/* Assign a violation for every set of sisters dominated by a node of category cat
that do not all have the same number of children.
Update Oct. 2020: make category specification optional
*/
function balancedSisters(stree, ptree, cat){
var vcount = 0;
// Base case: no violation if there are no children
if ((!ptree.children) || ptree.children.length === 0){
return vcount;
}
else{
if(!cat || ptree.cat===cat){
// Base case: violation if the children have differing numbers of children
var imbalanceFound = false;
var i = 0;
while(!imbalanceFound && i < ptree.children.length-1){
var sister1 = ptree.children[i];
var sister2 = ptree.children[i+1];
//Make sure there is a defined children array for each sister under consideration
if(!sister1.children){
sister1.children = [];
}
if(!sister2.children){
sister2.children = [];
}
//Assign a violation if the sisters do not have the same number of children
if(sister1.children.length != sister2.children.length){
imbalanceFound = true;
}
i++;
}
if(imbalanceFound){
vcount++;
}
}
// Recurse for every subtree
for(var j = 0; j<ptree.children.length; j++){
vcount += balancedSisters(stree, ptree.children[j], cat);
}
return vcount;
}
}
function getChildrenOfCat(ptree, cat){
if ((!ptree.children) || ptree.children.length === 0)
return [];
var catChildren = [];
for(var i in ptree.children){
if(ptree.children[i].cat===cat)
catChildren.push(ptree.children[i]);
}
return catChildren;
}
/* Assign a violation for every set of sisters of category cat
that do not all have the same number of children.
*/
function balSisChildCat(stree, ptree, cat){
var vcount = 0;
// Base case: no violation if there are no children
if ((!ptree.children) || ptree.children.length === 0){
return vcount;
}
// If there are children:
else
{
// Base case: violation if the children have differing numbers of children
var imbalanceFound = false;
var catChildren = getChildrenOfCat(ptree, cat);
var i = 0;
while(!imbalanceFound && i < catChildren.length-1){
var sister1 = catChildren[i];
var sister2 = catChildren[i+1];
//Make sure there is a defined children array for each sister under consideration
if(!sister1.children){
sister1.children = [];
}
if(!sister2.children){
sister2.children = [];
}
//Assign a violation if the sisters do not have the same number of children
if(sister1.children.length != sister2.children.length){
imbalanceFound = true;
}
i++;
}
if(imbalanceFound){
vcount++;
}
// Recurse for every subtree
for(var j = 0; j<ptree.children.length; j++){
vcount += balSisChildCat(stree, ptree.children[j], cat);
}
return vcount;
}
}/* Assign a violation for every node of category cat
such that its rightmost child of category (cat-1)
has more than two children.
*/
function binMaxRightmostBranches(s, ptree, cat) {
var vcount = 0;
//base case: we are at leaf && there are no children
//make sure there is children
if (ptree.children && ptree.children.length) {
if (ptree.cat === cat) {
//check rightmost child
var rightMost = ptree.children.length - 1;
var rightMostChild = ptree.children[rightMost];
if (rightMostChild.children && rightMostChild.children.length > 2) {
vcount++;
}
}
//check other nodes in ptree
for(var i = 0; i < ptree.children.length; i++) {
vcount += binMaxRightmostBranches(s, ptree.children[i], cat);
}
}
return vcount;
};
/* Assign a violation for every rightmost node x of category cat such that x dominates (at any level) more than two children of category cat such that x dominates (at any level) more than two children of category cat-1 */
function binMaxRightmostLeaves(s, ptree, cat) {
//make parent_ptree static variable to keep track of the parent ptree
if(typeof parent_ptree == 'undefined') {
parent_ptree = null;
}
var vcount = 0;
//if curr ptree has children
if(ptree.children && ptree.children.length) {
//and is the same cat as input cat
if(ptree.cat === cat) {
//if there is a parent and the current ptree is the rightmost child of that parent
//or if there is not parent but the cat is still the same as the input cat
if((parent_ptree && ptree === parent_ptree.children[parent_ptree.children.length - 1]) || parent_ptree === null) {
//count the leaves
var leaves = findLeaves(ptree);
//if the leaves exceed 2, increment vcount
if(leaves > 2) {
vcount++;
}
}
}
//code to recursively look through tree
for(var i = 0; i < ptree.children.length; i++) {
//set parent_ptree to the current ptree
parent_ptree = ptree;
//recursively call on children of ptree
vcount += binMaxRightmostLeaves(s, ptree.children[i], cat);
}
}
//remove everything in parent_ptree aka reset var to typeof undefined
delete parent_ptree;
return vcount;
};
/*helper function I created to count the leaves of a ptree*/
function findLeaves(ptree) {
var leaves = 0;
//if this ptree does not dominate another ptree with the same cat
if(isMinimal(ptree) && ptree.children) {
//add the number of leaves of the current ptree to the current amount of leaves
leaves = leaves + ptree.children.length;
}
//if there are children
if(ptree.children && ptree.children.length) {
//for every children
for(var i =0; i < ptree.children.length; i++){
//if they are the same cat as the current ptree
//count the leaves
leaves+=findLeaves(ptree.children[i]);
}
}
return leaves;
}
/* Assign a violation for every node of category cat
such that its rightmost child of category (cat-1)
has less than two children.
*/
function binMinRightmostBranches(s, ptree, cat) {
var vcount = 0;
//base case: we are at leaf && there are no children
//make sure there is children
if (ptree.children && ptree.children.length) {
if (ptree.cat === cat) {
//check rightmost child
var rightMost = ptree.children.length - 1;
var rightMostChild = ptree.children[rightMost];
if (!rightMostChild.children) {
rightMostChild.children = [];
}
if (rightMostChild.children.length < 2) {
vcount++;
}
}
//check other nodes in ptree
for(var i = 0; i < ptree.children.length; i++) {
vcount += binMaxRightmostBranches(s, ptree.children[i], cat);
}
}
return vcount;
};
/* Binarity that cares about the number of branches */
//sensitive to the category of the parent only (2 branches of any type is acceptable)
function binMinBranches(s, ptree, cat){
var vcount = 0;
if(ptree.children && ptree.children.length){
if(ptree.cat === cat && ptree.children.length===1){
//logreport("VIOLATION: "+ptree.id+" has only one child");
vcount++;
}
for(var i = 0; i<ptree.children.length; i++){
vcount += binMinBranches(s, ptree.children[i], cat);
}
}
return vcount;
}
//This function stops counting the violations once it finds the first one
function binMinBranchesInit(s, ptree, cat){
var vcount = 0;
if(ptree.children && ptree.children.length){
if(ptree.cat === cat && ptree.children.length===1){
//logreport("VIOLATION: "+ptree.id+" has only one child");
vcount++;
}
for(var i = 0; i<ptree.children.length; i++){
//these are some debugging print codes
/*console.log("ptree.children.length: "+ ptree.children.length);
console.log("i: "+ i);
console.log(ptree.cat);
console.log('vcount: '+vcount);
console.log('word: '+ptree.id);
*/
if(i === 1){
break;
}
vcount += binMinBranchesInit(s, ptree.children[i], cat);
}
}
return vcount;
}
//sensitive to the category of the parent only (2 branches of any type is acceptable)
//categorical evaluation: 1 violation for every super-binary branching node
function binMaxBranches(s, ptree, cat, n){
n = typeof(n)==='number'? n : 2;
var vcount = 0;
if(ptree.children && ptree.children.length){
if(ptree.cat === cat && ptree.children.length>n){
//logreport("VIOLATION: "+ptree.id+" has "+ptree.children.length+" children!");
vcount++;
}
for(var i = 0; i<ptree.children.length; i++){
vcount += binMaxBranches(s, ptree.children[i], cat, n);
}
}
return vcount;
}
//A combined binarity constraint (branch-counting)
function binBranches(stree, ptree, cat, n){
n = typeof(n)==='number'? n : 2;
var minCount = binMinBranches(stree, ptree, cat);
var maxCount = binMaxBranches(stree, ptree, cat, n);
return minCount+maxCount;
}
/* Category-sensitive branch-counting constraint
* (first proposed by Kalivoda 2019 in "New Analysis of Irish Syntax-Prosody", ms.)
* Assign a violation for every node of category cat that immediately dominates
* more than 2 children of category cat-1
*/
function binMaxBrCatSensitive(s, ptree, cat){
var vcount = 0;
var childcat = pCat.nextLower(cat);
if(ptree.children && ptree.children.length){
var categorySensitiveBranchCount = 0;
if(ptree.cat === cat && ptree.children.length>2){
//logreport("VIOLATION: "+ptree.id+" has "+ptree.children.length+" children!");
for(var j=0; j < ptree.children.length; j++){
if(ptree.children[j].cat===childcat){
categorySensitiveBranchCount++;
}
}
if(categorySensitiveBranchCount>2){
vcount++;
}
}
for(var i = 0; i<ptree.children.length; i++){
vcount += binMaxBrCatSensitive(s, ptree.children[i], cat);
}
}
return vcount;
}
//sensitive to the category of the parent only (2 branches of any type is acceptable)
//gradient evaluation: assigns 1 violation for every child past the first 2 ("third-born" or later)
function binMaxBranchesGradient(s, ptree, cat, n){
n = typeof(n)==='number'? n : 2;
var vcount = 0;
if(ptree.children && ptree.children.length){
var numChildren = ptree.children.length;
if(ptree.cat === cat && numChildren>n){
var excessChildren = numChildren - n;
//logreport(excessChildren+ " VIOLATION(s): "+ptree.id+" has "+numChildren+" children!");
vcount += excessChildren;
}
for(var i = 0; i<ptree.children.length; i++){
vcount += binMaxBranchesGradient(s, ptree.children[i], cat, n);
}
}
return vcount;
}
function binBrGradient(s, ptree, cat, n){
n = typeof(n)==='number'? n : 2;
return binMaxBranchesGradient(s, ptree, cat, n)+binMinBranches(s, ptree, cat);
}
/*TRUCKENBRODT-STYLE BINARITY*/
/* Categorical BinMax (Leaves)
* Assign one violation for every node of the prosodic category c such that this
* node dominates more than two nodes of the prosodic category immidately below c
* on the prosodic hierarchy.
*
* Parent-category-neutral version of:
* Sandalo & Truckenbrodt 2002: "Max-Bin: P-phrases consist of maximally two prosodic words"
* Assigns a violation for every node in ptree that dominates more than two prosodic words.
*/
function binMaxLeaves(s, ptree, c, n){
n = typeof(n)==='number'? n : 2;
var vcount = 0;
//the category we are looking for:
var target = pCat.nextLower(c);
//pCat.nextLower defined in prosdic hierarchy.js
//console.log("the target of binMaxLeaves is " + target);
if(ptree.children && ptree.children.length){
var targetDesc = getDescendentsOfCat(ptree, target);
//console.log("there are " + targetDesc.length + " " + target + "s");
if(ptree.cat === c && targetDesc.length > n){
vcount ++;
}
for(var i = 0; i < ptree.children.length; i++){
vcount += binMaxLeaves(s, ptree.children[i], c, n);
}
}
return vcount;
}
/*
* BinMax(phi-min)
* Violated if a minimal phi contains more than 2 minimal words --> leaf-counting
*/
function binMax_minLeaves(s, ptree, c){
// c = phi
markMinMax(ptree);
var vcount = 0;
if(ptree.children && ptree.children.length){
var leafCat = pCat.nextLower(c);
var wDesc = getDescendentsOfCat(ptree, leafCat);
// console.log("there are " + wDesc.length + " " + "ws");
if(ptree.cat === c && ptree.isMin){
var count = 0;
for(var i=0; i < wDesc.length; i++) {
if(wDesc[i].isMin) {
count++;
}
}
if(count > 2) {
vcount++;
}
}
for(var i = 0; i < ptree.children.length; i++){
vcount += binMax_minLeaves(s, ptree.children[i], c);
}
}
return vcount;
}
/* Gradiant BinMax (Leaves)
* I don't know how to define this constraint in prose, but it's binMaxLeaves as
* a gradient constraint instead of a categorical constraint.
*/
function binMaxLeavesGradient(s, ptree, c, n){
n = typeof(n)==='number'? n : 2;
var vcount = 0;
//the category we are looking for:
var target = pCat.nextLower(c);
//pCat.nextLower defined in prosodicHierarchy.js
if(ptree.children && ptree.children.length){
var targetDesc = getDescendentsOfCat(ptree, target);
if(ptree.cat === c && targetDesc.length > n){
vcount += targetDesc.length - 2; //this makes the constraint gradient
}
for(var i = 0; i < ptree.children.length; i++){
vcount += binMaxLeavesGradient(s, ptree.children[i], c, n);
}
}
return vcount;
}
/* BinMin (Leaves)
* Assign one violation for every node of the prosodic category c such that this
* node dominates less than two nodes of the prosodic category immidately below c
* on the prosodic hierarchy.
*/
function binMinLeaves(s, ptree, c){
var vcount = 0;
//the category we are looking for:
var target = pCat.nextLower(c);
//pCat.nextLower defined in prosdic hierarchy.js
if(ptree.children && ptree.children.length){
var targetDesc = getDescendentsOfCat(ptree, target);
if(ptree.cat === c && targetDesc.length < 2){
vcount ++;
}
for(var i = 0; i < ptree.children.length; i++){
vcount += binMinLeaves(s, ptree.children[i], c);
}
}
return vcount;
}
//Combines the violations of maximal and minimal binarity (leaf-counting)
function binLeaves(s, ptree, c, n){
n = typeof(n)==='number'? n : 2;
return binMaxLeaves(s, ptree, c, n) + binMinLeaves(s, ptree, c);
}
function binLeavesGradient(s, ptree, c, n){
n = typeof(n)==='number'? n : 2;
return binMaxLeavesGradient(s, ptree, c, n) + binMinLeaves(s, ptree, c);
}
//Helper function: given a node x, returns all the descendents of x that have category cat.
//Since this function is designed for use on prosodic trees, it does not take silence into account.
function getDescendentsOfCat(x, cat){
var descendents = [];
//logreport("x.cat is "+x.cat+ ", cat is " +cat);
if(x.children && x.children.length)
//x is non-terminal
{
for(var y=0; y < x.children.length; y++){
if(x.children[y].cat === cat){
descendents.push(x.children[y]);
}
var yDescendents = getDescendentsOfCat(x.children[y], cat);
for(var i=0; i < yDescendents.length; i++){
descendents.push(yDescendents[i]);
}
}
}
/* this else if statement was double counting terminal nodes of category cat.
* removing it causes double counting to stop.
else if(x.cat === cat) // x is a terminal of the right category
{
descendents.push(x);
}*/
return descendents;
}
/*<legacyBinarityConstraints> (for backwards compatability with old test files)*/
//Parent-category-neutral version of:
//Sandalo & Truckenbrodt 2002: "Max-Bin: P-phrases consist of maximally two prosodic words"
//Assigns a violation for every node in ptree that dominates more than two prosodic words.
function binMax2Words(s, ptree, cat){
var vcount = 0;
if(ptree.children && ptree.children.length){
wDesc = getDescendentsOfCat(ptree, 'w');
if(ptree.cat === cat && wDesc.length>2){
//logreport("VIOLATION: "+ptree.id+" dominates "+wDesc.length+" words!");
vcount++;
}
for(var i = 0; i<ptree.children.length; i++){
vcount += binMax2Words(s, ptree.children[i], cat);
}
}
return vcount;
}
//Gradient version of Truckenbrodt's Maximum Binarity
function binMax2WordsGradient(s, ptree, cat){
var vcount = 0;
if(ptree.children && ptree.children.length){
wDesc = getDescendentsOfCat(ptree, 'w');
if(ptree.cat === cat && wDesc.length>2){
//logreport("VIOLATION: "+ptree.id+" dominates "+wDesc.length+" words!");
vcount += (wDesc.length - 2);
}
for(var i = 0; i<ptree.children.length; i++){
vcount += binMax2WordsGradient(s, ptree.children[i], cat);
}
}
return vcount;