// *** // ------------------------------- // 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(); } } }
// *** // ----------------------------- // 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(); } } }