various changes, bugfixes with goto

master
Michael Murtaugh 6 years ago
parent eee29453d9
commit 2612aab009

File diff suppressed because one or more lines are too long

@ -1,45 +1,4 @@
{
"quit": [],
"synon": [
"belief feel think believe wish",
"family mother mom father dad sister brother wife children child",
"desire want need",
"sad unhappy depressed sick",
"happy elated glad better",
"cannot can't",
"everyone everybody nobody noone",
"be am is are was"
],
"final": "Goodbye. Thank you for talking to me.",
"pre": [
"dont don't",
"cant can't",
"wont won't",
"recollect remember",
"dreamt dreamed",
"dreams dream",
"maybe perhaps",
"how what",
"when what",
"certainly yes",
"machine computer",
"computers computer",
"were was",
"you're you are",
"i'm i am",
"same alike"
],
"post": [
"am are",
"your my",
"me you",
"myself yourself",
"yourself myself",
"i you",
"you I",
"my your",
"i'm you are"
],
"initial": "How do you do. Please tell me your problem.",
"keywords": [
{
@ -562,13 +521,13 @@
"rules": [
{
"decomp": "* my *",
"save": true,
"reasmb": [
"Does that have anything to do with the fact that your (2) ?",
"Lets discuss further why your (2).",
"Earlier you said your (2).",
"But your (2).",
"Does that have anything to do with the fact that your (2) ?"
],
"save": true
"But your (2)."
]
},
{
"decomp": "* my* @family *",
@ -784,5 +743,46 @@
],
"token": "like"
}
]
],
"final": "Goodbye. Thank you for talking to me.",
"post": [
"am are",
"your my",
"me you",
"myself yourself",
"yourself myself",
"i you",
"you I",
"my your",
"i'm you are"
],
"synon": [
"belief feel think believe wish",
"family mother mom father dad sister brother wife children child",
"desire want need",
"sad unhappy depressed sick",
"happy elated glad better",
"cannot can't",
"everyone everybody nobody noone",
"be am is are was"
],
"pre": [
"dont don't",
"cant can't",
"wont won't",
"recollect remember",
"dreamt dreamed",
"dreams dream",
"maybe perhaps",
"how what",
"when what",
"certainly yes",
"machine computer",
"computers computer",
"were was",
"you're you are",
"i'm i am",
"same alike"
],
"quit": []
}

@ -261,10 +261,10 @@ key: no
reasmb: Why 'no' ?
key: my 2
decomp: $ * my *
reasmb: Does that have anything to do with the fact that your (2) ?
reasmb: Lets discuss further why your (2).
reasmb: Earlier you said your (2).
reasmb: But your (2).
reasmb: Does that have anything to do with the fact that your (2) ?
decomp: * my* @family *
reasmb: Tell me more about your family.
reasmb: Who else in your family (4) ?

@ -52,13 +52,32 @@
<script src="eliza.js"></script>
<script>
function eliza (rules) {
chatbot({
rules: rules,
input: document.getElementById("textinput"),
output: document.getElementById("display"),
debug: false,
autoscroll: true
var input = document.getElementById("textinput"),
output = document.getElementById("display"),
bot = chatbot(rules, true);
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");
display.scrollTop = display.scrollTopMax;
}
input.addEventListener("keypress", function (event) {
if (event.keyCode == 13) {
var text = input.value;
log(text, "user");
say(bot(text), "bot");
input.value = "";
input.focus();
}
});
say(rules.initial);
}
</script>
<script src="doctor.js"></script>

@ -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;
}
Loading…
Cancel
Save