html + opts

master
Michael Murtaugh 6 years ago
parent 371be47195
commit 60c699bae4

@ -0,0 +1,66 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<style type="text/css">
#wrapper {
display: flex;
flex-direction: column;
position: absolute;
left: 5%; top: 5%; right: 5%; bottom: 5%;
}
#textinput {
width: 100%;
border: none;
background: none;
}
#input {
padding: 2%;
flex: 0 1 auto;
background: aqua;
}
#display {
flex: 1 1 auto;
background: pink;
padding: 2%;
overflow: auto;
}
#display div.msg {
padding-bottom: 0.5em;
}
#display div.human {
color: black;
}
#display div.bot {
color: black;
text-transform: uppercase;
}
#display div.debug {
color: red;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="display">
</div>
<div id="input">
<input type="text" id="textinput" autofocus />
</div>
</div>
<script src="eliza.js"></script>
<script>
function eliza (rules) {
chatbot({
rules: rules,
input: document.getElementById("textinput"),
output: document.getElementById("display"),
debug: true,
autoscroll: true
});
}
</script>
<script src="doctor.js"></script>
</body>
</html>

@ -7,7 +7,12 @@ 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 Use the accompanying eliza_script_to_json.py to prepare the rules in JSON format
*/ */
function chatbot (rules, textarea, display, debug) { function chatbot (opts) {
var rules = opts.rules,
textarea = opts.input,
display = opts.output,
debug = opts.debug || false,
autoscroll = opts.autoscroll === undefined ? true : opts.autoscroll;
var saved_statements = []; var saved_statements = [];
function process_rules (rules) { function process_rules (rules) {
function looping_iterator (l) { function looping_iterator (l) {
@ -153,7 +158,7 @@ function chatbot (rules, textarea, display, debug) {
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"); console.log("using saved statement");
return saved_statements.pop(); return saved_statements.shift();
} }
while (true) { while (true) {
console.log("trying keyword", keyword.token); console.log("trying keyword", keyword.token);
@ -183,13 +188,16 @@ function chatbot (rules, textarea, display, debug) {
function log (msg, kls) { function log (msg, kls) {
var d = document.createElement("div"); var d = document.createElement("div");
d.setAttribute("class", kls); d.setAttribute("class", "msg " + kls);
d.innerHTML = msg; d.innerHTML = msg;
display.appendChild(d); display.appendChild(d);
} }
function say (msg) { function say (msg) {
log(msg, "bot"); log(msg, "bot");
if (autoscroll) {
display.scrollTop = display.scrollTopMax;
}
} }
function process (text) { function process (text) {

Loading…
Cancel
Save