|
|
|
@ -129,8 +129,10 @@ function chatbot (opts) {
|
|
|
|
|
|
|
|
|
|
function match_decomp (pattern, tokens) {
|
|
|
|
|
var ppat = compile_pattern(pattern);
|
|
|
|
|
// console.log("compile_pattern.in", pattern);
|
|
|
|
|
// console.log("compile_pattern.out", ppat);
|
|
|
|
|
if (debug) {
|
|
|
|
|
console.log("compile_pattern.in", pattern);
|
|
|
|
|
console.log("compile_pattern.out", ppat);
|
|
|
|
|
}
|
|
|
|
|
var ppat = new RegExp(ppat, "i");
|
|
|
|
|
return ppat.exec(tokens.join(" "));
|
|
|
|
|
}
|
|
|
|
@ -144,9 +146,7 @@ function chatbot (opts) {
|
|
|
|
|
function do_reasmb (reasmb, match, tokens) {
|
|
|
|
|
if (Array.isArray(match)) {
|
|
|
|
|
return reasmb.replace(/\((\d+)\)/, function (m, n) {
|
|
|
|
|
// console.log("replace", m, n);
|
|
|
|
|
var ret = match[parseInt(n)];
|
|
|
|
|
return do_post(ret); // apply POST substitutions here to matching input
|
|
|
|
|
return do_post(match[parseInt(n)]); // apply POST substitutions here to matching input
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
return reasmb;
|
|
|
|
@ -157,27 +157,27 @@ function chatbot (opts) {
|
|
|
|
|
for (var i=0, l=keywords.length; i<l; i++) {
|
|
|
|
|
var keyword = keywords[i];
|
|
|
|
|
if ((keyword.token == "xnone") && (saved_statements.length > 0)) {
|
|
|
|
|
console.log("using saved statement");
|
|
|
|
|
if (debug) { console.log("using saved statement"); }
|
|
|
|
|
return saved_statements.shift();
|
|
|
|
|
}
|
|
|
|
|
while (true) {
|
|
|
|
|
console.log("trying keyword", keyword.token);
|
|
|
|
|
if (debug) { console.log("trying keyword", keyword.token); }
|
|
|
|
|
for (var ri=0, rl = keyword.rules.length; ri<rl; ri++) {
|
|
|
|
|
var rule = keyword.rules[ri];
|
|
|
|
|
console.log("trying rule", rule.decomp, "("+(ri+1)+"/"+rl+")");
|
|
|
|
|
if (debug) { console.log("trying rule", rule.decomp, "("+(ri+1)+"/"+rl+")"); }
|
|
|
|
|
var match = match_decomp(rule.decomp, tokens);
|
|
|
|
|
if (match) {
|
|
|
|
|
var ra = rule.reasmb();
|
|
|
|
|
if (rule['save']) {
|
|
|
|
|
var save = do_reasmb(ra, match, tokens);
|
|
|
|
|
console.log("save", save);
|
|
|
|
|
if (debug) { console.log("save", save); }
|
|
|
|
|
saved_statements.push(save);
|
|
|
|
|
} else if (ra.indexOf("goto ") == 0) {
|
|
|
|
|
var goto_name = ra.substr(5);
|
|
|
|
|
console.log("goto", goto_name);
|
|
|
|
|
if (debug) { console.log("goto", goto_name); }
|
|
|
|
|
keyword = rules.keywords_by_token[goto_name]
|
|
|
|
|
} else {
|
|
|
|
|
console.log("match", match, ra);
|
|
|
|
|
if (debug) { console.log("match", match, ra); }
|
|
|
|
|
return do_reasmb(ra, match, tokens);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|