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