Пример #1
0
        // ***
        // -------------------------------
        // Methods for removing sentences.
        // -------------------------------
        // ***

        public void RemoveSentence(string[] sentenceWords)
        {
            if (sentenceWords == null)
            {
                throw new ArgumentNullException(nameof(sentenceWords));
            }
            // Return early if there's nothing to do.
            if (sentenceWords.Length < Order)
            {
                return;
            }

            string[][] chains = MarkovTools.TokenizeSentence(sentenceWords, Order);

            using (var cmd = conn.CreateCommand())
            {
                cmd.CommandText = "PRAGMA foreign_keys = ON";
                cmd.ExecuteNonQuery();

                using (var tr = conn.BeginTransaction())
                {
                    cmd.Transaction = tr;
                    DeleteChains(chains, cmd);
                    tr.Commit();
                }
            }
        }
Пример #2
0
        // ***
        // -----------------------------
        // Methods for adding sentences.
        // -----------------------------
        // ***


        /// <summary>
        /// Adds sentence to the brain. Sentence should not have null or empty entries.
        /// On what you Split (besides spaces) depends on how much whitespace 'formatting' you want the brain to retain.
        /// </summary>
        /// <exception cref="ArgumentNullException">Thrown if sentenceWords is null.</exception>
        /// <exception cref="ArgumentException">Thrown if sentenceWords contains null or empty entries.</exception>
        /// <param name="sentenceWords">Sentence (array of words).</param>
        /// <param name="source">Source of the sentence, will be ignored if null or empty.</param>
        public void AddSentence(string[] sentenceWords, string source)
        {
            if (sentenceWords == null)
            {
                throw new ArgumentNullException(nameof(sentenceWords));
            }
            // Return early if there's nothing to do.
            if (sentenceWords.Length < Order)
            {
                return;
            }

            string[][] chains = MarkovTools.TokenizeSentence(sentenceWords, Order);

            var sw = Stopwatch.StartNew();

            using (SqliteTransaction tr = conn.BeginTransaction())
                using (SqliteCommand insertCmd = conn.CreateCommand(),
                       sourcesCmd = conn.CreateCommand())
                {
                    insertCmd.Transaction  = tr;
                    sourcesCmd.Transaction = tr;

                    if (!string.IsNullOrEmpty(source))
                    {
                        // Make sure `source` exists in Sources.
                        sourcesCmd.CommandText = "INSERT OR IGNORE INTO Sources VALUES(null, @Source)";
                        sourcesCmd.Parameters.AddWithValue("@Source", source);
                        sourcesCmd.ExecuteNonQuery();
                    }

                    UpdateWordCount(sentenceWords, insertCmd);
                    InsertChains(chains, source, insertCmd, sourcesCmd);

                    try
                    {
                        tr.Commit();
                    }
                    catch (SqliteException)
                    {
                        Console.WriteLine("!! ERROR ADDING: " + string.Join(" ", sentenceWords));
                        Console.WriteLine("!! AddSentence time: " + sw.Elapsed);
                        throw;
                    }
                    finally
                    {
                        sw.Stop();
                    }
                }
        }
Пример #3
0
        public void Add(string[] sentence, string source)
        {
            if (sentence == null)
            {
                throw new ArgumentNullException("sentence");
            }

            if (Filter)
            {
                InternalAdd(MarkovTools.Filter(sentence), source);
            }
            else
            {
                InternalAdd(sentence, source);
            }
        }
Пример #4
0
        public Sentence BuildResponse(string[] message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (Filter)
            {
                return(InternalResponse(MarkovTools.Filter(message)));
            }
            else
            {
                return(InternalResponse(message));
            }
        }
Пример #5
0
        public void Remove(string[] sentence)
        {
            if (sentence == null)
            {
                throw new ArgumentNullException("sentence");
            }

            if (Filter)
            {
                brain.RemoveSentence(MarkovTools.Filter(sentence));
            }
            else
            {
                brain.RemoveSentence(sentence);
            }
        }