Example #1
0
        public static bool Save(AI ai, string filename)
        {
            try { File.Delete(filename); }
            catch { return false; } //return false if it failed to delete the old file, eg because of permissions

            Dictionary<Word, int> map = new Dictionary<Word, int>();

            int count = 0;
            //Word properties, associations
            string propertyList = "", followList = "", associationList = "";

            //Generates integer-mapped references to words
            foreach ( Word word in ai.wordbank.wordlist.Values )
            { map.Add(word, count); count++; }

            foreach ( KeyValuePair<Word, int> wordIndex in map )
            {
                //Adds word to list, with properties (key fields)
                propertyList += WordPropertiesString(wordIndex.Key);

                //Adds each follower to the mapping of words to following words
                foreach ( KeyValuePair<Word, int> follower in wordIndex.Key.followingWords )
                { followList += WordFollowingString(wordIndex, follower, map); }

                //Adds each association to the mapping of words to associated words
                foreach ( Word association in wordIndex.Key.associations )
                { associationList += WordAssoiationString(wordIndex.Value, association, map); }
            }

            File.WriteAllText(filename, propertyList.TrimEnd() +"\r\n");
            File.AppendAllText(filename, followList.TrimEnd() +"\r\n");
            File.AppendAllText(filename, associationList.TrimEnd());

            return true;
        }
Example #2
0
        public static bool Load(AI ai, string filename)
        {
            if ( File.Exists(filename) == false ) { return false; }

            string[] lines = File.ReadAllLines(filename);
            if ( lines.Length < 3 ) { return false; }

            string propertyList = lines[0], followList = lines[1], associationList = lines[2];
            Dictionary<int, Word> map = new Dictionary<int, Word>();

            int count = 0;
            foreach ( string word in propertyList.Split(' ') )
            {
                string[] parameters = word.Split('.');
                if ( parameters.Length < 5 ) { Program.DebugMsg("Load - Skipped word (too few args), data: " +word +"|c:" + parameters.Length); continue; }

                Word newWord = null;
                if ( ai.wordbank.TryAddWord(parameters[0], out newWord) == false )
                { Program.DebugMsg("Load - Skipped word (addWord failed), data: " +word); continue; }

                newWord.startingWord = bool.Parse(parameters[1]);
                newWord.endingWord = bool.Parse(parameters[2]);
                newWord.capitalisedWordWeight = int.Parse(parameters[3]);
                newWord.frequency = int.Parse(parameters[4]);

                map.Add(count, newWord);

                count++;
            }

            //Reading in every following word and its frequency
            foreach ( string follower in followList.Split(' ') )
            {
                string[] values = follower.Split(':');
                if ( values.Length < 3 ) { Program.DebugMsg("Load - Skipped follower (too few args), data: " +follower + " |c:" +values.Length); continue; }

                Word src, dest;
                src = map[int.Parse(values[0])];
                dest = map[int.Parse(values[1])];

                int frequency = int.Parse(values[2]);

                src.followingWords.Add(new KeyValuePair<Word, int>(dest, frequency));
            }

            //Reading in every association
            foreach ( string association in followList.Split(' ') )
            {
                string[] values = association.Split(':');
                if ( values.Length < 2 ) { Program.DebugMsg("Load - Skipped association (too few args), data: " +association + " |c:" +values.Length); continue; }

                Word src, dest;
                src = map[int.Parse(values[0])];
                dest = map[int.Parse(values[1])];

                src.associations.Add(dest);
            }

            return true;
        }
Example #3
0
        public static void ParseUserInput(AI ai)
        {
            string input = DANI.Program.ReadIn().Trim();

            //Basic checks
            if ( input == "quit" ) { Application.Exit(); }
            if ( input == "" ) { return; }

            //Checking if it's not a command - if so, consult AI
            if ( input.Substring(0, 1) != "/" )
            {
                if ( printInput ) { Program.PrintOutLn("You: " + input, false); }
                DANI.Program.PrintOutLn("Dani: ", false);
                DANI.Program.PrintOutLn(ai.GetResponse(input), true); //on a separate line for text-to-speech reasons
            }
            else //Otherwise treat it as a command
            {
                //Tokenise user input
                string[] tokens = input.Substring(1).ToLower().Split(' ');
                if ( tokens.Length == 0 ) { return; }

                switch ( tokens[0] )
                {
                    //Reset the AI
                    case "reset": currentAI = new AI(); break;
                    case "clear": form.outputTextbox.Clear(); break;

                    //Clear the console output
                    case "exit":
                    case "quit": Application.Exit(); break;

                    //Shows the help listing
                    case "help": ShowHelp(); break;

                    //Change the minimum number of sentences generated
                    case "sentencemin":
                    {
                        int newValue;
                        if ( (tokens.Length > 1) && (int.TryParse(tokens[1], out newValue)) )
                        {
                            Sentence.minSentences = newValue;
                            Sentence.maxSentences = Math.Max(Sentence.maxSentences, Sentence.minSentences);
                        }
                        break;
                    }

                    //Change the maximum number of sentences generated
                    case "sentencemax":
                    {
                        int newValue;
                        if ( ( tokens.Length > 1 ) && ( int.TryParse(tokens[1], out newValue) ) )
                        {
                            Sentence.maxSentences = newValue;
                            Sentence.minSentences = Math.Min(Sentence.minSentences, Sentence.maxSentences);
                        }
                        break;
                    }

                    //Wordbank commands split by tokens
                    case "wordbank":
                    {
                        if ( tokens.Length < 2 ) { return; }
                        switch ( tokens[1] )
                        {
                            case "clear": form.outputTextbox.Clear(); break;
                            case "print":
                            {
                                if ( tokens.Length < 3 )
                                { PrintOutLn(currentAI.wordbank.ToString(), false); }
                                else
                                {
                                    Word foundWord;
                                    if ( currentAI.TryGetWord(tokens[2], out foundWord) )
                                    { PrintOutLn(foundWord.ToString(), false); }
                                }
                                break;
                            }

                        } //end of argument switch
                        break;
                    }

                    //Heh, why not
                    case "crash": Environment.FailFast("Hello world"); break;

                } //end of command interpreter
            }
        }
Example #4
0
        static void Main()
        {
            //Windows Forms boilerplate code
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            getFileWindow.InitialDirectory = Application.StartupPath;
            saveFileWindow.InitialDirectory = Application.StartupPath;

            currentAI = new AI(); //Could have multiple AIs talking to each other
            form = new InputForm();

            ShowHelp(); //prints usage instructions to output

            Application.Run(form);
        }