ChatBot(); //call constructor
// CONSTRUCTOR
function ChatBot(){
this.callbackHandler = this.Handler; //parameter variable initialized via properties or component inspector panel
this.callbackHandlerLocation = this._parent;
Key.addListener(this);
this.init();
}
// ===============================================
// ::: PUBLIC METHODS :::
// ===============================================
// GETTERS ***********
// SETTERS ***********
function setCallbackHandler(method, location){
this.callbackHandlerLocation = (location == undefined) ? this.callbackHandlerLocation: location;
this.callbackHandler = method;
}
// METHODS **********
function onKeyDown(){
if(Key.isDown(Key.ENTER)){
this.findAnswer(this.input_txt.text);
this.output_txt.scroll = this.output_txt.maxscroll;
this.input_txt.text = "";
Selection.setFocus(this.input_txt);
}
}
function say(the_str){
this.output_txt.text += the_str + newline;
}
function init(){
me = this;
this.my_xml = new XML();
this.my_xml.ignoreWhite = true;
this.my_xml.onLoad = function(success){
if(success){
var root_xml = this;
while(root_xml.nodeName == null){
root_xml = root_xml.firstChild;
}
//trace(root_xml);
me.dataObj = new Object();
me.createObject(me.dataObj, root_xml);
Selection.setFocus(this.input_txt);
}else{
this.output_txt = "Er is een fout opgetreden!";
}
}
this.my_xml.load(this.the_file);
}
// convert the XML database to native flash object database
function createObject(theObj, the_xml){
for(var i in the_xml.childNodes){
if(the_xml.childNodes[i].nodeValue == null){
var obj = theObj[the_xml.childNodes[i].nodeName] = new Object();
this.createObject(obj, the_xml.childNodes[i]);
}else{
theObj["text"] = the_xml.childNodes[i].nodeValue;
}
}
}
// search the database based on text entered
function findAnswer(the_str){
the_str = this.shaveChars(the_str);
var my_array = the_str.split(" ");
var firstFound = null;
my_array.push("text");
var pos = this.dataObj;
for(var i = 0; i < my_array.length; i++){
if(pos[my_array[i]] != undefined){
pos = pos[my_array[i]];
if(firstFound == null){
firstFound = my_array[i];
}
}
}
if(pos.length != undefined){
var text_array = pos.split("|");
for(var i = 0; i < text_array.length; i++){
var n = text_array[i].lastIndexOf(":");
if(n >= 0){
var x_str = text_array[i].substr(n+1);
this.execCallback(x_str);
}else{
this.output_txt.text += text_array[i] + newline;
}
}
}else{
if(firstFound == null){
this.output_txt.text += "Sorry, Ik snap niet wat u bedoeld!" + newline;
}else{
this.output_txt.text += "Ik weet het niet "+firstFound+" dat is." + newline;
}
}
}
// removes unwanted characters
function shaveChars(the_str){
var chars_array = new Array(".", "?", ",", "!", "'");
for(var i in chars_array){
var str_array = the_str.split(chars_array[i]);
the_str = str_array.join("");
}
return the_str;
}
// ===============================================
// ::: PRIVATE METHODS :::
// ===============================================
function execCallback(the_str){
trace("exec: "+the_str+" "+this.callbackHandler);
this.callbackHandlerLocation[this.callbackHandler](this, the_str);
}