Code coverage report for api/glosser/Glosser.js

Statements: 69.3% (237 / 342)      Branches: 65.02% (158 / 243)      Functions: 70.59% (24 / 34)      Lines: 69.3% (237 / 342)      Ignored: none     

All files » api/glosser/ » Glosser.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 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 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916    1 1 1 1     1 1                               1 30 30   30   30 30     1 1           1                                       1 6     6 6 6 3 3 6 6       6 6 6   6 17 2   15   15 15   15 33 33 33 33   22 22     11 11       6   6 5   6   6 6                             1 14 14 14   14 14 14   14                             1                                   1 22     22 74 64     10       22     1 2     1                           6     6 2                   61 61     4                             2     2 2 2   2 2 2     2 2 2         2         2       2     2       2 2                                                                                   2 2   2                 2           351                             351     16 15   16     15 15 15     510 90       420         420 285 285   420     15 15   16                             10       10         10       10         10 1       9 9 9   9 9 2   7 7 7     2 2                     11     11   11     11 11 200 10   190 47   47 42     47 47           143     11                 3 3   3     3 3 3   3 1 1   3 3 3 12 6                 3     3   6 6     6 22 6     16 94 94 14       80 10 10         10 10             6 2 2 2 2 2 2   4 4         6 6   6 6   6 23 5   18 108     108 15 15   1 1 1   14 14                           6 6 6 25   6   6   6             6   6     3   3         3 3           12 12       12 11 11   1     12                                                                                                                                                           10     10 10 10   10         2     2     2 2                                                                                                                                                                                                                                                                                       4 4 2 2     2       2   2           4                             1  
"use strict";
/* globals localStorage */
var Q = require("q");
var FieldDBObject = require("../FieldDBObject").FieldDBObject;
var mapReduceFactory = require("./../map_reduce/MapReduce").MapReduceFactory;
var Lexicon = require("../lexicon/Lexicon").Lexicon;
 
// Load n grams map reduce which is used in both couchdb and in the codebase
var morphemeNGramsMapString = require("../../map_reduce_data/views/morpheme_n_grams/map").morpheme_n_grams;
var MORPHEMES_N_GRAMS_MAP_REDUCE = mapReduceFactory({
  filename: "morpheme_n_grams",
  mapString: morphemeNGramsMapString
});
 
/**
 * @class The Glosser is able to guess morpheme segmentation and/or glosses from an orthography/transcription.
 * The default Glosser (this one) uses a lexicon which is generated off of existing data in the corpus to do this.
 *
 * The Glosser can display a visual representation of itself if D3 is provided to it as options
 * or D3 is loaded in the context of the app.
 *
 * @name  Glosser
 * @extends FieldDBObject
 * @constructs
 */
var Glosser = function Glosser(options) {
  Eif (!this._fieldDBtype) {
    this._fieldDBtype = "Glosser";
  }
  options = options || {};
 
  this.debug("Constructing Glosser length: ", options);
  FieldDBObject.apply(this, arguments);
};
 
Glosser.morphemeBoundaryRegEX = /[-=]/g;
Glosser.Lexicon = Lexicon;
 
/**
 * [morpheme_n_grams_mapReduce description]
 * @type {Function}
 */
Glosser.morpheme_n_grams_mapReduce = MORPHEMES_N_GRAMS_MAP_REDUCE;
 
/**
 * Finds all combinations of an utterance line by looping through all
 * possible parses of all words in the utterance line.
 *
 * This results in a list of alternateMorphemeLines which can be shown to the user
 * as a type-ahead on the utterance line.
 *
 * These alternateMorphemeLines can be further filtered using other information
 * (likelyhood of long distance relationships between morphemes,
 * likelyhood of long distance relationships between glosses).
 *
 * http://stackoverflow.com/questions/15298912/javascript-generating-combinations-from-n-arrays-with-m-elements
 *
 * @param  {Object} options An object containing metadata,
 *                           options.columns is used to generate the combinations
 *                           options.sort is optionally used to sort the resulting combinations
 * @return {Object}         [description]
 */
