Pre-release
AdventureJS Docs Downloads
Score: 0 Moves: 0
// tokenizeBoundaries.js

(function () {
  /* global AdventureJS A */

  var p = AdventureJS.Parser.prototype;

  /**
   * Convert periods used as sentence boundaries to pipes
   * and insert a space to help parsing compound words.
   * The inciting example is:
   * "take all but glass key."
   * which was being parsed as
   * "take all -glass key."
   * instead of
   * "take all -glass_key."
   * @memberOf AdventureJS.Parser
   * @method AdventureJS.Parser#tokenizeBoundaries
   * @param {String} input
   * @returns {String}
   */
  p.tokenizeBoundaries = function Parser_tokenizeBoundaries(input) {
    this.game.log(
      "L1704",
      "log",
      "high",
      `[tokenizeBoundaries.js] tokenizeBoundaries() receive: ${input}`,
      "Parser"
    );

    let count = 0;
    input = input.replace(/\.(?=\s|$)/g, (match, offset, string) => {
      count++;
      const before = string.slice(0, offset);
      const word = before.match(/[\w.]+$/)?.[0] ?? "";
      const wordp = `${word}.`;
      const after = string.slice(offset + 1);
      const next_word = after.match(/^\s*([\w.]+)/)?.[1] ?? "";
      const next_word_is_verb =
        next_word && this.game.dictionary.getVerb(next_word);
      // const is_first_word =

      console.warn(`tokenizeBoundaries`, { wordp, next_word });
      if (
        "undefined" !== typeof this.game.world_lookup[wordp] ||
        this.game.dictionary.getAbbreviation(wordp)
      ) {
        if (next_word_is_verb) {
          console.warn(`this is word`, count);
          console.warn(`next_word_is_verb`, next_word);
          // is next word a verb? if so, though we found
          // a word with this period in the lookup, it's
          // probably also a sentence boundary, because
          // the player wouldn't use two periods. example:
          // "talk to dr. go east."

          // @TODO this seems like it might need more nuance

          // this next bit shouldn't happen now that
          // tokenizeCommands is pre-empting this situation
          // but leaving this here until we sort out nuance

          // is this word a character in first position?
          // if (count === 1) {
          //   let asset;
          //   let lookup = this.game.world_lookup[wordp];
          //   console.warn({ lookup });
          //   if (lookup && lookup.IDs.length === 1) {
          //     asset = this.game.getAsset(lookup.IDs[0]);
          //   }
          //   console.warn({ asset });
          //   if (asset && asset.hasClass("Character")) {
          //     console.warn(`CHARACTER WITH . IN NAME BEFORE VERB`);
          //   }
          // }

          console.warn(`${wordp} return ". |"`);
          return ". |";
        }
        // otherwise just return the original period
        console.warn(`${wordp} return "."`);
        return ".";
      }

      // if a word with this period isn't found in the lookup,
      // then it must be a sentence boundary
      console.warn(`${word} return " |"`);
      return " |";
    });

    if (input.endsWith("|")) input = input.slice(0, -1).trim();

    this.game.log(
      "L1705",
      "log",
      "high",
      `[tokenizeBoundaries.js] tokenizeBoundaries() return: ${input}`,
      "Parser"
    );
    return input;
  };
})();