|
|
|
@ -7,14 +7,11 @@ See LICENSE.txt and http://artlibre.org/licence/lal/en/
|
|
|
|
|
Use the accompanying eliza_script_to_json.py to prepare the rules in JSON format
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
function chatbot (opts) {
|
|
|
|
|
var rules = opts.rules,
|
|
|
|
|
textarea = opts.input,
|
|
|
|
|
display = opts.output,
|
|
|
|
|
debug = opts.debug || false,
|
|
|
|
|
autoscroll = opts.autoscroll === undefined ? true : opts.autoscroll;
|
|
|
|
|
function chatbot (rules, debug) {
|
|
|
|
|
var saved_statements = [];
|
|
|
|
|
|
|
|
|
|
function process_rules (rules) {
|
|
|
|
|
// transfrom / pre-process the rules
|
|
|
|
|
function looping_iterator (l) {
|
|
|
|
|
var next = function () {
|
|
|
|
|
var ret = l[next.i];
|
|
|
|
@ -48,9 +45,7 @@ function chatbot (opts) {
|
|
|
|
|
// index keywords by name
|
|
|
|
|
rules.keywords_by_token = {};
|
|
|
|
|
rules.keywords.forEach(function (x) {
|
|
|
|
|
// console.log("token", x.token);
|
|
|
|
|
rules.keywords_by_token[x.token] = x;
|
|
|
|
|
// wrap the rules
|
|
|
|
|
x.rules.forEach(function (r) {
|
|
|
|
|
// ensure list
|
|
|
|
|
if (!Array.isArray(r.reasmb)) { r.reasmb = [r.reasmb]; }
|
|
|
|
@ -58,7 +53,6 @@ function chatbot (opts) {
|
|
|
|
|
r.reasmb = looping_iterator(r.reasmb);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
// console.log("keywords_by_token", rules.keywords_by_token)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// function trim (text) { return text.replace(/\s+$/, "").replace(/\s+/, ""); }
|
|
|
|
@ -160,7 +154,9 @@ function chatbot (opts) {
|
|
|
|
|
if (debug) { console.log("using saved statement"); }
|
|
|
|
|
return saved_statements.shift();
|
|
|
|
|
}
|
|
|
|
|
while (true) {
|
|
|
|
|
var loop = true;
|
|
|
|
|
while (loop) {
|
|
|
|
|
loop = false;
|
|
|
|
|
if (debug) { console.log("trying keyword", keyword.token); }
|
|
|
|
|
for (var ri=0, rl = keyword.rules.length; ri<rl; ri++) {
|
|
|
|
|
var rule = keyword.rules[ri];
|
|
|
|
@ -175,7 +171,9 @@ function chatbot (opts) {
|
|
|
|
|
} else if (ra.indexOf("goto ") == 0) {
|
|
|
|
|
var goto_name = ra.substr(5);
|
|
|
|
|
if (debug) { console.log("goto", goto_name); }
|
|
|
|
|
keyword = rules.keywords_by_token[goto_name]
|
|
|
|
|
keyword = rules.keywords_by_token[goto_name];
|
|
|
|
|
loop = true;
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
if (debug) { console.log("match", match, ra); }
|
|
|
|
|
return do_reasmb(ra, match, tokens);
|
|
|
|
@ -186,20 +184,6 @@ function chatbot (opts) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function log (msg, kls) {
|
|
|
|
|
var d = document.createElement("div");
|
|
|
|
|
d.setAttribute("class", "msg " + kls);
|
|
|
|
|
d.innerHTML = msg;
|
|
|
|
|
display.appendChild(d);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function say (msg) {
|
|
|
|
|
log(msg, "bot");
|
|
|
|
|
if (autoscroll) {
|
|
|
|
|
display.scrollTop = display.scrollTopMax;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function process (text) {
|
|
|
|
|
if (debug) { console.log("input", text); }
|
|
|
|
|
var tokens = tokenize(text);
|
|
|
|
@ -210,18 +194,9 @@ function chatbot (opts) {
|
|
|
|
|
if (debug) { console.log("keywords", keywords.map(function (x) { return x.token })); }
|
|
|
|
|
var output = apply_keywords(keywords, tokens);
|
|
|
|
|
if (debug) { console.log("output", output); }
|
|
|
|
|
say(output);
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process_rules(rules);
|
|
|
|
|
textarea.addEventListener("keypress", function (event) {
|
|
|
|
|
if (event.keyCode == 13) {
|
|
|
|
|
var text = textarea.value;
|
|
|
|
|
log(text, "user");
|
|
|
|
|
process(text);
|
|
|
|
|
textarea.value = "";
|
|
|
|
|
textarea.focus();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
say(rules.initial);
|
|
|
|
|
return process;
|
|
|
|
|
}
|