//////////////////////////////////////////////// ////////////////PROGRAM INFO//////////////////// // "Kon" is a "learning bot" written in C#. // // The bot will connect to an IRC server and // // "watch" the conversations. It will then // // "learn" from them. You'll then be able to // // talk to it and hopefully it'll hold a conv-// // ersation. // //////////////////////////////////////////////// // The "LB Brain" was based on a chatterbot by// // BishounenNightBird of Esper.net called "LB"// // ("Language Bot"). While this brain does // // not follow LB exactly, he still inspired me// // and I want to thank him. // //////////////////////////////////////////////// // "Kon" is an ongoing project started by // // James Iyouboushi. // // Emails: jmp1139@my.gulfcoast.edu // // Iyouboushi@gmail.com // //////////////////////////////////////////////// // This file was last updated on: 2/18/2008 // //////////////////////////////////////////////// using System; using System.Collections; using System.Text; using System.IO; using System.Threading; namespace Kon { class LBbrain { #region variables public static StreamWriter brain; private const string BRAIN_FILE = "lb.brain"; private Random randnum = new Random(); private string lastLine = ""; private string previousSearch = ""; #endregion public LBbrain() { } #region Add To Brain public void addToBrain(string conversation, string nick) { // These are never going to change and can be filtered here. if ((conversation.StartsWith("http://") || (conversation.StartsWith("www.")))) { // We don't want to add URLs. return; } if (conversation.StartsWith("!")) return; if (conversation.StartsWith("")) return; if (conversation.StartsWith("[")) return; if (conversation.StartsWith("<")) return; // We don't want to capture stuff that's supposed to be for or from LB either. if (conversation.StartsWith("lb:")) return; if (nick.ToUpper() == "LB4") return; if (nick.ToUpper() == "LB5") return; // we don't want stuff that's too short. if (conversation.Length < 5) return; conversation = conversation.Replace("Kon", "UNNAMED_USER"); // "unnamed user" will later become a nickname conversation = conversation.Replace("kon", "UNNAMED_USER"); // "unnamed user" will later become a nickname conversation = conversation.Replace("KON", "UNNAMED_USER"); // "unnamed user" will later become a nickname // Time to filter out some stuff. Filter filter = new Filter(); // filter emoticons conversation = filter.replaceWithCustom_emoticons(conversation); // filter image extensions. conversation = filter.replaceWithNull_ImageExt(conversation); // filter out some things that I personally don't want in it. conversation = filter.replace_custom(conversation); // filter out the HTML and web-based stuff conversation = filter.replaceWithNull_HTML(conversation); conversation = filter.replaceWithNull_webbased(conversation); // Finally, we don't want the bot to have a bad potty mouth. conversation = filter.replaceWithNull_swears(conversation); // Tidy up some stuff. conversation = conversation.Trim(); // Well, that's good enough for now. Let's make sure it's not null and add it to the brain! if (conversation != "") { brain = new StreamWriter(BRAIN_FILE, true); string[] messageArray; messageArray = conversation.Split(new char[] { ' ' }); int lastWordPos = (messageArray.Length-1); int j = 1; for (int i = 0; i < messageArray.Length; i++) { if (i == 0) { if (lastWordPos == 0) brain.WriteLine("START_SENTENCE " + messageArray[i]); if (lastWordPos > 0) brain.WriteLine("START_SENTENCE " + messageArray[i] + " " + messageArray[i + 1]); brain.Flush(); } else { if (i == lastWordPos) brain.WriteLine(messageArray[i - 1] + " " + messageArray[i] + " END_SENTENCE"); else { brain.WriteLine(messageArray[i - j] + " " + messageArray[i] + " " + messageArray[i + 1]); } brain.Flush(); } } j++; brain.Close(); } } #endregion #region Pull From Brain public String pullFromBrain(string conversation) { // Does the bot have a brain? If not, we can't proceede without crashing. if (!File.Exists(BRAIN_FILE)) { Console.WriteLine(BRAIN_FILE + " does not exist."); return "I need to see some conversations before I can use this brain!"; } // Okay, it does. Continue. String randomLine; String convoStart = getFirstTwoWordsFromConvo(conversation); // this is going to be implemented later. // Grab the beginning of our sentence randomLine = getStartingSentence(convoStart); string searchWord = ""; // While the searchWord is not "END_SENTENCE" let's continue to build the sentence. do { searchWord = getLastWord(randomLine); if (searchWord != "") randomLine += getLine(searchWord); ///Console.WriteLine("current line: " + randomLine); } while ((searchWord != "END_SENTENCE END_SENTENCE") && (randomLine.Length <= 500)); // Last step: remove "START_SENTENCE" and "END_SENTENCE", trim up the space and replace the emoticons. randomLine = randomLine.Replace("START_SENTENCE", ""); randomLine = randomLine.Replace("END_SENTENCE", ""); randomLine = randomLine.Replace(" ", ""); randomLine = randomLine.Trim(); Filter filter = new Filter(); // needed to replace the emoticons. randomLine = randomLine.Replace("RANDOM_EMOTICON_HAPPY", filter.randomEmoticon_happy()); randomLine = randomLine.Replace("RANDOM_EMOTICON_SAD", filter.randomEmoticon_sad()); return randomLine; } #endregion #region getFirstTwoWordsFromConvo private String getFirstTwoWordsFromConvo(string conversation) { // This is going to be implemented in the future. string firstTwoWords = ""; return firstTwoWords; } #endregion #region getStartingSentence() private String getStartingSentence(string convoStart) { using (StreamReader brainFile = File.OpenText(BRAIN_FILE)) { // This will add all of the lines in the brain file to an array so we can use them ArrayList startingLines = new ArrayList(); string line; int numberOfLines; while ((line = brainFile.ReadLine()) != null) { if (line.StartsWith("START_SENTENCE")) startingLines.Add(line); } numberOfLines = startingLines.Count; // Now we need to randomly pick a line and pull a line. Thread.Sleep(50); int rnd = randnum.Next(numberOfLines); string randomStart = (string)startingLines[rnd]; brainFile.Close(); return randomStart; } } #endregion #region getLastWord(randomLine) private String getLastWord(string randomLine) { string[] messageArray; messageArray = randomLine.Split(new char[] { ' ' }); int lastWord = (messageArray.Length - 1); if (lastWord >= 1) return messageArray[lastWord - 1] + " " + messageArray[lastWord]; else return messageArray[lastWord]; } #endregion #region getLine(string searchWord) private String getLine(string searchWord) { using (StreamReader brainFile = File.OpenText(BRAIN_FILE)) { // This will add all of the lines in the brain file to an array so we can use them ArrayList lines = new ArrayList(); string line; int numberOfLines; searchWord = searchWord.Replace("START_SENTENCE", ""); searchWord = searchWord.Trim(); while ((line = brainFile.ReadLine()) != null) { if (line.StartsWith(searchWord)) lines.Add(line); } numberOfLines = lines.Count; if (numberOfLines == 0) return " END_SENTENCE"; if (previousSearch == searchWord) return " END_SENTENCE"; // Now we need to randomly pick a line and pull a line. Thread.Sleep(50); int rnd = randnum.Next(numberOfLines); string randomLine = (string)lines[rnd]; lastLine = randomLine; // If I don't do this it'll repeat the word twice in the sentence. randomLine = randomLine.Replace(searchWord, ""); previousSearch = searchWord; brainFile.Close(); return randomLine; } } #endregion } }