Pre-release
Adventure.js Docs Downloads
Score: 0 Moves: 0
// joinVerbNounPrepNouns.js

(function () {
  /*global adventurejs A*/
  "use strict";

  var p = adventurejs.Parser.prototype;

  /**
   * <p>
   * Search input for compound verb phrases in the format of
   * "verb noun preposition noun",
   * by comparing the input string against entries in
   * dictionary.verb_noun_prep_nouns,
   * which was populated by Verb definitions.
   * If we find a set of words that match an entry,
   * we compress the words into a single string
   * that is a Verb name.
   * </p>
   * <p>
   * This is a holdover from an earlier parsing technique
   * that is not much used now. (This particular example
   * is no longer valid.)
   * <br>
   * <code class="property">"ask grocer about eggplant"</code>
   * becomes
   * <code class="property">"ask_about grocer eggplant"</code>
   * </p>
   * @memberOf adventurejs.Parser
   * @method adventurejs.Parser#joinVerbNounPrepNouns
   * @param {String} input Player input.
   * @returns {String}
   */
  p.joinVerbNounPrepNouns = function Parser_joinVerbNounPrepNouns(input) {
    for (var i = 0; i < this.dictionary.verb_noun_prep_nouns.length; i++) {
      var pair = this.dictionary.verb_noun_prep_nouns[i][0];
      var verb = pair.split(" ")[0];
      var prep = pair.split(" ")[1];
      var combined = this.dictionary.verb_noun_prep_nouns[i][1];

      /**
       * Valid chars are alphanumeric, plus:
       * " " spaces, used as delimeters
       * "_" underscores, used to serialize compound names
       * "," commas, stripped from input but then used with multiple search results
       * "=" used with multiple search results
       * "&" used with multiple search results
       */
      var search = verb + " ([,_=&A-Za-z0-9]*) " + prep;

      var regex = new RegExp(search, "g");
      var replaced = input.replace(regex, combined + " $1");
      if (replaced !== input) {
        // save a record of the original input
        this.input_history[0].input_verb = pair;
        this.input_history[0].joint = "verb_noun_prep_noun";
        input = replaced;
        break;
      }
    }

    this.game.log(
      "log",
      "high",
      "parseInput.js > joinVerbNounPrepNouns returns: " + input,
      "Parser",
    );
    return input;
  };
})();