Code coverage report for api/datum/LanguageDatum.js

Statements: 84.02% (163 / 194)      Branches: 68.49% (100 / 146)      Functions: 84.62% (22 / 26)      Lines: 84.02% (163 / 194)      Ignored: none     

All files » api/datum/ » LanguageDatum.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    1 1                                                                                             1 7375 823   7375 700 700       7375 7375     1             11     11 11                     45635 45635 45635           1214 700   1214 1214             56278 56254   24       3419 1292   3419                         187099 180564 125406   55158 55158 55158   6535       5238 5238 4098   5238 5238 5238             1 1           2     2             178985 172451   6534       2186 440   2186                                                             4     4           4                   4 4   4   4 64 12   64 40     24   24 78                           78     24   4 4     4 4 24 5         4 32   32 192 192 70   122     32     4   4                                               1   1 1     1 9       1 9   1 9     1 1 1 1 1 1 1 1 1 1 1     1   1 1 1 1 1   1 1 1 1 1   1 1 1 1 1   1 1 1 1 1   1 1 1 1 1       1             1 4 1 1             1 1 1 1 1       1 1       1 3 3                   1 1           1   1             1 1 9       1 1 1 1 9 2   9 1   9 1     1 1               1  
"use strict";
 
var Datum = require("./Datum").Datum;
var FieldDBObject = require("./../FieldDBObject").FieldDBObject;
 
/**
 * @class The LanguageDatum widget is the place where all linguistic data is
 *        entered; one at a time.
 *
 * @property {DatumField} transcription The transcription field generally
 *           corresponds to the first line in linguistic examples that can
 *           either be written in the language's orthography or a
 *           romanization of the language. An additional field can be added
 *           if the language has a non-roman script. (This was previously called the utterance field).
 * @property {DatumField} gloss The gloss field corresponds to the gloss
 *           line in linguistic examples where the morphological details of
 *           the words are displayed.
 * @property {DatumField} translation The translation field corresponds to
 *           the third line in linguistic examples where in general an
 *           English translation. An additional field can be added if
 *           translations into other languages is needed.
 * @property {DatumField} judgement The judgement is the grammaticality
 *           judgement associated with the datum, so grammatical,
 *           ungrammatical, felicitous, unfelicitous etc.
 * @property {AudioVisual} audioVideo LanguageDatums can be associated with an audio or video
 *           file.
 * @property {Session} session The session provides details about the set of
 *           data elicited. The session will contain details such as date,
 *           language, consultant etc.
 * @property {Comments} comments The comments is a collection of comments
 *           associated with the datum, this is meant for comments like on a
 *           blog, not necessarily notes, which can be encoded in a
 *           field.(Use Case: team discussing a particular datum)
 * @property {DatumTags} datumtags The datum tags are a collection of tags
 *           associated with the datum. These are made completely by the
 *           user.They are like blog tags, a way for the user to make
 *           categories without make a hierarchical structure, and make
 *           datum easier for search.
 * @property {Date} dateEntered The date the LanguageDatum was first saved.
 * @property {Date} dateModified The date the LanguageDatum was last saved.
 *
 * @description The initialize function brings up the datum widget in small
 *              view with one set of datum fields. However, the datum widget
 *              can contain more than datum field set and can also be viewed
 *              in full screen mode.
 *
 * @name  LanguageDatum
 * @extends Datum
 * @constructs
 */
var LanguageDatum = function LanguageDatum(options) {
  if (!this._fieldDBtype) {
    this._fieldDBtype = "LanguageDatum";
  }
  if (options && typeof options === "string") {
    this.debug("Turning LanguageDatum " + options + " to an object");
    options = {
      orthography: options
    };
  }
  this.debug("Constructing LanguageDatum: ", options);
  Datum.apply(this, [options]);
};
 