Glosser.calculateAllAlternativeCombinations = function(options) {
  var debug = function() {
    // console.log(arguments);
  };
  options.alternateMorphemeLines = options.alternateMorphemeLines || [];
  debug("Beginning with a set of alternatives " + options.alternateMorphemeLines);
  if (options.alternateMorphemeSegmentationsByWord) {
    options.columns = [];
    for (var word in options.alternateMorphemeSegmentationsByWord) {
      Eif (options.alternateMorphemeSegmentationsByWord.hasOwnProperty(word) && options.alternateMorphemeSegmentationsByWord[word].fullParses) {
        options.columns.push(options.alternateMorphemeSegmentationsByWord[word].fullParses);
      }
    }
  }
  options.columns = options.columns || [];
  var utteranceWordCount = options.columns.length - 1;
  debug("Finding all alternative parses for ", options.columns);
 
  var recurse = function(parseInProgress, currentColumnIndex) {
    if (!options.columns[currentColumnIndex]) {
      return;
    }
    Glosser.removeRedundantCopies(options.columns[currentColumnIndex]);
 
    var numberOfParsesForThisWord = options.columns[currentColumnIndex].length;
    debug(" There are " + numberOfParsesForThisWord + " alternative parses for word " + (currentColumnIndex + 1));
 
    for (var possibleParseIndex = 0; possibleParseIndex < numberOfParsesForThisWord; possibleParseIndex++) {
      var copyOfParseInProgress = parseInProgress.slice(0); // clone column
      var possibleParseOfThisWord = options.columns[currentColumnIndex][possibleParseIndex];
      copyOfParseInProgress.push(possibleParseOfThisWord);
      if (currentColumnIndex === utteranceWordCount) {
        // We have a full parse of all words
        debug("  found a complete alternate", copyOfParseInProgress);
        options.alternateMorphemeLines.push(copyOfParseInProgress.join(" "));
      } else {
 
        debug("\n" + copyOfParseInProgress + ":  set");
        recurse(copyOfParseInProgress, currentColumnIndex + 1);
      }
    }
  };
  recurse([], 0);
 
  if (typeof options.sort === "function") {
    options.alternateMorphemeLines = options.alternateMorphemeLines.sort(options.sort);
  }
  Glosser.removeRedundantCopies(options.alternateMorphemeLines);
 
  debug(options.alternateMorphemeLines);
  return options;
};
 
/**
 * If passed to a alternateMorphemeLines.map() results in a sort order where more
 * segmentations are listed first (on top in a typeahead).
 *
 * This is only useful if the user doesn't realize that the app can segment
 * the utterance line, and/or if the glosser's segmentations are conservative
 * and only offer real segmentations.
 *
 * @param  {String} a A string (normallly a morphemes line)
 * @param  {String} b A string (normallly a morphemes line)
 * @return {boolean}   If a has more segmentations than b
 */
Glosser.sortAlternatesByMoreSegmentation = function(a, b) {
  var segmentationsA = 0;
  Eif (a && typeof a.split === "function") {
    segmentationsA = a.split(Glosser.morphemeBoundaryRegEX).length;
  }
  var segmentationsB = 0;
  Eif (b && typeof b.split === "function") {
    segmentationsB = b.split(Glosser.morphemeBoundaryRegEX).length;
  }
  return segmentationsB - segmentationsA;
};
 
/**
 * If passed to a alternateMorphemeLines.map() results in a sort order where fewer segmentations
 * are listed first (on top in a typeahead).
 *
 * This is useful if the user prefers to see segmentations, but will most often
 * choose the utteracne line as the morphemes line and then add the segmentations
 * themself.
 *
 * @param  {String} a A string (normallly a morphemes line)
 * @param  {String} b A string (normallly a morphemes line)
 * @return {boolean}   If a has more segmentations than b
 */
Glosser.sortAlternatesByLessSegmentation = function(a, b) {
  var segmentationsA = 0;
  if (a && typeof a.split === "function") {
    segmentationsA = a.split(Glosser.morphemeBoundaryRegEX).length;
  }
  var segmentationsB = 0;
  if (b && typeof b.split === "function") {
    segmentationsB = b.split(Glosser.morphemeBoundaryRegEX).length;
  }
  return segmentationsA - segmentationsB;
};
 
/**
 * Removes redundant copies from an array
 *
 * @param  {Array} input An array to be cleaned
 * @return {Array}       The same array after removing redundant copies
 */
Glosser.removeRedundantCopies = function(input) {
  var hash = {};
  // var copy = [];
 
  for (var i = input.length - 1; i >= 0; --i) {
    if (!hash.hasOwnProperty(input[i])) {
      hash[input[i]] = true;
      // copy.unshift(input[i]);
    } else {
      input.splice(i, 1);
    }
  }
 
  return input;
};
 
Glosser.removeWordBoundaryPlaceholders = function(input) {
  return input.replace(/-?@-?/g, " ").trim();
};
 
