| 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 | 1
1
1
1
1
1
1
1
1
1
1
1
1
1
6530
6530
6530
6530
6530
6530
6530
6530
6530
1
50599
75806
75806
75806
75806
69272
75806
75806
24781
24781
75806
69272
7960
5864
5864
2096
2096
2096
6900
6900
1075
5825
9002
6486
6486
9002
745
745
39231
1
39230
74
4
4
6530
6530
6530
6530
6530
522
1
1
834
834
834
834
834
287
287
287
287
834
8
8
8
8
834
547
287
2
285
285
287
1
| var CORS = require("../CORS");
var Q = require("q");
var BASE_LEXICON_NODE = require("./../datum/LanguageDatum").LanguageDatum;
var DEFAULT_CORPUS = require("./../../api/corpus/corpus.json");
var AudioVideos = require("./../audio_video/AudioVideos").AudioVideos;
var Contexts = require("./../lexicon/Contexts").Contexts;
var Comments = require("./../comment/Comments").Comments;
var DatumFields = require("./../datum/DatumFields").DatumFields;
var DatumStates = require("./../datum/DatumStates").DatumStates;
var DatumTags = require("./../datum/DatumTags").DatumTags;
var Images = require("./../image/Images").Images;
var Session = require("./../datum/Session").Session;
var escapeRegexCharacters = function(regex) {
return regex.replace(/([()[{*+.$^\\|?])/g, "\\$1");
};
/**
* @class Lexicon Node is key value pair with an index of related datum. It allows the search to index
* the corpus to find datum, it is also used by the default glosser to guess glosses based on what the user inputs on line 1 (utterance/orthography).
*
* @description Lexicon Node is key value pair with an index of related datum. It allows the search to index
* the corpus to find datum, it is also used by the default glosser to guess glosses based on what the user inputs on line 1 (utterance/orthography).
*
* @name LexiconNode
* @extends LanguageDatum
* @constructs
*/
var LexiconNode = function(options) {
Eif (!this._fieldDBtype) {
this._fieldDBtype = "LexiconNode";
}
// this.debugMode = true;
this.debug("Declaring a LexiconNode", options);
BASE_LEXICON_NODE.apply(this, arguments);
Eif (typeof this.toJSON === "function") {
this.fossil = this.toJSON();
} else {
this.fossil = {};
this.expandedIGTFields.map(function(field) {
if (field && this[field]) {
this.fossil[field] = this[field];
}
});
}
this.debug("constructed " + this.id);
this.debug("morphemes " + this.morphemes);
this.debug("contexts ", this.contexts);
};
LexiconNode.prototype = Object.create(BASE_LEXICON_NODE.prototype, /** @lends LexiconNode.prototype */ {
constructor: {
value: LexiconNode
},
api: {
value: "lexicalNodes"
},
// Internal models: used by the parse function
INTERNAL_MODELS: {
value: {
fields: DatumFields,
audioVideo: AudioVideos,
session: Session,
comments: Comments,
contexts: Contexts,
datumStates: DatumStates,
datumTags: DatumTags,
images: Images
}
},
/**
* Lexical entries/Nodes unique id is composed of their morphemes and gloss.
* However, the user can edit the id/headword if there really are more
* than one morpheme with the exact same morphemes and gloss combo.
* @return {String} [description]
*/
id: {
configurable: true,
get: function() {
return this.headword;
},
set: function(value) {
if (this.headword !== value) {
this.headword = value;
}
}
},
headword: {
configurable: true,
get: function() {
this.debug(" getting headword ", this.fields ? this.fields.length : this);
// Let users customize the headword
Iif (this._headword) {
return this._headword;
}
var constructHeadwordFromMorphemesAndGloss = "";
if (this.morphemes) {
constructHeadwordFromMorphemesAndGloss = this.morphemes;
}
constructHeadwordFromMorphemesAndGloss += LexiconNode.HEADWORD_DELIMITER;
// without this morphemes glossed as ? will be counted as another morpheme, if that morpheme has a gloss
if (!this.gloss || this.gloss === "?" || this.gloss === "??") {
// constructHeadwordFromMorphemesAndGloss += "?";
} else Eif (this.gloss) {
constructHeadwordFromMorphemesAndGloss += this.gloss;
}
if (constructHeadwordFromMorphemesAndGloss && constructHeadwordFromMorphemesAndGloss !== LexiconNode.HEADWORD_DELIMITER) {
return constructHeadwordFromMorphemesAndGloss;
}
},
set: function(value) {
this._headword = this._id = value;
}
},
count: {
configurable: true,
get: function() {
if (!this._contexts || !(this._contexts instanceof Contexts) || !this._contexts.length) {
this.debug(" getting _count of " + this.id + " " + this._count);
return this._count || 0;
}
Eif (this._contexts && this._contexts instanceof Contexts) {
this.debug(" getting number of context counts of " + this.id + " " + this._contexts.length);
return this._contexts.length;
}
return 0;
},
set: function(value) {
this.debug(" setting count of " + this.id + " " + value);
if (this._contexts && this._contexts instanceof Contexts && this._contexts.length) {
this.debug("Ignoring count " + value + ", this lexical node has contexts we can use instead." + this._contexts.length);
// this._contexts.length = value;
} else {
this._count = value;
}
}
},
categories: {
configurable: true,
get: function() {
return this.syntacticCategory.split(/\s+/);
},
set: function(value) {
this.syntacticCategory = value.join(" ");
}
},
datumids: {
configurable: true,
get: function() {
return this._datumids || [];
},
set: function(value) {
this._datumids = value;
}
},
contexts: {
configurable: true,
get: function() {
if (!this._contexts) {
this._contexts = new this.INTERNAL_MODELS["contexts"]();
this.debug("Initializing contexts of " + this.id);
}
return this._contexts;
},
set: function(value) {
// this.debugMode = true;
this.ensureSetViaAppropriateType("contexts", value);
// this.debugMode = false;
this.debug(" set contexts to ", this._contexts);
// console.log(" setting context ", value);
// if (!this._contexts || !this._contexts.length) {
// this._contexts = value;
// } else {
// var contextIsNew = true;
// for (var newContextIndex = 0; newContextIndex < value.length; newContextIndex++) {
// var newContext = value[newContextIndex];
// this._contexts.map(function(existingContext) {
// // If any of the properties in an existing context match, we dont need to add this as a context
// for (var attrib in existingContext) {
// if (existingContext.hasOwnProperty(attrib) && existingContext[attrib] && existingContext[attrib].length > 2) {
// if (existingContext[attrib] === newContext[attrib]) {
// console.log("this context already exists", newContext[attrib]);
// contextIsNew = false;
// }
// }
// }
// });
// if (contextIsNew) {
// this._contexts.push(newContext);
// }
// }
// }
}
},
corpus: {
get: function() {
if (this.parent && this.parent.corpus) {
return this.parent.corpus;
}
if (this._corpus) {
return this._corpus;
}
},
set: function(value) {
Iif (this.parent) {
return;
}
this._corpus = value;
}
},
addField: {
value: function(field) {
this.debug("Adding a field to lexical node " + this.id, field);
Eif (!this.fields) {
Eif (!this.corpus || !this.corpus.datumFields || !this.corpus.datumFields.length) {
this.fields = JSON.parse(JSON.stringify(DEFAULT_CORPUS.datumFields));
}
}
return BASE_LEXICON_NODE.prototype.addField.apply(this, arguments);
}
},
merge: {
value: function(callOnSelf, anotherObject, optionalOverwriteOrAsk) {
// this.contexts = LexiconNode.mergeContextsIntoContexts.apply(this, [this]);
// this.contexts = LexiconNode.mergeContextsIntoContexts.apply(this, [anotherObject]);
// console.log("this.contexts", this.contexts);
return BASE_LEXICON_NODE.prototype.merge.apply(this, [callOnSelf, anotherObject, optionalOverwriteOrAsk]);
}
},
uniqueEntriesOnHeadword: {
value: function(a, b) {
if (a && !b && this._fieldDBtype === "LexiconNode") {
a = this;
b = a;
}
// console.log(" checking headword equality ", a.headword, b.headword);
var equal = true;
if (!a || !b) {
equal = false;
return equal;
}
if (a.headword !== b.headword) {
// console.log(" Head words dont match " + a.headword + " vs " + b.headword)
equal = false;
return equal;
}
return equal;
}
},
equalDepreated: {
value: function(b) {
this.warn(" checking equality ", this.headword, b.headword);
var a = this;
var equal = true;
var fieldIndex,
field,
tmpArray;
if (!a || !b) {
equal = false;
return equal;
}
if (a.headword !== b.headword) {
equal = false;
return equal;
}
this.debug(" checking expandedIGTFields", this.expandedIGTFields);
for (fieldIndex in this.expandedIGTFields) {
field = this.expandedIGTFields[fieldIndex];
if (a.hasOwnProperty(field)) {
this.debug(field);
if (a[field] !== b[field]) {
equal = false;
}
}
}
// If the nodes are "equal" then make sure they have the same combineFieldsIfEqual
if (equal) {
for (fieldIndex in this.combineFieldsIfEqual) {
field = this.combineFieldsIfEqual[fieldIndex];
if (a.hasOwnProperty(field)) {
tmpArray = [];
a[field] = a[field].concat(b[field]);
/*jshint loopfunc: true */
a[field].map(function(item) {
if (tmpArray.indexOf(item) === -1) {
tmpArray.push(item);
}
});
a[field] = tmpArray;
b[field] = tmpArray;
}
}
if (a.confidence && b.confidence) {
a.confidence = (parseFloat(a.confidence, 10) + parseFloat(b.confidence, 10)) / 2;
b.confidence = a.confidence;
}
if (a.allomorphs || b.allomorphs) {
var allomorphs = a.allomorphs ? a.allomorphs : "";
allomorphs = allomorphs + allomorphs ? ", " : "";
allomorphs = allomorphs + b.allomorphs ? b.allomorphs : "";
a.allomorphs = allomorphs;
b.allomorphs = allomorphs;
}
}
return equal;
}
},
expandedIGTFields: {
value: ["morphemes", "gloss", "allomorphy", "phonetic", "orthography"]
},
combineFieldsIfEqual: {
value: ["datumids", "utteranceContext"]
},
sortBy: {
value: "headword",
writable: true
},
compare: {
value: function(a, b) {
if (a && !b && this._fieldDBtype === "LexiconNode") {
a = this;
b = a;
}
var result = 0;
var sortBy = a.sortBy;
if (!b || !b || !b[this.sortBy]) {
return -1;
}
if (!a || !a || !a[this.sortBy]) {
return 1;
}
if ((typeof(a[sortBy]) === "number") && (typeof(b[sortBy]) === "number")) {
result = a[sortBy] - b[sortBy];
} else if ((typeof(a[sortBy]) === "string") && (typeof(b[sortBy]) === "string")) {
if (a[sortBy] < b[sortBy]) {
result = -1;
} else if (a[sortBy] > b[sortBy]) {
result = 1;
} else {
result = 0; // Same, so cannot be both in the lexicon since they are the same
}
} else if (typeof(a[sortBy]) === "string") {
result = 1;
} else {
result = -1;
}
return result;
}
},
clean: {
value: function() {
// console.log("Preparing datum with this lexical entry to be cleaned...");
var deferred = Q.defer();
this.cleanedData = [];
var promises = [];
var self = this;
this.proposedChanges = [];
var successFunction = function(doc) {
if (BASE_LEXICON_NODE && BASE_LEXICON_NODE.prototype.INTERNAL_MODELS) {
doc = new BASE_LEXICON_NODE(doc);
}
// console.log(doc);
doc.fields.map(function(datumField) {
if (self.fossil[datumField.label] !== self[datumField.label] && (new RegExp(escapeRegexCharacters(self.fossil[datumField.label]), "i")).test(datumField.mask)) {
var change = {
before: datumField.mask + ""
};
// TODO this makes things lower case... because part of the map reduce seems to be doing that...
datumField.mask = datumField.mask.replace(new RegExp(escapeRegexCharacters(self.fossil[datumField.label]), "ig"), self[datumField.label]);
datumField.value = datumField.mask;
change.after = datumField.mask;
self.proposedChanges.push(change);
return datumField;
}
});
self.cleanedData.push(doc);
};
var failFunction = function(reason) {
self.warn("There was a problem opening your data.", reason);
self.bug("There was a problem opening your data. " + reason.userFriendlyErrors);
};
for (var idIndex = 0; idIndex < this.datumids.length; idIndex++) {
// console.log(this.datumids.length[idIndex]);
promises[idIndex] = CORS.makeCORSRequest({
method: "GET",
dataType: "json",
url: this.url + "/" + this.datumids[idIndex]
});
promises[idIndex].then(successFunction).fail(failFunction);
}
Q.allSettled(promises).then(function(results) {
self.debug(" cleaned promises are finished ", results);
deferred.resolve(self.proposedChanges);
});
return deferred.promise;
}
},
save: {
value: function() {
var deferred = Q.defer(),
self = this,
promises = [];
self.debug("Saving cleaned datum...");
while (this.cleanedData && this.cleanedData.length > 0) {
var cleanedDatum = this.cleanedData.pop();
promises.push(CORS.makeCORSRequest({
method: "PUT",
dataType: "json",
data: cleanedDatum,
url: this.url + "/" + cleanedDatum._id
}));
}
Q.allSettled(promises).then(function(results) {
results.map(function(result) {
if (result.state === "fulfilled" && result.value && result.value.ok) {
self.debug("Your changes have been successfully saved! ", result.value);
} else {
self.bug("One of your changes was not saved " + cleanedDatum._id + " " + result.value.userFriendlyErrors.join("\n"));
}
});
deferred.resolve(results);
self.unsaved = true;
});
return deferred.promise;
}
},
unsaved: {
configurable: true,
get: function() {
if (this._unsaved) {
return this._unsaved;
}
if (this.fossil && !this.equals(this.fossil)) {
this._unsaved = true;
}
return this._unsaved;
},
set: function(value) {
this._unsaved = value;
}
}
});
LexiconNode.HEADWORD_DELIMITER = "|";
LexiconNode.mergeContextsIntoContexts = function(value) {
Iif (!value) {
return;
}
// console.log("mergeContextsIntoContexts: ", value.fieldDBtype);
var context = {
URL: ""
};
// if (value.id && value.id.length > 3 && (!this.id || value.id !== this.id)) {
// context.URL = value.id;
// }
var contextWasFound = false;
Iif (value.context) {
if (typeof value.context === "object") {
context = value.context;
context.URL = context.URL || "";
console.log("Context was already an object on this lexical node", context);
} else {
context.context = value.context + "";
}
// console.log(" Moved context into contexts ", context);
contextWasFound = true;
value.context = "";
delete value.context;
}
if (value.utterance) {
context.utterance = value.utterance + "";
// console.log(" Moved utterance into contexts ", context);
contextWasFound = true;
value.utterance = "";
delete value.utterance;
// console.log(" Moved utterance out: ", value.utterance);
}
if (value.orthography && value.orthography !== value.morphemes) {
context.orthography = value.orthography + "";
// console.log(" Moved orthography into contexts ", value.morphemes);
contextWasFound = true;
value.orthography = "";
delete value.orthography;
}
if (!contextWasFound) {
// console.log("Context was not found, not changed", value.contexts);
return value.contexts;
}
if (!value.contexts) {
// console.log("Initializing contexts");
value.contexts = new LexiconNode.prototype.INTERNAL_MODELS["contexts"]([context]);
} else {
Iif (!(value.contexts instanceof LexiconNode.prototype.INTERNAL_MODELS["contexts"])) {
value.contexts = new LexiconNode.prototype.INTERNAL_MODELS["contexts"]([value.contexts]);
}
value.contexts.add(context);
}
return value.contexts;
};
exports.LexiconNode = LexiconNode;
|