// joinTryToVerbs.js
(function () {
/* global adventurejs A */
var p = adventurejs.Parser.prototype;
/**
* <p>
* Search input for verb phrases in the format of
* "try to verb".
* We compare the input string against entries in
* dictionary.try_to_verbs,
* which was populated automatically at initialization.
* If we find a set of words that match an entry,
* we save a record for use with future verb logic.
* </p>
* <p>
* For example:<br>
* <code class="property">"try to swim"</code>
* is identified as
* <code class="property">"swim"</code>
* </p>
* @memberOf adventurejs.Parser
* @method adventurejs.Parser#joinTryToVerbs
* @param {String} input Player input.
* @returns {String}
*/
p.joinTryToVerbs = function Parser_joinTryToVerbs(input) {
this.game.log(
"L1631",
"log",
"high",
`[joinTryToVerbs.js] joinTryToVerbs() receive: ${input}`,
"Parser"
);
for (var i = 0; i < this.dictionary.try_to_verbs.length; i++) {
var verb_phrase = this.dictionary.try_to_verbs[i][0];
var verb_id = this.dictionary.try_to_verbs[i][1];
var verb = verb_phrase.split(" ")[0];
// search anywhere in string
//var search = "\\b"+verb_phrase+"\\b";
// only search beginning of string
var search = "^(\\b" + verb_phrase + "\\b)";
var regex = new RegExp(search, "g");
var replaced = input.replace(regex, verb_id);
if (replaced !== input) {
// save a record of the original input
this.input_history[0].tokens[verb_id] = {
key: verb_id,
source: verb_phrase,
};
this.input_history[0].input_verb = verb_id;
this.input_history[0].verb_phrase = verb_phrase;
this.input_history[0].verb_phrasal_pattern = "try_to_verb";
// this version replaces the verb with the verb phrase
input = replaced;
// this version replaces the verb with the verb id
// if (verb !== verb_id) {
// input = input.replace(verb, verb_id);
// }
break;
}
}
this.game.log(
"L1632",
"log",
"high",
`[joinTryToVerbs.js] joinTryToVerbs() return:\n${input}`,
"Parser"
);
return input;
};
})();