LanguageDatum.prototype = Object.create(Datum.prototype, /** @lends LanguageDatum.prototype */ {
  constructor: {
    value: LanguageDatum
  },
 
  length: {
    get: function() {
      Iif (this.utterance) {
        return this.utterance.length;
      }
      Eif (this.orthography) {
        return this.orthography.length;
      }
      if (this.morphemes) {
        return this.morphemes.length;
      }
    }
  },
 
  orthography: {
    configurable: true,
    get: function() {
      Eif (this.fields && this.fields.orthography) {
        this.debug(" getting orthography " + this._id + " to " + this.fields.orthography.value);
        return this.fields.orthography.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      if (!this.fields || !this.fields.orthography) {
        this.addField("orthography");
      }
      this.debug("    Setting orthography " + this._id + " to " + value);
      this.fields.orthography.value = value;
    }
  },
 
  utterance: {
    configurable: true,
    get: function() {
      if (this.fields && this.fields.utterance) {
        return this.fields.utterance.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      if (!this.fields || !this.fields.utterance) {
        this.addField("utterance");
      }
      this.fields.utterance.value = value;
    }
  },
 
  /**
   * Morphemes of the datum, if morphemes is empty it will provide the utterance
   * or the orthography as a last resort copy
   *
   * @return {String} morphemes, or utterance or orthography
   */
  morphemes: {
    configurable: true,
    get: function() {
      if (this.fields && this.fields.morphemes) {
        if (this.fields.morphemes.value) {
          return this.fields.morphemes.value;
        }
        var placeholder = this.utterance || this.orthography || FieldDBObject.DEFAULT_STRING;
        this.debug("morphemes is not defined so using " + placeholder);
        return placeholder + "";
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      this.debug("setting morphemes " + value);
      if (!this.fields || !this.fields.morphemes) {
        this.addField("morphemes");
      }
      this.debug(" this.fields.morphemes ", this.fields.morphemes);
      this.fields.morphemes.value = value;
      this.debug(" this.fields.morphemes.value ", this.fields.morphemes.value);
    }
  },
 
  allomorphs: {
    configurable: true,
    get: function() {
      Eif (this.fields && this.fields.allomorphs) {
        return this.fields.allomorphs.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      Iif (!this.fields || !this.fields.allomorphs) {
        this.addField("allomorphs");
      }
      this.fields.allomorphs.value = value;
    }
  },
 
  gloss: {
    configurable: true,
    get: function() {
      if (this.fields && this.fields.gloss) {
        return this.fields.gloss.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      if (!this.fields || !this.fields.gloss) {
        this.addField("gloss");
      }
      this.fields.gloss.value = value;
    }
  },
 
  syntacticCategory: {
    configurable: true,
    get: function() {
      if (this.fields && this.fields.syntacticCategory) {
        return this.fields.syntacticCategory.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      if (!this.fields || !this.fields.syntacticCategory) {
        this.addField("syntacticCategory");
      }
      this.fields.syntacticCategory.value = value;
    }
  },
 
  translation: {
    configurable: true,
    get: function() {
      if (this.fields && this.fields.translation) {
        return this.fields.translation.value;
      } else {
        return FieldDBObject.DEFAULT_STRING;
      }
    },
    set: function(value) {
      Iif (!this.fields || !this.fields.translation) {
        this.addField("translation");
      }
      this.fields.translation.value = value;
    }
  },
 
  igt: {
    get: function() {
      var igtLines = {},
        parallelText = {},
        tuples = [],
        feederWord,
        fullWord,
        lengthOfLongestIGTLineInWords,
        igtLine,
        cellIndex,
        tuple;
 
      var punctuationToRemove = /[#?!,\/\(\)\*\#]/g;
      var whiteSpaceSplit = /[ \t\n]+/;
      // var leipzigSplit = /[=-]+/;
      var cleanUngrammaticalitySubstitutions = false;
 
      this.fields.map(function(field) {
        if (field.type && field.type.indexOf("parallelText") > -1) {
          parallelText[field.id] = field.value;
        }
        if (!field.type || field.type.indexOf("IGT") === -1) {
          return;
        }
 
        var chunks = field.value.replace(/#?!.,\//g, "").split(whiteSpaceSplit);
 
        chunks = chunks.map(function(chunk) {
          Iif (cleanUngrammaticalitySubstitutions) {
            // If the token it not null or the empty string
            if (chunk) {
              // Replace (*_) with _
              feederWord = chunk.replace(/\(\*[^)]*\)/g, "$1");
              // Replace *(_) with _
              feederWord = feederWord.replace(/\*\(([^)]*)\)/, "$1");
              // Remove all remaining punctuation
              fullWord = feederWord.replace(punctuationToRemove, "");
              chunk = fullWord;
            }
          }
 
          // return chunk.split(leipzigSplit);
          return chunk;
        });
 
        igtLines[field.id] = chunks;
      });
      this.debug("Collected all the IGT lines", igtLines);
      this.debug("Collected all the Paralel Text lines", parallelText);
 
      // Build triples
      lengthOfLongestIGTLineInWords = 0;
      for (igtLine in igtLines) {
        if (igtLines[igtLine] && igtLines[igtLine].length > lengthOfLongestIGTLineInWords) {
          lengthOfLongestIGTLineInWords = igtLines[igtLine].length;
        }
      }
 
      // for each word
      for (cellIndex = 0; cellIndex < lengthOfLongestIGTLineInWords; cellIndex++) {
        tuple = {};
        // for each row of the igt
        for (igtLine in igtLines) {
          this.debug("working on   " + igtLine, igtLines[igtLine]);
          if (igtLines[igtLine] && igtLines[igtLine].length > cellIndex && igtLines[igtLine][cellIndex]) {
            tuple[igtLine] = igtLines[igtLine][cellIndex];
          } else {
            tuple[igtLine] = "";
          }
        }
        tuples.push(tuple);
      }
 
      this.debug("IGT+ tuples", tuples);
 
      return {
        tuples: tuples,
        parallelText: parallelText
      };
 
    },
    set: function() {
      this.warn("Setting the igt has to be copy pasted from one of the other codebases");
    }
  },
 
  /**
   * The LaTeXiT function automatically mark-ups an example in LaTeX code
   * (\exg. \"a) and then copies it on the export modal so that when the user
   * switches over to their LaTeX file they only need to paste it in.
   *
   * We did a poll on Facebook among EGGers, and other linguists we know and
   * found that Linguex was very popular, and GB4E, so we did the export in
   * GB4E.
   */
  /* jshint ignore:start */
 
  laTeXiT: {
    value: function(showInExportModal) {
      this.debug(showInExportModal);
      //corpus's most frequent fields
      var frequentFields;
      Iif (this.application && this.application.corpus && this.application.corpus.frequentFields) {
        frequentFields = this.application.corpus.frequentFields;
      } else {
        frequentFields = this.fields.map(function(field) {
          return field.id
        });
      }
      //this datum/datalist's datumfields and their names
      var fields = this.fields.map(function(field) {
        return field.value;
      });
      var fieldLabels = this.fields.map(function(field) {
        return field.id;
      });
      //setting up for IGT case...
      var judgementIndex = -1;
      var judgement = "";
      var utteranceIndex = -1;
      var utterance = "";
      var morphemesIndex = -1;
      var morphemes = "";
      var glossIndex = -1;
      var gloss = "";
      var translationIndex = -1;
      var translation = "";
      var result = "\n\\begin{exe} \n  \\ex \[";
 
      //IGT case:
      Eif (this.datumIsInterlinearGlossText()) {
        /* get the key pieces of the IGT and delete them from the fields and fieldLabels arrays*/
        judgementIndex = fieldLabels.indexOf("judgement");
        Eif (judgementIndex >= 0) {
          judgement = fields[judgementIndex];
          fieldLabels.splice(judgementIndex, 1);
          fields.splice(judgementIndex, 1);
        }
        utteranceIndex = fieldLabels.indexOf("utterance");
        Eif (utteranceIndex >= 0) {
          utterance = fields[utteranceIndex];
          fieldLabels.splice(utteranceIndex, 1);
          fields.splice(utteranceIndex, 1);
        }
        morphemesIndex = fieldLabels.indexOf("morphemes");
        Eif (morphemesIndex >= 0) {
          morphemes = fields[morphemesIndex];
          fieldLabels.splice(morphemesIndex, 1);
          fields.splice(morphemesIndex, 1);
        }
        glossIndex = fieldLabels.indexOf("gloss");
        Eif (glossIndex >= 0) {
          gloss = fields[glossIndex];
          fieldLabels.splice(glossIndex, 1);
          fields.splice(glossIndex, 1);
        }
        translationIndex = fieldLabels.indexOf("translation");
        Eif (translationIndex >= 0) {
          translation = fields[translationIndex];
          fieldLabels.splice(translationIndex, 1);
          fields.splice(translationIndex, 1);
        }
        //print the main IGT, escaping special latex chars
        /* ignore unnecessary escapement */
        result = result + this.escapeLatexChars(judgement) + "\]\{" + this.escapeLatexChars(utterance) +
          "\n  \\gll " + this.escapeLatexChars(morphemes) + "\\\\" +
          "\n  " + this.escapeLatexChars(gloss) + "\\\\" +
          "\n  \\trans " + this.escapeLatexChars(translation) + "\}" +
          "\n\\label\{\}";
      }
      //remove any empty fields from our arrays
      for (var i = fields.length - 1; i >= 0; i--) {
        if (!fields[i]) {
          fields.splice(i, 1);
          fieldLabels.splice(i, 1);
        }
 
      }
      /*throughout this next section, print frequent fields and infrequent ones differently
      frequent fields get latex'd as items in a description and infrequent ones are the same,
      but commented out.*/
      Eif (fields && (fields.length > 0)) {
        var numInfrequent = 0;
        for (var field in fields) {
          Eif (frequentFields.indexOf(fieldLabels[field]) >= 0) {
            break;
          }
          numInfrequent++;
        }
        Eif (numInfrequent !== fieldLabels.length) {
          result = result + "\n  \\begin\{description\}";
        } else {
          result = result + "\n%  \\begin\{description\}";
        }
        for (field in fields) {
          Eif (fields[field] && (frequentFields.indexOf(fieldLabels[field]) >= 0)) {
            result = result + "\n    \\item\[\\sc\{" + this.escapeLatexChars(fieldLabels[field]) + "\}\] " + this.escapeLatexChars(fields[field]);
          } else if (fields[field]) {
            /* If as a field that is designed for LaTex dont excape the LaTeX characters */
            if (fieldLabels[field].toLowerCase().indexOf("latex") > -1) {
              result = result + "\n " + fields[field];
            } else {
              result = result + "\n%    \\item\[\\sc\{" + this.escapeLatexChars(fieldLabels[field]) + "\}\] " + this.escapeLatexChars(fields[field]);
            }
          }
        }
        Eif (numInfrequent !== fieldLabels.length) {
          result = result + "\n  \\end\{description\}";
        } else {
          result = result + "\n%  \\end\{description\}";
        }
 
      }
      result = result + "\n\\end{exe}\n\n";
 
      return result;
    }
  },
  /* jshint ignore:end */
 
  datumIsInterlinearGlossText: {
    value: function(fieldLabels) {
      Eif (!fieldLabels) {
        fieldLabels = this.fields.map(function(field) {
          return field.id;
        });
      }
 
      var utteranceOrMorphemes = false;
      var gloss = false;
      var trans = false;
      for (var fieldLabel in fieldLabels) {
        if (fieldLabels[fieldLabel] === "utterance" || fieldLabels[fieldLabel] === "morphemes") {
          utteranceOrMorphemes = true;
        }
        if (fieldLabels[fieldLabel] === "gloss") {
          gloss = true;
        }
        if (fieldLabels[fieldLabel] === "translation") {
          trans = true;
        }
      }
      Eif (gloss || utteranceOrMorphemes || trans) {
        return true;
      } else {
        return false;
      }
    }
  }
 
});
exports.LanguageDatum = LanguageDatum;