Last active
December 16, 2015 02:29
-
-
Save vihanb/d2cf4e892cea3134ab98 to your computer and use it in GitHub Desktop.
A bot which learns english through conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The Chatbot run on | |
// @Upgoat (StackExchange) | |
// http://codegolf.stackexchange.com/users/47996 | |
// This Chatbot is designed for learning how to have a conversation | |
// It does this by having conversations with you, taking alternating parts | |
// Bascically through enough supervised learning it should be able to talk | |
function Speak(out,m) {document.getElementById("input").value=(m?":"+m.id.split("-")[1]+" ":"")+out;document.getElementById("sayit-button").click()} | |
Array.prototype.random = function() { return this.length ? this[Math.floor(Math.random() * this.length)] : "<broken goat/no items>" }; | |
//console.log = (function(old){return function(){Speak.apply(this, arguments);old.apply(console, arguments)}}(console.log)); | |
// Config | |
var delay = 500; // 2000 might possible work better | |
var latestart = ""; | |
var NAME = "Chatgoat"; // Chatbot name, modifiable | |
var TAG = `<${NAME}> `; | |
var START= latestart || TAG+`My name is ${NAME}, Talk to me and I'll try to have a meaningful conversation with you` | |
if (!latestart) Speak(START); | |
var called = []; | |
// VERY important | |
var DATA = { | |
"conv": { // Conversations | |
/* | |
"Downgoat": { // User | |
"cs": [ // User Conversations | |
{ | |
id: 0, // Conversation last ID | |
ct: 0, // Conversation items | |
speak: [], // Test | |
gen: [], // Generated responses | |
user: true | |
} | |
] | |
} | |
*/ | |
}, | |
learned: [ // All the learned phrases | |
["hello"] | |
], | |
words: { // Word frequency list | |
"hello": 1 | |
}, | |
"punc": [".!\u0003"], | |
}; | |
var conv_temp = { id: 0, ct: 0, speak: [], gen: [], user: true }; | |
window.commands = {}; | |
function Bare(words){return words.toLowerCase().match(/[A-Za-z]+/g) || []} | |
function GetFrequency() { | |
DATA.words = [].concat.apply([], DATA.learned).reduce(function(obj, item) { | |
if (obj[item]) obj[item] = 1; | |
else obj[item]++; | |
}, {}); | |
return DATA.words; | |
} | |
function GenerateSentence(conv) { | |
var CV = DATA.learned[conv.ct]; | |
return CV || "<Error / Could not generate response>"; | |
} | |
var play = function play() { | |
var help = { | |
"/done": "Kills chatgoat retuning a conversation data dump", | |
"/start": "Starts a new conversation", | |
"/end": "Returns a dump of the conversation" | |
}; | |
var alive = true; | |
window.commands = $.extend(window.commands, { | |
"help": function () { return Object.keys(help).map(function(item) { return TAG+item + ": " + help[item] }).join("\n") }, | |
"done": function () { return Speak(JSON.stringify(DATA, null, 2)) }, | |
"start":function (t,u,i) { | |
if (DATA.conv[u]) { // User exists | |
DATA.conv[u].cs.unshift(conv_temp); return "Done" | |
DATA.conv[u].cs[0].user = !DATA.conv[u].cs[1].user; | |
} else { // Make user | |
DATA.conv[u] = { cs: [conv_temp] }; | |
} | |
if (!DATA.conv[u].cs[0].user) { | |
var CV = DATA.conv[u].cs, | |
IT = CV[0]; | |
Speak( GenerateSentence(IT) ); | |
} | |
Speak("Started, when talking, let Chatgoat respond before you post another message"); | |
}, | |
"end": function(t,u) { Speak(JSON.stringify(DATA.conv[u].cs[DATA.conv[u].cs.length - 1])) } | |
}); | |
var messages = Array.from(document.querySelectorAll(".message")).filter(function (message, i, all) { | |
var id = message.getAttribute("id").split("-")[1]; | |
if (~called.indexOf(id)) return false; | |
else called.push(id); | |
return i > all.map(function (q) { return q.textContent.trim() }).lastIndexOf(START) && | |
message.parentNode.parentNode.querySelector(".username").textContent.trim() !== "Upgoat"; | |
}); | |
messages.forEach(function (message) { | |
var text = $(message.querySelector(".content")).text().trim(); | |
var user = message.parentNode.parentNode.querySelector(".username").textContent.trim(); | |
var ID = message.getAttribute("id").split("-")[1]; | |
var link = ":" + ID + " "; | |
if (/^\/[a-z]+$/.test(text)) { | |
window.commands[text.slice(1)](text, user, ID); | |
// Speak("Done"); | |
} else { | |
if (DATA.conv[user]) { | |
DATA.conv[user].cs[0].speak.push(text); // Add conversation response | |
DATA.conv[user].cs[0].id = ID; // Set ID | |
var count = DATA.conv[user].cs[0].ct++; | |
DATA.learned[count]; | |
Bare(text).map(function(item) { !~DATA.learned[count].indexOf(item) && DATA.learned[count].push(item) }); | |
} else { | |
Speak(link + "You haven't started a conversation yet. Start one with /start. Conversations are needed so I can know who I'm talking to."); | |
} | |
} | |
Object.keys(DATA.conv).map(function(username) { | |
return DATA.conv[username].cs[0] | |
}).filter(function (conversation) { | |
return !conversation.user; | |
}); | |
setTimeout("", 2100); | |
}); | |
if (alive) setTimeout(play, delay); // Avoids zombie Chatgoat | |
}; | |
play(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment