-
Notifications
You must be signed in to change notification settings - Fork 2
/
candidategenerator.js
315 lines (266 loc) · 9.45 KB
/
candidategenerator.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
if(typeof window === "undefined"){
window = {};
}
(function() {
window.GEN_impl = function(sTree, leaves, options) {
var recursiveOptions = {};
for (var k in options) {
if (options.hasOwnProperty(k) && k !== 'requireRecWrapper')
recursiveOptions[k] = options[k];
}
/* if rootCategory and recursiveCategory are the same, we don't want to call
* addRecCatWrapped because half of the candidates will have a root node with
* only one child, which will be of the same category, ie. {i {i (...) (...)}}
*/
var rootlessCand = gen(leaves, recursiveOptions)
if(options.rootCategory !== options.recursiveCategory){
rootlessCand = addRecCatWrapped(gen(leaves, recursiveOptions), options);
}
var candidates = [];
for(var i=0; i<rootlessCand.length; i++){
var pRoot = wrapInRootCat(rootlessCand[i], options);
if (!pRoot)
continue;
if (options.obeysHeadedness && !obeysHeadedness(pRoot))
continue;
candidates.push([sTree, pRoot]);
}
return candidates;
}
/* Function to check if a tree obeys headedness. Each node must either be be
* terminal or have at least one child of the category immidately below its own
* on the prosodic hierarch. Otherwise, return false. Written as a recursive
* function, basically a constraint.
*/
function obeysHeadedness(tree){
//inner function
function nodeIsHeaded(node) {
/* Function to check if a node is headed. Relies on the prosodic hierarchy being
* properly defined. Returns true iff
* a. one of the node's children is of the category directly below its own category * on the prosodic hierarchy,
* b. one of the node's descendants is of the same category as the node
* c. the node is terminal.
*/
var children = node.children;
//vacuously true if node is terminal
if (!children)
return true;
for (var i = 0; i < children.length; i++)
if (children[i].cat === pCat.nextLower(node.cat)
|| children[i].cat === node.cat){
return true;
}
return false;
}
//outer function
//first, check the parent node
if (!nodeIsHeaded(tree))
return false;
//return false if one of the children does not obey headedness
if (tree.children){
for (var x = 0; x<tree.children.length; x++){
if (!obeysHeadedness(tree.children[x])) //recursive function call
return false;
}
}
//if we get this far, the tree obeys headedness
return true;
}
function obeysExhaustivity(cat, children) {
for (var i = 0; i < children.length; i++)
if (cat !== children[i].cat && pCat.nextLower(cat) !== children[i].cat){
return false;
}
return true;
}
function wrapInRootCat(candidate, options){
if (options && options.obeysExhaustivity){ // check that options.obeysExhaustivity is defined
if(typeof options.obeysExhaustivity ==="boolean" && options.obeysExhaustivity && !obeysExhaustivity(options.rootCategory, candidate)){
return null;
}
else if (options.obeysExhaustivity instanceof Array && options.obeysExhaustivity.indexOf(options.rootCategory)>=0 && !obeysExhaustivity(options.rootCategory, candidate)){
return null;
}
}
if(candidate.length < 2 && options.rootCategory === candidate[0].cat){
return null;
}
//if we get here, there aren't any relevant exhaustivity violations
return {id: 'root', cat: options.rootCategory, children: candidate};
}
/*Conceptually, returns all possible parenthesizations of leaves that don't
* have a set of parentheses enclosing all of the leaves
* Format: returns an array of parenthesizations, where each parenthesization
* is an array of children, where each child is
* either a node of category recursiveCategory (with descendant nodes attached)
* or a leaf (of category terminalCategory)
* Options:
*/
function gen(leaves, options){
var candidates = []; //each candidate will be an array of siblings
var cand;
if(!(leaves instanceof Array))
throw new Error(leaves+" is not a list of leaves.");
//Base case: 0 leaves
if(leaves.length === 0){
candidates.push([]);
return candidates;
}
//Recursive case: at least 1 terminal. Consider all candidates where the first i words are grouped together
for(var i = 1; i <= leaves.length; i++){
//First, create the right sides:
var rightLeaves = leaves.slice(i, leaves.length);
//recursion at top level
//var test_output = gen(rightLeaves,options);
var rightsides = addRecCatWrapped(gen(rightLeaves, options), options);
/*if(options.noUnary){
//console.log("gen:",test_output);
//console.log("rightsides:",rightsides);
};*/
//recursion at lower levels
if(pushRecCat(options)){
var wRightsides = addRecCatWrapped(gen(rightLeaves, options), options);
rightsides.concat(wRightsides);
popRecCat(options);
}
//Then create left sides and combine them with the right sides.
//Case 1: the first i leaves attach directly to parent (no wrapping in a recursive category)
var leftside = leaves.slice(0,i);
// We don't need to check the left side for nonrecursivity, because it's all leaves
//Combine the all-leaf leftside with all the possible rightsides that have a phi at their left edge (or are empty)
for(var j = 0; j<rightsides.length; j++){
var currRightside = rightsides[j];
var firstRight = currRightside[0];
if(!currRightside.length || (firstRight.children && firstRight.children.length) || (firstRight.cat != options.terminalCategory && !isLower(pCat, firstRight.cat, options.terminalCategory)))
{
cand = leftside.concat(currRightside);
candidates.push(cand);
}
}
if(i<leaves.length){
if(options.noUnary && i<2){
continue;
//Don't generate any candidates where the first terminal is in an intermediate level node by itself.
}
//Case 2: the first i words are wrapped in an intermediate level node
//Case 2a: first recursive category
var phiLeftsides = gen(leaves.slice(0,i), options);
for(var k = 0; k<phiLeftsides.length; k++)
{
var phiNode = wrapInRecCat(phiLeftsides[k], options);
if (!phiNode){
continue;
}
var leftside = [phiNode];
for(var j = 0; j<rightsides.length; j++)
{
cand = leftside.concat(rightsides[j]);
candidates.push(cand);
}
}
//Case 3
//Try to build left-sides that are wrapped in the next lower recursive category but aren't wrapped in the current recursive category
if(pushRecCat(options)){
var wLeftsides = gen(leaves.slice(0,i), options);
for(var k = 0; k<wLeftsides.length; k++){
var wLeftside = wrapInRecCat(wLeftsides[k], options);
if(wLeftside){
//console.log(i, "wLeftside:", wLeftside);
//Combine the all-leaf leftside with all the possible rightsides that aren't empty
for(var j = 0; j<rightsides.length; j++){
if(rightsides[j].length)
{
cand = [wLeftside].concat(rightsides[j]);
candidates.push(cand);
}
}
}
}
popRecCat(options);
}
}
}
//Now try to use recursion at the next recursive category
if (pushRecCat(options)) {
var wCands = gen(leaves, options);
//Add things that are entirely wrapped in [ ]
for (var i = 0; i < wCands.length; i++) {
cand = wCands[i];
var wrappedCand = wrapInRecCat(cand, options);
if(wrappedCand)
candidates.push([wrappedCand]);
}
popRecCat(options);
}
return candidates;
}
function wrapInRecCat(candidate, options){
// Check for Exhaustivity violations below the phi, if phi is listed as one of the exhaustivity levels to check
if (options && options.obeysExhaustivity){
if ((typeof options.obeysExhaustivity === "boolean" || options.obeysExhaustivity.indexOf(options.recursiveCategory)>=0) && !obeysExhaustivity(options.recursiveCategory, candidate))
return null;
}
if (options && options.obeysNonrecursivity){
for (var i = 0; i < candidate.length; i++)
if (candidate[i].cat === options.recursiveCategory){
return null;
}
}
if (options && options.noUnary && candidate.length === 1){
return null;
}
// Don't wrap anything in a recursive category that is already wrapped in one
if (candidate.length === 1 && (candidate[0] && candidate[0].cat === options.recursiveCategory)){
if(candidate[0].cat==='phi')
console.log("wrapInRecCat", options.recursiveCategory, candidate);
//console.log("Not wrapping ", candidate);
return null;
}
return {id: options.recursiveCategory+(options.counters.recNum++), cat: options.recursiveCategory, children: candidate};
}
//Takes a list of candidates and doubles it to root each of them in a phi
//If options.noUnary, skip wrapInRecCat-ing candidates that are only 1 terminal long
function addRecCatWrapped(candidates, options){
var origLen = candidates.length;
var result = [];
if (!options.requireRecWrapper) {
result = candidates;
}
for(var i=0; i<origLen; i++){
var candLen = candidates[i].length;
if(candLen) {
var phiNode = wrapInRecCat(candidates[i], options);
if (phiNode){
result.push([phiNode]);
}
}
}
return result;
}
//Move to the next recursive category, if there is one.
function pushRecCat(options){
var nextIndex = options.recursiveCatIndex + 1;
if(nextIndex > options.recursiveCats.length-1){
return false;
}
else{
var nextRecCat = options.recursiveCats[nextIndex];
options.recursiveCategory = nextRecCat;
options.recursiveCatIndex = nextIndex;
return true;
}
}
//Move to the previous recursive category, if there is one.
function popRecCat(options){
var prevIndex = options.recursiveCatIndex - 1;
if(prevIndex < 0){
return false;
}
else{
var prevRecCat = options.recursiveCats[prevIndex];
options.recursiveCategory = prevRecCat;
options.recursiveCatIndex = prevIndex;
return true;
}
}
})();