Glosser.prototype = Object.create(FieldDBObject.prototype, /** @lends Glosser.prototype */ {
  constructor: {
    value: Glosser
  },
 
  // Internal models: used by the parse function
  INTERNAL_MODELS: {
    value: {
      lexicon: Lexicon,
    }
  },
 
  dbname: {
    get: function() {
      Iif (this._dbname) {
        return this._dbname;
      }
      if (this.corpus && this.corpus.dbname) {
        return this.corpus.dbname;
      }
    },
    set: function(value) {
      this._dbname = value;
    }
  },
 
  lexicon: {
    get: function() {
      this.debug("getting lexicon");
      return this._lexicon;
    },
    set: function(value) {
      this.ensureSetViaAppropriateType("lexicon", value);
    }
  },
 
  downloadPrecedenceRules: {
    value: function(glosserURL) {
      this.warn("DEPRECATED downloadPrecedenceRules use fetch instead");
      return this.fetch({
        url: glosserURL
      });
    }
  },
 
  fetch: {
    value: function(options) {
      Iif (this.fetching && this.whenFetched) {
        return this.whenFetched;
      }
      var self = this;
      var deferred = Q.defer();
      this.whenFetched = deferred.promise;
 
      options = options || {};
      var glosserURL = "";
      Iif (options && options.url) {
        glosserURL = options.url;
      }
      Eif (!glosserURL || glosserURL === "default") {
        var error;
        Iif (!this.dbname && !options.dbname) {
          error = {
            userFriendlyErrors: ["Glosser's webservice can't be guessed, there is no current database identifier so the URL must be defined."]
          };
        }
        Iif (!this.corpus) {
          error = {
            userFriendlyErrors: ["Glosser's webservice can't be guessed, there is no corpus loaded so the URL must be defined."]
          };
        }
        Iif (error) {
          deferred.reject(error);
          return this.whenFetched;
        }
        Iif (this.corpus.prefs && this.corpus.prefs.glosserURL) {
          glosserURL = this.corpus.prefs.glosserURL;
        } else {
          glosserURL = this.corpus.url + "/_design/data/_view/" + MORPHEMES_N_GRAMS_MAP_REDUCE.filename + "?group=true";
        }
      }
 
      this.fetching = true;
      Lexicon.CORS.makeCORSRequest({
          type: "GET",
          url: glosserURL
        })
        .then(function(reducedCouchDBresult) {
          if (!reducedCouchDBresult || !reducedCouchDBresult.rows || reducedCouchDBresult.rows.length === undefined) {
            self.fetching = false;
            deferred.reject({
              details: reducedCouchDBresult,
              userFriendlyErrors: ["The result from the server " + glosserURL + " was not a standard response. Please report this."]
            });
            return;
          }
 
          // Reduce the reducedCouchDBresult such that reducedCouchDBresult which are found in multiple source
          // words are only used/included once.
          self.morphemeSegmentationKnowledgeBase = reducedCouchDBresult;
          self.fetching = false;
          // Save the reduced precedence self.morphemeSegmentationKnowledgeBase in localStorage
          if (self.dbname) {
            try {
              localStorage.setItem(self.dbname + "morphemeSegmentationKnowledgeBase", JSON.stringify(self.morphemeSegmentationKnowledgeBase));
            } catch (error) {
              if (error.message === "localStorage is not defined") {
                console.warn("cannot save precedence self.morphemeSegmentationKnowledgeBase for offline use");
              } else {
                //remove other corpora's self.morphemeSegmentationKnowledgeBase to make space for the most recent...
                for (var x in localStorage) {
                  if (x.indexOf("self.morphemeSegmentationKnowledgeBase") > -1) {
                    localStorage.removeItem(x);
                  }
                }
                try {
                  localStorage.setItem(self.dbname + "self.morphemeSegmentationKnowledgeBase", JSON.stringify(self.morphemeSegmentationKnowledgeBase));
                } catch (error2) {
                  self.warn("Your lexicon is huge!", error2);
                }
              }
            }
          }
          deferred.resolve(self.morphemeSegmentationKnowledgeBase);
        }, function(reason) {
          self.fetching = false;
          self.debug("Error getting precedence self.morphemeSegmentationKnowledgeBase:", reason);
          // self.bug(reason.userFriendlyErrors.join(" "));
          deferred.reject(reason);
        })
        .fail(function(exception) {
          self.fetching = false;
          self.warn("Exception getting precedence self.morphemeSegmentationKnowledgeBase:", exception.stack);
          self.bug("Exception getting precedence rules in the glosser.");
          deferred.reject(exception);
        });
 
      return this.whenFetched;
    }
  },
 
  morphemeSegmentationKnowledgeBase: {
    get: function() {
      Iif (!this._morphemeSegmentationKnowledgeBase && (!this.lastMorphemePrecedenceRelationsLookupTimestamp && Date.now() - this.lastMorphemePrecedenceRelationsLookupTimestamp > 30000) && this.corpus && this.corpus.this.dbname) {
        try {
          var value = localStorage.getItme(this.corpus.this.dbname + "morphemeSegmentationKnowledgeBase");
          if (value) {
            value = JSON.parse(value);
          }
          if (value) {
            this.morphemeSegmentationKnowledgeBase = value;
          }
          this.lastMorphemePrecedenceRelationsLookupTimestamp = Date.now();
        } catch (exception) {
          console.warn("Glosser's morpheme segmentation knowledge cannot be persisted across page loads for offline use.", exception);
          this.lastMorphemePrecedenceRelationsLookupTimestamp = Infinity;
        }
      }
      return this._morphemeSegmentationKnowledgeBase;
    },
    set: function(value) {
      if (value && value.rows && typeof value.rows.filter === "function") {
        value = value.rows;
      }
      if (value && typeof value.filter === "function") {
        // Reduce the rules such that rules which are found in multiple source
        // words are only used/included once.
        var uniqueRules = {};
        var size = 0;
        value = value.map(function(row) {
          /* TODO handle lexicon precedecne rules instead of simple condensed rules */
 
          if (row.key && typeof row.key.indexOf === "function" && row.key.indexOf("@-@") > -1) {
            return;
          }
 
          // If all ready reduced, use the count as the value
          Iif (typeof row.value === "number") {
            uniqueRules[row.key] = row.value;
            size++;
          } else {
            // If not ready reduced, add the value into an array which will serve as the count
            if (!uniqueRules[row.key]) {
              uniqueRules[row.key] = uniqueRules[row.key] || [];
              size++;
            }
            uniqueRules[row.key].push(row.value);
          }
        });
        uniqueRules.length = size;
        value = uniqueRules;
      }
      this._morphemeSegmentationKnowledgeBase = value;
    }
  },
 
  /**
   * Takes in an utterance line and, based on our current set of precendence
   * self.morphemeSegmentationKnowledgeBase, guesses what the morpheme line would be. The algorithm is
   * very conservative.
   *
   * @param {String} unparsedUtterance The raw utterance line.
   *
   * @return {String} The guessed morphemes line.
   */
  morphemefinder: {
    value: function(datum, justCopyDontGuessIGT) {
      Iif (!datum || !datum.utterance) {
        return datum;
      }
      // Keep the current morphemes as an alternate, and copy the utterance into the morphemes if it was empty
      Iif (datum.morphemes) {
        datum.alternateMorphemeLines = datum.alternateMorphemeLines || [];
        datum.alternateMorphemeLines.unshift(datum.morphemes);
        Glosser.removeRedundantCopies(datum.alternateMorphemeLines);
      } else {
        datum.morphemes = datum.utterance + "";
      }
 
      // If the user doesnt want to guess on this call, this datum, or any glosser data, dont bother guessing segmentation
      Iif (justCopyDontGuessIGT || this.justCopyDontGuessIGT || datum.justCopyDontGuessIGT) {
        return datum;
      }
 
      // If there is no data, dont bother guessing segmentation
      if (!this.morphemeSegmentationKnowledgeBase || !this.morphemeSegmentationKnowledgeBase.length) {
        return datum;
      }
 
      // Divide the utterance line into words
      datum.utteranceWithExplictWordBoundaries = "@" + datum.utterance.trim().toLocaleLowerCase().split(/\s+/).join("@") + "@";
      datum.matchingRules = [];
      this.findRelevantSegmentationContexts(datum);
 
      this.debug(" Found matchingRules " + datum.utterance + " which conservatively yields exact segmentation matches " + datum.utteranceWithExplictWordBoundaries, datum.matchingRules);
      if (this.conservative === false) {
        console.warn(" Continuing to look harder for segmentation. Currently " + datum.utterance + " directly segments as " + datum.utteranceWithExplictWordBoundaries);
      } else {
        this.debug(" Not looking harder for segmentation. Currently " + datum.utterance + " directly segments as " + datum.utteranceWithExplictWordBoundaries);
        datum.morphemes = datum.utteranceWithExplictWordBoundaries.replace(/-?@-?/g, " ").trim();
        return datum;
      }
 
      this.findaAllPossibleMorphemeLines(datum);
      return datum;
    }
  },
 
  /**
   * Find the this.morphemeSegmentationKnowledgeBase which match in local precedence
   * @param  {Object} options [description]
   * @return {Object} options after processing                               [description]
   */
  findRelevantSegmentationContexts: {
    value: function(options) {
      Iif (!options.utteranceWithExplictWordBoundaries) {
        return options;
      }
      this.debug("Looking for matching rules " + options.utteranceWithExplictWordBoundaries);
 
      Iif (!options.matchingRules) {
        options.matchingRules = [];
      }
      var originalUtteranceWithWordBoundaries = options.utteranceWithExplictWordBoundaries + "";
      for (var segmentation in this.morphemeSegmentationKnowledgeBase) {
        if (!this.morphemeSegmentationKnowledgeBase.hasOwnProperty(segmentation) || segmentation === "length") {
          continue;
        }
        if (originalUtteranceWithWordBoundaries.indexOf(segmentation.replace(Glosser.morphemeBoundaryRegEX, "")) >= 0) {
          this.debug("   matching rule " + originalUtteranceWithWordBoundaries + " <-" + segmentation);
          // If this has enough context then consider it a correct segmentation
          if (segmentation.split(Glosser.morphemeBoundaryRegEX).length > 2) {
            options.utteranceWithExplictWordBoundaries = options.utteranceWithExplictWordBoundaries.replace(segmentation.replace(Glosser.morphemeBoundaryRegEX, ""), segmentation);
          }
 
          this.debug("      now " + options.utteranceWithExplictWordBoundaries);
          options.matchingRules.push({
            segmentation: segmentation,
            contexts: this.morphemeSegmentationKnowledgeBase[segmentation],
            morphemes: segmentation.split(Glosser.morphemeBoundaryRegEX)
          });
        } else {
          this.debug("   not matching rule " + originalUtteranceWithWordBoundaries + " |- " + segmentation);
        }
      }
      return options;
    }
  },
 
  findaAllPossibleMorphemeLines: {
    value: function(options) {
 
      // Attempt to find the longest template which the matching this.morphemeSegmentationKnowledgeBase can
      // generate from start to end
      options.alternateMorphemeLines = options.alternateMorphemeLines || [];
      options.usedRules = options.usedRules || [];
 
      Iif (!options.utteranceWithExplictWordBoundaries) {
        return options;
      }
      options.utterance = options.utterance || Glosser.removeWordBoundaryPlaceholders(options.utteranceWithExplictWordBoundaries);
      options.alternateMorphemeLines.push(options.utterance);
      this.debug("alternateMorphemeLines " + options.alternateMorphemeLines);
 
      if (!options.matchingRules || !options.matchingRules.length) {
        this.findRelevantSegmentationContexts(options);
        options.alternateMorphemeLines.push(Glosser.removeWordBoundaryPlaceholders(options.utteranceWithExplictWordBoundaries));
      }
      Eif (!options.alternateMorphemeSegmentationsByWord) {
        options.alternateMorphemeSegmentationsByWord = {};
        options.utteranceWithExplictWordBoundaries.split(/-?@-?/).map(function(word) {
          if (word) {
            options.alternateMorphemeSegmentationsByWord[word.replace(Glosser.morphemeBoundaryRegEX, "")] = {
              fullParses: [word],
              prefixTemplates: [],
              suffixTemplates: []
            };
          }
        });
      }
 
      var match,
        matchIndex;
 
      for (var word in options.alternateMorphemeSegmentationsByWord) {
 
        this.debug("\nFinding prefixtemplate " + word);
        var prefixtemplate = ["@"];
 
        // up to 10 morphemes long
        for (var i = 0; i < 10; i++) {
          if (prefixtemplate[i] === undefined) {
            break;
          }
          // Look at each rule
          for (matchIndex = 0; matchIndex < options.matchingRules.length; matchIndex++) {
            match = options.matchingRules[matchIndex];
            if (prefixtemplate.length > 2 && prefixtemplate[prefixtemplate.length - 1] === "@") {
              continue; //we found the end already
            }
 
            // this morpheme matches, and the following morpheme can be found somewhere later in the word
            if (prefixtemplate[i] === match.morphemes[0] && (match.morphemes[1] === "@" || word.indexOf(match.morphemes[1]) > -1)) {
              options.usedRules.push(match);
              Iif (prefixtemplate[i + 1]) { // ambiguity (two potential following morphemes)
                this.debug("Ambiguity point for prefixes " + word + " " + prefixtemplate[i + 1], options.alternateMorphemeSegmentationsByWord[word]);
                options.alternateMorphemeSegmentationsByWord[word].prefixTemplates.push(prefixtemplate.pop());
                break;
              } else {
                prefixtemplate[i + 1] = match.morphemes[1];
                this.debug("   prefixtemplate " + prefixtemplate.join("-"));
              }
            }
          }
        }
 
        // If prefix template matches the whole word (ie we have seen it before, return the prefix template exactly)
        if (prefixtemplate.length > 2 && prefixtemplate[0] === "@" && prefixtemplate[prefixtemplate.length - 1] === "@") {
          this.debug("Prefix matches entire word. " + prefixtemplate.join("-"));
          prefixtemplate.pop(); //remove final @
          prefixtemplate.shift(); //remove intitial @
          options.alternateMorphemeSegmentationsByWord[word].fullParses.push(prefixtemplate.join("-"));
          prefixtemplate = options.alternateMorphemeSegmentationsByWord[word].prefixTemplates.pop() || ["@"];
          this.debug("Considering another parse " + prefixtemplate.join("-"));
        } else {
          prefixtemplate = options.alternateMorphemeSegmentationsByWord[word].prefixTemplates.pop() || ["@"];
          this.debug("Considering another parse " + prefixtemplate.join("-"));
        }
 
        // If the prefix template hit ambiguity in the middle, try from the suffix
        // in until it hits ambiguity
        var suffixtemplate = [];
        Eif (prefixtemplate[prefixtemplate.length - 1] !== "@" || prefixtemplate.length === 1) {
          // Suffix:
          suffixtemplate.push("@");
          this.debug("   suffixtemplate " + suffixtemplate.join("-"));
 
          for (var ii = 0; ii < 10; ii++) {
            if (suffixtemplate[ii] === undefined) {
              break;
            }
            for (var suffixMatchIndex = 0; suffixMatchIndex < options.matchingRules.length; suffixMatchIndex++) {
              var suffixmatch = options.matchingRules[suffixMatchIndex];
 
              // this morpheme matches, and the following morpheme can be found somewhere later in the word
              if (suffixtemplate[ii] === suffixmatch.morphemes[1] && (suffixmatch.morphemes[0] === "@" || word.indexOf(suffixmatch.morphemes[0]) > -1)) {
                options.usedRules.push(suffixmatch);
                if (suffixtemplate[ii + 1]) { // ambiguity (two potential
                  // following morphemes)
                  this.debug("Ambiguity point for suffixes " + word + " " + suffixtemplate[ii + 1], options.alternateMorphemeSegmentationsByWord[word]);
                  options.alternateMorphemeSegmentationsByWord[word].suffixTemplates.push(suffixtemplate.pop());
                  break;
                } else {
                  suffixtemplate[ii + 1] = suffixmatch.morphemes[0];
                  this.debug("   suffixtemplate " + suffixtemplate.join("-"));
 
                }
              }
            }
          }
        } else {
          this.debug("what is this condition");
        }
 
        // Combine prefix and suffix templates into one regular expression which
        // can be tested against the word to find a potential parse.
        // Regular expressions will look something like
        //    (@)(.*)(hall)(.*)(o)(.*)(wa)(.*)(n)(.*)(@)
        var template = [];
        template = prefixtemplate.concat(suffixtemplate.reverse());
        for (var slot in template) {
          template[slot] = "(" + template[slot] + ")";
        }
        var regex = new RegExp(template.join("(.*)"), "");
 
        this.debug("Considering regex ", regex);
        // Use the regular expression to find a guessed morphemes line
        var potentialParse = word
          .replace(regex, "$1-$2-$3-$4-$5-$6-$7-$8-$9") // Use backreferences to parse into morphemes
          .replace(/\$[0-9]/g, "") // Remove any backreferences that weren't used
          .replace(/@/g, "") // Remove the start/end-of-line symbol
          .replace(/--+/g, "-") // Ensure that there is only ever one "-" in a row
          .replace(/^-/, "") // Remove "-" at the start of the word
          .replace(/-$/, ""); // Remove "-" at the end of the word
        this.debug("Potential parse of " + word.replace(/@/g, "") + " is " + potentialParse);
 
        options.alternateMorphemeSegmentationsByWord[word].fullParses.push(potentialParse);
 
      }
      options.sort = Glosser.sortAlternatesByMoreSegmentation;
 
      Glosser.calculateAllAlternativeCombinations(options);
 
      // options.alternateMorphemeLines.push(fullParse);
 
      // options.alternateMorphemeLines.push(parsedWords.join(" "));
      options.morphemes = options.alternateMorphemeLines[0];
      return options;
    }
  },
 
  contextSensitiveGlossFinder: {
    value: function(fields, justCopyDontGuessIGT) {
      var morphemesLine = fields.morphemes;
      Iif (!morphemesLine) {
        return "";
      }
 
      if (justCopyDontGuessIGT || !this.lexicon) {
        var justQuestionMarks = morphemesLine.trim().replace(/[^ =-]+/g, "?");
        fields.gloss = justQuestionMarks;
      } else {
        fields = this.lexicon.guessContextSensitiveGlosses(fields);
      }
 
      return fields;
    }
  },
 
  // simplisticGlossFinder: {
  //   value: function(morphemesLine, justCopyDontGuessIGT) {
  //     if (!morphemesLine) {
  //       return "";
  //     }
 
  //     if (justCopyDontGuessIGT) {
  //       var justQuestionMarks = morphemesLine.trim().replace(/[^ -]+/g, "?");
  //       return justQuestionMarks;
  //     }
 
  //     if (!this.dbname) {
  //       if (!this.application || !this.application.get("corpus")) {
  //         throw "Glosser can't be guessed, there is no app so the URL must be defined.";
  //       }
  //       this.dbname = this.application.get("corpus").get("couchConnection").this.dbname;
  //     }
 
  //     var lexiconNodes = localStorage.getItem(this.dbname + "lexiconResults");
  //     if (!lexiconNodes) {
  //       return "";
  //     }
 
  //     lexiconNodes = JSON.parse(lexiconNodes);
 
  //     var glossGroups = [];
  //     var morphemeGroup = morphemesLine.split(/\s+/);
  //     for (var group in morphemeGroup) {
  //       var morphemes = morphemeGroup[group].split("-");
  //       var glosses = [];
  //       for (var m in morphemes) {
 
  //         var matchingNodes = [];
  //         var node = lexiconNodes[morphemes[m]];
  //         if (node) {
  //           matchingNodes.push(node);
  //         }
  //         node = lexiconNodes[morphemes[m].toLowerCase()];
  //         if (node) {
  //           matchingNodes.push(node);
  //         }
  //         var gloss = "?"; // If there's no matching gloss, use question marks
  //         if (matchingNodes.length > 0) {
  //           // Take the first gloss for this morpheme
  //           this.debug("Glosses which match: ", matchingNodes);
  //           try {
  //             gloss = matchingNodes[0][0].gloss;
  //           } catch (e) {
  //             this.debug(matchingNodes);
  //           }
  //         }
  //         glosses.push(gloss);
 
  //       }
 
  //       glossGroups.push(glosses.join("-"));
  //     }
 
  //     // Replace the gloss line with the guessed glosses
  //     return glossGroups.join(" ");
  //   }
  // },
 
  guessUtteranceFromMorphemes: {
    value: function(fields) {
      if (!fields.utterance && fields.morphemes) {
        fields.utterance = fields.morphemes.replace(/[-.]/g, "");
      }
      return fields;
    }
  },
 
  guessMorphemesFromUtterance: {
    value: function(fields, justCopyDontGuessIGT) {
      Iif (fields.morphemes) {
        return fields;
      }
      this.morphemefinder(fields, justCopyDontGuessIGT);
      Eif (!fields.gloss) {
        fields = this.contextSensitiveGlossFinder(fields, justCopyDontGuessIGT);
      }
      return fields;
    }
  },
  guessGlossFromMorphemes: {
    value: function(fields, justCopyDontGuessIGT) {
      Iif (fields.gloss) {
        return fields;
      }
      Iif (!fields.morphemes) {
        fields.morphemes = fields.utterance;
      }
      fields = this.contextSensitiveGlossFinder(fields, justCopyDontGuessIGT);
      return fields;
    }
  },
 
  // glossFinder: {
  //   value: function(morphemesLine) {
  //     //Guess a gloss
  //     var morphemeGroup = morphemesLine.split(/\s+/);
  //     var glossGroups = [];
  //     if (!this.lexicon) {
  //       var corpusSize = 31; //TODO get corpus size another way. // app.get("corpus").datalists.models[app.get("corpus").datalists.models.length-1].get("datumIds").length;
  //       if (corpusSize > 30 && !Glosser.toastedUserToSync) {
  //         Glosser.toastedUserToSync = true;
  //         this.popup("You probably have enough data to train an autoglosser for your corpus.\n\nIf you sync your data with the team server then editing the morphemes will automatically run the auto glosser.", "alert-success", "Sync to train your auto-glosser:");
  //       } else {
  //         Glosser.toastedUserToImport++;
  //         if (Glosser.toastedUserToImport % 10 === 1 && corpusSize < 30) {
  //           this.popup("You have roughly " + corpusSize + " datum saved in your pouch, if you have around 30 datum, then you have enough data to train an autoglosser for your corpus.", "alert-info", "AutoGlosser:");
  //         }
  //       }
  //     }
  //     for (var group in morphemeGroup) {
  //       var morphemes = morphemeGroup[group].split("-");
  //       var glosses = [];
  //       for (var m in morphemes) {
  //         if (!this.lexicon) {
  //           glosses.push("?");
  //           continue;
  //         }
  //         console.log("looking for morpheme " + morphemes[m]);
  //         // Take the first gloss for this morpheme
  //         var matchingNode = this.lexicon.find("morphemes", morphemes[m]);
  //         var gloss = "?"; // If there"s no matching gloss, use question marks
  //         this.warn(matchingNode);
  //         if (matchingNode.length) {
  //           gloss = matchingNode[0].gloss;
  //         }
  //         glosses.push(gloss);
  //       }
 
  //       glossGroups.push(glosses.join("-"));
  //     }
 
  //     // Replace the gloss line with the guessed glosses
  //     return glossGroups.join(" ");
  //   }
  // },
 
  /**
   * Takes as a parameters an array of self.morphemeSegmentationKnowledgeBase which came from CouchDB precedence rule query.
   * Example Rule: {"key":{"x":"@","relation":"precedes","y":"aqtu","context":"aqtu-nay-wa-n"},"value":2}
   */
  morphemeSegmentationKnowledgeBaseGraph: {
    get: function() {
      if (this._morphemeSegmentationKnowledgeBaseGraph) {
        return this._morphemeSegmentationKnowledgeBaseGraph;
      }
      if (!this.morphemeSegmentationKnowledgeBase) {
        return;
      }
      /*
       * Cycle through the precedence this.morphemeSegmentationKnowledgeBase, convert them into graph edges with the morpheme index in the morpheme array as the source/target values
       */
      var morphemeLinks = [];
      var morphemes = [];
      for (var i in this.morphemeSegmentationKnowledgeBase) {
        /* make the @ more like what a linguist recognizes for word boundaries */
        if (this.morphemeSegmentationKnowledgeBase[i].key.x === "@") {
          this.morphemeSegmentationKnowledgeBase[i].key.x = "#_";
        }
        if (this.morphemeSegmentationKnowledgeBase[i].key.y === "@") {
          this.morphemeSegmentationKnowledgeBase[i].key.y = "_#";
        }
        var xpos = morphemes.indexOf(this.morphemeSegmentationKnowledgeBase[i].key.x);
        if (xpos < 0) {
          morphemes.push(this.morphemeSegmentationKnowledgeBase[i].key.x);
          xpos = morphemes.length - 1;
        }
        var ypos = morphemes.indexOf(this.morphemeSegmentationKnowledgeBase[i].key.y);
        if (ypos < 0) {
          morphemes.push(this.morphemeSegmentationKnowledgeBase[i].key.y);
          ypos = morphemes.length - 1;
        }
        //To avoid loops?
        if (this.morphemeSegmentationKnowledgeBase[i].key.y.indexOf("@") === -1) {
          morphemeLinks.push({
            source: xpos,
            target: ypos,
            value: 1 //TODO use the context counting to get a weight measure
          });
        }
      }
 
      /*
       * Build the morphemes into nodes and color them by their morpheme length, could be a good measure of outliers
       */
      var morphemenodes = [];
      for (var m in morphemes) {
        morphemenodes.push({
          name: morphemes[m],
          length: morphemes[m].length
        });
      }
 
      /*
       * Create the JSON required by D3
       */
      this.morphemeSegmentationKnowledgeBaseGraph = {};
      this.morphemeSegmentationKnowledgeBaseGraph.links = morphemeLinks;
      this.morphemeSegmentationKnowledgeBaseGraph.nodes = morphemenodes;
 
      return this.morphemeSegmentationKnowledgeBaseGraph;
    }
  },
 
  saveAndInterConnectInApp: {
    value: function(callback) {
      if (typeof callback === "function") {
        callback();
      }
    }
  },
 
  visualizeMorphemesAsForceDirectedGraph: {
    value: function() {
      this.warn("visualizeMorphemesAsForceDirectedGraph is deprecated use visualizePrecedenceRelationsAsForceDirectedGraph instead");
    }
  },
 
  /*
   * Show precedes relations in a connected graph
   *
   * http://bl.ocks.org/mbostock/1153292
   * http://alignedleft.com/tutorials/d3/binding-data
   *
   * @param  {Object} element Obligatory element where the render should go
   * @return {Glosser}            returns itself for chaining calls
   */
  visualizePrecedenceRelationsAsForceDirectedGraph: {
    value: function(options) {
      options = options || {};
      if (!this.lexicon || !this.lexicon.length) {
        this.warn("Cannot visualize an empty lexicon.");
        return this;
      }
 
      Iif (!this.lexicon.connectedGraph && !this.lexicon.entryRelations && this.morphemeSegmentationKnowledgeBase) {
        this.lexicon.entryRelations = this.morphemeSegmentationKnowledgeBase;
      }
 
      this.lexicon.visualizeAsForceDirectedGraph(options);
 
      return this;
    }
  },
 
  render: {
    value: function(options) {
      return this.visualizePrecedenceRelationsAsForceDirectedGraph(options);
    }
  },
 
  isWithinConfidenceRange: {
    value: function(morpheme, confidenceRange) {
      if (!confidenceRange) {
        return true;
      }
      return morpheme.confidence <= confidenceRange.max && morpheme.confidence >= confidenceRange.min;
    }
  }
 
});
 
exports.Glosser = Glosser;