コード例 #1
0
ファイル: MainForm.cs プロジェクト: MaxRev-Dev/ai-sys
 public MainForm()
 {
     InitializeComponent();
     _heartBeat.Elapsed += OnHeartBeat;
     _heartBeat.Interval = 50;
     _heartBeat.Start();
     _chain = new MarkovChain();
     Load  += MainForm_Load;
 }
コード例 #2
0
        public static void Serialize(MarkovChain chain, Stream stream)
        {
            // Will be faster than calling Array.IndexOf for all words
            Dictionary <String, Int32> wordLUT = CreateWordLookupTable(chain.SubSentences);

            Dictionary <String, Int32> .KeyCollection wordList = wordLUT.Keys;

            using (var bw = new BinaryWriter(stream, Encoding.UTF8, true))
            {
                // Write the header
                bw.Write("MARKOV");    // Magic number
                bw.Write(5);           // Version

                // Write all words
                bw.Write(wordList.Count);
                foreach (var word in wordList)
                {
                    bw.Write(word);
                }

                // Write sentence initiators indexes
                bw.Write(chain.SentenceInitiators.Count);
                foreach (var initiator in chain.SentenceInitiators)
                {
                    bw.Write(wordLUT[initiator]);
                }

                // Write amount of word and sub-sentences pairs
                bw.Write(chain.SubSentences.Count);
                foreach (KeyValuePair <String, List <String[]> > kv in chain.SubSentences)
                {
                    // Write index of the word
                    bw.Write(wordLUT[kv.Key]);

                    // Write amount of sub-sentences
                    List <String[]> subsentences = kv.Value;
                    bw.Write(subsentences.Count);
                    foreach (var subsentence in subsentences)
                    {
                        // Then write the index of each word in
                        // the sub-sentence
                        bw.Write(subsentence.Length);
                        foreach (var word in subsentence)
                        {
                            bw.Write(wordLUT[word]);
                        }
                    }
                }
            }
        }
コード例 #3
0
        public static MarkovChain Deserialize(Stream stream)
        {
            using (var br = new BinaryReader(stream, Encoding.UTF8, true))
            {
                FileHeader header = ReadHeader(br);
                if (!header.IsValid)
                {
                    throw new Exception("Invalid Markov save file.");
                }

                var chain = new MarkovChain( );

                String[] wordlist;

                // Read word dictionary
                wordlist = new String[br.ReadInt32( )];
                for (var i = 0; i < wordlist.Length; i++)
                {
                    wordlist[i] = br.ReadString( );
                }

                // Read sentence intiators
                var initiatorCount = br.ReadInt32( );
                while (initiatorCount-- > 0)
                {
                    // Read each word by it's index
                    chain.SentenceInitiators.Add(wordlist[br.ReadInt32( )]);
                }

                // Read the (word, sub-sentences) pairs
                var pairCount = br.ReadInt32( );
                while (pairCount-- > 0)
                {
                    // Read the word by it's index
                    var word = wordlist[br.ReadInt32( )];

                    // Read the sub-sentences
                    var subsentenceCount = br.ReadInt32( );
                    chain.SubSentences[word] = new List <String[]> (subsentenceCount);
                    while (subsentenceCount-- > 0)
                    {
                        String[] subsentence = new String[br.ReadInt32( )];
                        for (var i = 0; i < subsentence.Length; i++)
                        {
                            // Read each word of the sub-sentence
                            // by their indexes
                            subsentence[i] = wordlist[br.ReadInt32( )];
                        }

                        chain.SubSentences[word].Add(subsentence);
                    }
                }

                for (var i = 0; i < wordlist.Length; i++)
                {
                    wordlist[i] = null;
                }
                wordlist = null;

                return(chain);
            }
        }