universal_bot
Version:
Taking the principles of aiml - but allowing a dynamic and extendable *.js environment
347 lines (225 loc) • 6.59 kB
JavaScript
var convert = require('xml-js');
var fs = require('fs');
/*
var xml =
'<?xml version="1.0" encoding="utf-8"?>' +
'<note importance="high" logged="true">' +
' <title>Happy</title>' +
' <todo>Work</todo>' +
' <todo>Play</todo>' +
'</note>';
var res = convert.xml2json(xml, {compact: true, spaces: 4});
*/
function get_template ( tmp ) {
var tlist = [];
var li = tmp.li;
for (var m = 0; m < li.length; m++ ) {
tlist.push( li[m]._text );
}
return tlist;
}
function check_template ( template) {
if (Array.isArray( template )) {
}
}
function condition ( entry ) {
var clist = [];
var ln = entry.li.length;
var param = entry._attributes.name;
var o = { condition: true };
for ( var n = 0; n < ln; n++) {
var item = entry.li[n];
// console.log( item );
var ctmp = {};
ctmp[param] = null;
var attr = item._attributes;
if (attr) ctmp[param] = attr.value;
var tmp = null;
if (item.random) {
ctmp.template = get_template( item.random );
// ctmp.template = check_templkate( ctmp.template);
} else {
if ( Array.isArray( item._text ) === true ) item._text = remove_stars( item._text );
ctmp.template = item._text;
// console.log ( ctmp );
}
clist.push( ctmp );
}
return clist;
}
function remove_stars( txt) {
var s = "";
for (var q = 0; q < txt.length; q++) {
s += txt[q];
if (q !== txt.length -1) s += "*";
}
return s;
}
function thinker ( think) {
var type = think._attributes.name;
// console.log("TYP ist " + type );
var value = null;
if ( think.star) {
console.log("STAR");
if ( think.star._attributes) {
switch( think.star._attributes.index ) {
case '1':
value = "*";
break;
case '2':
value = "**";
break;
case '3':
value = "***";
break;
default:
value = "*";
break;
}
}
console.log(think.star );
}
else value = think._text;
var o = {};
o[type] = value;
console.log("THINKER");
console.log( o );
return o;
}
function get_think ( think ){
console.log( think );
if (Array.isArray( think.set )) {
var s = [];
for (var n = 0; n < think.set.length; n++) {
var item = think.set[n];
console.log( item );
s.push ( thinker( item ) );
}
console.log (s );
return s;
}
else
{
console.log( think._set );
if ( think._set ) {
var type = think.set._attributes.name;
var value = think.set._text;
// console.log( type );
// console.log( value );
var o = {};
o[type] = value;
return o;
}
}
}
function remove_empty_types ( obj ) {
for (key in obj) {
if (obj[key] === null) delete ( obj[key] );
}
}
function get_var ( item ) {
console.log( item );
console.log( item.get);
var s = "";
for (key in item) {
if (key === "_text") {
console.log("KEY");
var t = item._text;
if (Array.isArray(t ) === true ) {
for (var n = 0; n < t.length; n++) {
s += t[n];
}
}
else s += t;
}
if (key === "get") {
var g = item.get;
console.log("GET");
if (Array.isArray(g ) === true ) {
for (var n = 0; n < g.length; n++) {
// console.log(g[n]);
var gg = g[n]._attributes.name;
s += ' {' + gg + '} ';
}
}
else
{
var gg = g._attributes.name;
s += ' {' + gg + '} ';
}
}
}
s = replace_break(s);
return s;
}
function replace_break( str ){
return str.replace(/(\r\n|\n|\r)/gm,"");
}
var out = "out/grammar.json";
var topic = "grammar";
fs.readFile('./aiml/AIML/grammar.aiml', 'utf8', function read(err, data) {
if (err) {
console.log("Problem");
throw err;
}
if (data) console.log("Daten sind da");
content = data;
var result = convert.xml2json(content, {compact: true, spaces: 4});
var s = JSON.parse( result );
var list = s.aiml.category;
var converted = [];
for (var i = 0; i < list.length; i++) {
var item = list[i];
var pattern = item.pattern._text;
var template = item.template;
var tpl;
if (template.random) {
var t = template.random.li;
tpl = [];
for (var n = 0; n < t.length; n ++) {
if (Array.isArray( t[n]._text ) === true ) t[n]._text = remove_stars( t[n]._text );
tpl.push ( t[n]._text );
}
// console.log( tpl );
}
else
{
if ( template.get) tpl = get_var( template);
else tpl = template._text;
}
var srai = null;
if ( template.srai) srai = template.srai._text;
var think = null;
if (template.think) think = get_think ( template.think );
if (template.condition) {
var o = condition( template.condition );
tpl = o;
var o = {
condition: true,
topic: topic,
pattern: pattern,
template: tpl,
srai: srai,
think: think
};
remove_empty_types( o );
if ( item.that ) o.that = item.that._text;
converted.push( o );
}
else
{
var o = {
pattern: pattern,
topic: topic,
template: tpl,
srai: srai,
think: think
};
if ( item.that ) o.that = item.that._text;
remove_empty_types( o );
converted.push( o );
}
var s = JSON.stringify( converted, null, 4);
fs.writeFile(out, s);
}
});