-
Notifications
You must be signed in to change notification settings - Fork 2
/
candidategenerator_wrapper.js
307 lines (276 loc) · 13.2 KB
/
candidategenerator_wrapper.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
/* Takes a list of words and returns the candidate set of trees (JS objects)
Options is an object consisting of the parameters of GEN. Its properties can be:
- obeysExhaustivity (boolean or array of categories at which to require conformity to exhaustivity)
- obeysHeadedness (boolean)
- obeysNonrecursivity (boolean)
- rootCategory (string)
- recursiveCategory (string) --> '-' separated list of categories, from highest to lowest (e.g. 'phi-w', not 'w-phi')
-> saved in recursiveCats (see below) + becomes a string rep of the current recursive category
- terminalCategory (string)
- recursiveCatIndex (int): tracks which recursive category we're currently using
- recursiveCats (list of strings): list of recursive categories to use
- addTones (string). Possible values include:
- "addJapaneseTones"
- "addIrishTones_Elfner"
- "addIrishTones_Kalivoda"
- noUnary (boolean): if true, don't create any nodes that immediately dominate only a single node.
- maxBranching (numeric): maximum number of children that any node in the tree can have
- requireRecWrapper (boolean). Formerly "requirePhiStem"
- syntactic (boolean): are we generating syntactic trees?
- ph (prosodic heirarchy object):
pCat: custom pCat used in GEN
categoryPairings: custom category pairings passed to makeTableau passed to constraints
*/
window.GEN = function(sTree, words, options){
options = options || {obeysHeadedness: true}; // if options is undefined, set it to an empty object (so you can query its properties without crashing things)
//Set obeysHeadedness:true by default
//Set prosodic hierarchy if we're making prosodic trees. Don't bother with this for syntactic trees.
if(!options.syntactic){
// Create the ph object if none was passed or what was passed was incomplete, and set it the default PH object, defined in prosodicHierarchy.js
if (!(options.ph && options.ph.pCat && options.ph.categoryPairings)){
options.ph = PH_PHI;
//console.log("The prosodic hierarchy input to GEN was missing or incomplete, so ph has been set by default to PH_PHI, defined in prosodicHierarchy.js");
}
setPCat(options.ph.pCat);
setCategoryPairings(options.ph.categoryPairings);
// give a warning if there are categories from categoryPairings not present in pCat
if (!checkProsodicHierarchy(options.ph.pCat, options.ph.categoryPairings)){
displayWarning("One or more categories in the provided map of syntactic-prosodic correspondences (categoryPairings) do not exist in the provided prosodic hierarchy (pCat). Resetting pCat and categoryPairings to their default values, defined in PH_PHI.");
//set pCat and categoryPairings to their default values
resetPCat();
resetCategoryPairings();
options.ph = PH_PHI;
}
}
//Set the relevant category hierarchy (syntactic or prosodic) based on the GEN option syntactic
var categoryHierarchy = options.syntactic ? sCat : pCat;
var defaultRecCat = options.syntactic ? "xp" : "phi"; //sets the default of recursiveCategory option to "phi" if prosodic, "xp" if syntactic
options.recursiveCategory = options.recursiveCategory || defaultRecCat;
// Check for multiple recursive categories
if(options.recursiveCategory && options.recursiveCategory.length){
if(typeof options.recursiveCategory === "string"){
var recCats = options.recursiveCategory.split('-');
}
else {
var recCats = [];
for(var i = 0; i<options.recursiveCategory.length; i++){
recCats = recCats.concat(options.recursiveCategory[i]);
}
}
if(recCats.length > 1){
//console.log(recCats);
options.recursiveCatIndex = 0;
//Set current recursiveCategory
options.recursiveCategory = recCats[options.recursiveCatIndex];
//Save list of all categories
options.recursiveCats = recCats;
}
if(recCats.length > 2){
this.alert("You have entered more than 2 recursive categories!")
}
}
if(!options.recursiveCats){
options.recursiveCats = [options.recursiveCategory];
}
//Point to first recursiveCat
options.recursiveCatIndex = 0;
/* First, warn the user if they have specified terminalCategory and/or
* rootCategory without specifying recursiveCategory
*/
if(!options.recursiveCategory && (options.rootCategory || options.terminalCategory)){
if(!window.confirm("You have not specified the recursive category for GEN, it will default to "+ defaultRecCat +".\nClick OK if you wish to continue."))
throw new Error("GEN was canceled by user.");
}
/* the prosodic hierarchy should include the categories specified in
* options.rootCategory, options.recursiveCategory and options.terminalCategory
* But if they are not, the default setting code throws unhelpful errors.
* The finally block throws more helpful errors and alert boxes instead
*/
//a flag for whether the user has included a novel category undefined in categoryHierarchy
var novelCategories = false;
try{
options.recursiveCategory = options.recursiveCategory || defaultRecCat;
//sets the default of rootCategory based on recursiveCategory
options.rootCategory = options.rootCategory || categoryHierarchy.nextHigher(options.recursiveCategory);
//sets the default of terminalCategory based on recursiveCategory
options.terminalCategory = options.terminalCategory|| categoryHierarchy.nextLower(options.recursiveCategory);
}
finally{
var novelCatWarning = " is not a valid category with the current settings.\nCurrently valid prosodic categories: " + JSON.stringify(pCat) + "\nValid syntactic categories: " + JSON.stringify(sCat);
//private function to avoid code duplication in warning about novel recursive cats
function novelRecursiveCatEval(recCat){
if(categoryHierarchy.indexOf(recCat)<0){
var err = new Error("Specified recursive category "+recCat+novelCatWarning);
displayError(err.message, err);
novelCategories = true;
throw err;
}
}
if(options.rootCategory && categoryHierarchy.indexOf(options.rootCategory)<0){
var err = new Error("Specified root category "+options.recursiveCategory+novelCatWarning);
displayError(err.message, err);
novelCategories = true;
throw err;
}
//Throw an error for any specified recursive category(s) that are valid.
//if...else structure because there could be more than 1 recursive cat:
//Multiple recursive cats: options.recursiveCats is only defined if options.recursiveCategory contained a hyphen and has been split (line 62 above).
if(options.recursiveCats && options.recursiveCats.length){
for(let i in options.recursiveCats){
novelRecursiveCatEval(options.recursiveCats[i]);
}
}
//Only one recursive cat
else{
novelRecursiveCatEval(options.recursiveCategory);
}
// Throws an error for the defined terminal category if it is not a valid category.
// Don't check terminal category if we're building syntactic trees.
if(!options.syntactic && options.terminalCategory && categoryHierarchy.indexOf(options.terminalCategory)<0){
var err = new Error("Specified terminal category "+options.recursiveCategory+novelCatWarning);
displayError(err.message, err);
novelCategories = true;
throw err;
}
}
//Warnings for adverse GEN options combinations:
if(options.rootCategory === options.recursiveCategory && options.obeysNonrecursivity){
displayWarning("You have instructed GEN to produce non-recursive trees and to produce trees where the root node and intermediate nodes are of the same category. Some of the trees GEN produces will be recursive.");
}
if(options.rootCategory === options.terminalCategory && options.obeysNonrecursivity){
displayWarning("You have instructed GEN to produce non-recursive trees and to produce trees where the root node and terminal nodes are of the same category. All of the trees GEN produces will be recursive.");
}
if(options.recursiveCategory === options.terminalCategory && options.obeysNonrecursivity){
displayWarning("You have instructed GEN to produce non-recursive trees and to produce trees where the intermediate nodes and the terminal nodes are of the same category. You will only get one bracketing.");
}
//Perform additional checks of layering if novel categories are not involved.
if(!novelCategories){
if(categoryHierarchy.isHigher(options.recursiveCategory, options.rootCategory) || categoryHierarchy.isHigher(options.terminalCategory, options.recursiveCategory)){
displayWarning("You have instructed GEN to produce trees that do not obey layering. See pCat and sCat in prosodicHierarchy.js");
}
else{
//Check that the highest recursive category is immediately below the selected root category.
if(options.recursiveCategory !== categoryHierarchy.nextLower(options.rootCategory) && options.recursiveCategory !== options.rootCategory)
{
displayWarning(""+options.recursiveCategory+" is not directly below "+options.rootCategory+" in the prosodic hierarchy. None of the resulting trees will be exhaustive because GEN will not generate any "+categoryHierarchy.nextLower(options.rootCategory)+"s. See pCat and sCat in prosodicHierarchy.js");
}
//Check that the lowest recursive category is immediately above the chosen terminal category.
if(!options.recursiveCats){
options.recursiveCats = [options.recursiveCategory];
}
var lowestRecCat = options.recursiveCats[options.recursiveCats.length-1];
if(options.terminalCategory !== categoryHierarchy.nextLower(lowestRecCat) && options.terminalCategory !== lowestRecCat){
displayWarning(""+options.terminalCategory+" is not directly below "+lowestRecCat+" in the prosodic hierarchy. None of the resulting trees will be exhaustive because GEN will not generate any "+categoryHierarchy.nextLower(lowestRecCat)+"s. Current pCat: "+pCat);
}
}
}
if(typeof words === "string") { // words can be a space-separated string of words or an array of words; if string, split up into an array
if (!words) { // if empty, scrape words from sTree
if(sTree.cat && sTree.id){
words = getLeaves(sTree);
}
else{
let message = "window.GEN() was called no valid input!";
displayError(message);
return [];
}
} else {
words = words.split(' ');
words = deduplicateTerminals(words);
}
} else {
words = deduplicateTerminals(words);
}
var leaves = [];
options.counters = {
recNum: 0
}
for(var i=0; i<words.length; i++){
leaves.push(wrapInLeafCat(words[i], options.terminalCategory, options.syntactic));
}
return window.GEN_impl(sTree, leaves, options);
}
function deduplicateTerminals(terminalList) {
//Check for duplicate words
var occurrences = {};
var dedupedTerminals = [];
for(var i=0; i<terminalList.length; i++){
var t = terminalList[i];
//If this is the first occurrence of t, don't append an index
if(!occurrences.hasOwnProperty(t)){
dedupedTerminals.push(t);
occurrences[t] = 1;
}
// If we've seen t before, then add an index to it such that the 2nd occurrence of t
// becomes t_1.
else{
dedupedTerminals.push(t+'_'+occurrences[t]);
occurrences[t] = occurrences[t] + 1;
}
}
return dedupedTerminals;
}
/** Function to take a string and category and return an object wordObj with attributes
* wordObj.id = word
* wordObj.cat = cat
*
* Also convert hyphenated information about accent and status as a clitic that is
* appended to the word argument to attributes of wordObj.
*
* If input word is already an object, return it after checking its category.
* - if word.cat == cat, return as is
* - if word.cat == "clitic" and syntactic==true, create an x0 layer over the clitic
* - if word.cat == "clitic" and syntactic==false, change word.cat to "syll"
* - if word.cat != cat, and word.cat != clitic, change word.cat to cat.
*/
function wrapInLeafCat(word, cat, syntactic){
var wordObj;
//If word is already an object with appropriate properties, then check categories and return.
if(typeof word === "object"){
if(word.cat && word.id){
wordObj = JSON.parse(JSON.stringify(word)); //deep copy shortcut
//convert "clitic" to "syll" if we're making a prosodic tree
if(wordObj.cat==="clitic"){
if(!syntactic){
wordObj.cat = "syll";
}
else{ //if it's a clitic and we're making syntactic trees, then give it an x0 layer
var cliticObj = wordObj;
wordObj = addParent(cliticObj);
}
}
//otherwise change cat to the specified cat if they don't match
else if (wordObj.cat !== cat){
wordObj.cat = cat;
}
return wordObj;
}
else displayWarning("wrapInLeafCat: argument word is already an object but lacks an id or cat.");
}
//Otherwise, word is a string and must be converted into an object.
else{
var myCat = cat || 'w'; //by default, the leaf category is 'w'
var wordId = word;
//check if the input specifies this is a clitic and set category appropriately
var isClitic = word.indexOf('-clitic')>=0;
if (isClitic){
myCat = syntactic ? 'clitic' : 'syll'; //syntactic tree vs prosodic trees
wordId = wordId.split('-clitic')[0];
}
wordObj = {cat: myCat};
//check if the input specifies this is an accented word, and set accent to true if so
if(word.indexOf('-accent') >= 0){
wordObj.accent = true;
wordId = wordId.split('-accent')[0];
}
wordObj.id = wordId;
//add an x0 layer if this is a (syntactic) clitic
if(myCat==="clitic"){
wordObj = addParent(wordObj);
}
return wordObj;
}
}
function addParent(child, parentCat="x0", parentId="clitic_x0"){
return {cat:parentCat, id:parentId, children:[child]};
}