我有一个机器人可以帮助网站上的版主,我想添加人工智能。这是基于Markov Chains。问题是我希望机器人记录所有数据字符串,然后在马尔可夫链中使用。本质上,机器人正在从用户交谈中收集的聊天数据中学习。
我现在所拥有的是:
var fs = require('fs');
var logit = fs.createWriteStream('Data.js', {'flags': 'a'});
chatEvent(function(data) {
data.message = stringFix(data.message); //just fixes punctuation
a = JSON.stringify(data.message);
logit.write(a + ',');
});
这会创建一个消息列表(代码段),如下所示:
"Shall I play Ellie Goulding - Hanging On (Sound Remedy Remix)",
"Or Mango - Sayonara",
"mmn mangos",
"What should I choose",
"only because you're thinking food",
"how can you expect a woman to be happy if a man persists in treating her like a perfectly normal human being",
我希望它能做到这一点:
module.exports = {
data: [
"Shall I play Ellie Goulding - Hanging On (Sound Remedy Remix)",
"Or Mango - Sayonara",
"mmn mangos",
"What should I choose",
"only because you're thinking food",
"how can you expect a woman to be happy if a man persists in treating her like a perfectly normal human being",
]
}
以便所有消息都保存在数据中:[]数组。这样我就可以通过执行以下操作来调用静态视图数据:
Data = require('./Data').data;
sentences = [];
sentences = Data;
有没有办法做到这一点?也可以从Data.js文件中直播,以便在将它发送到文件时将所有新消息添加到句子数组中,就像我的机器人不断学习一样。如果不是我可以设置它,以便所有消息都保存到data.js文件并推送到句子文件,这样,如果僵尸程序崩溃/重新连接,它可以从data.js文件重新加载数据和从它停止的地方起飞。
答案 0 :(得分:0)
您可以“初始化”js文件,然后插入数据 实现可以是这样的。
var fs = require("fs");
var fcontent = 'module.exports = {data: []}';
fs.writeFile('Data.js', fcontent, function (err) {
var offset= 25;
var logit = fs.createWriteStream('Data.js', {flags: 'r+',start:offset});
function beforeRead(cbk){
logit.write("]};");
cbk();
}
function message(msg){
logit.write( (offset==25) ? ('"'+msg+'"') : (',"'+msg+'"') );
offset+=msg.length;
}
//chatEvent
message("I'm a bot");
message("I can't hurt you");
beforeRead(function(){
//here we have a correct syntax js file
});
});
即使您可以在if中注入初始写入,但它会失去可读性。谨慎行事,这可能是危险的(scape字符串,捕获错误,......)