-
Notifications
You must be signed in to change notification settings - Fork 2
/
treeFormatting.js
148 lines (140 loc) · 4.14 KB
/
treeFormatting.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
/*defines brackets used in tableau for various categories*/
var categoryBrackets = {
"i": "{}",
"cp": "{}",
"xp": "[]",
"phi": "()",
"x0": ["[x0 ","]"],
"w": ["[", "]"],
"clitic": ["",""],
"syll": ["",""],
"Ft": ["(F ", ")"],
"u": ["{u ", "}"]
};
var subWordBrackets = {
"i": "{}",
"cp": "{}",
"xp": "[]",
"phi": "()",
"x0": ["[x0 ","]"],
"clitic": ["",""],
"u": ["{u ", "}"],
"w": ["[","]"],
"Ft": ["(",")"],
"syll": ["",""]
};
/* Function that takes a [default=prosodic] tree and returns a string version where phi boundaries are marked with '(' ')'
Possible options:
- invisibleCategories: by default, every category in categoryBrackets gets a bracket
- parens: default mappings in categoryBrackets can be overwritten with a map
- showNewCats: if true, annotate categories that aren't found in categoryBrackets with [cat ], where cat is the new category
- showTones: set to addJapaneseTones, addIrishTones_Elfner, etc. to annotate the tree with appropriate tones and show them in its parenthesization
- showHeads: if true, mark heads with an astrisk
*/
function parenthesizeTree(tree, options){
var parTree = [];
var toneTree = [];
options = options || {};
var showNewCats = options.showNewCats || true;
var invisCats = options.invisibleCategories || [];
var showTones = options.showTones || false;
var parens = options.parens || Object.assign({}, (options.subword)? subWordBrackets : categoryBrackets);
if(options.showTones){
tree = window[options.showTones](tree);
}
function processNode(node){
var nonTerminal = (node.children instanceof Array) && node.children.length;
if (showNewCats && !parens.hasOwnProperty(node.cat)){
parens[node.cat] = ["["+node.cat+" ", "]"];
}
var visible = invisCats.indexOf(node.cat) === -1 && parens.hasOwnProperty(node.cat);
if (nonTerminal) {
if (visible) {
var tempLabel = parens[node.cat][0];
tempLabel = addAttributeLabels(node, tempLabel)
if (node["func"] || node["silentHead"] || node["foc"]){
tempLabel += " ";
}
parTree.push(tempLabel);
//parTree.push(parens[0]);
if(showTones){
toneTree.push(parens[node.cat][0]);
if(node.tones){
toneTree.push(node.tones);
toneTree.push(' ');
var toneStringLength = node.tones.length+1;
parTree.push(' '.repeat(toneStringLength));
}
}
}
for(var i=0; i<node.children.length; i++){
processNode(node.children[i]);
if(i<node.children.length-1){
parTree.push(' ');
if(showTones){
toneTree.push(' ');
}
}
}
if (visible){
parTree.push(parens[node.cat][1]);
if(node.head && options.showHeads){
parTree.push('*'); //marks head with a *
}
//parTree.push(parens[1]);
if(showTones){
toneTree.push(parens[node.cat][1]);
//console.log(parens[node.cat]);
//console.log(toneTree[toneTree.length-1]);
}
}
}
//terminal but visible
else if (visible) {
var tempLabel = node.id;
parTree.push(addAttributeLabels(node, tempLabel));
//parTree.push(node.id);
if(node.cat!='w' && node.cat!='x0'){
parTree.push('.'+node.cat);
}
if(node.head && options.showHeads){
parTree.push("*");
}
if(showTones && node.tones){
toneTree.push(node.tones);
var toneIdDiff = node.tones.length - node.id.length;
if(toneIdDiff > 0)
parTree.push(' '.repeat(toneIdDiff));
if(toneIdDiff < 0)
toneTree.push(' '.repeat(-toneIdDiff));
}
if(showTones && !node.tones){
toneTree.push(' '.repeat(node.id.length))
}
}
// parTree.push(node.id.split('_')[0]);
}
processNode(tree);
guiTree = parTree.join('');
if(showTones)
guiTree = guiTree + '\n' + toneTree.join('');
return guiTree;
}
function addAttributeLabels(node, tempLabel){
if (node ["accent"]){
//add .a if the node has an accent attribute with a value that isn't 'u' or 'U', and the node's id isn't already a or A.
var idPref = node.id.split('_')[0];
var accentLabel = (node.accent && idPref !== 'A' && idPref !== 'a')? '.a': '';
tempLabel += accentLabel;
}
if (node["func"]){
tempLabel += ".f";
}
if (node["silentHead"]){
tempLabel += ".sh";
}
if (node["foc"]){
tempLabel += ".foc";
}
return tempLabel;
}