From d99692ff1cf9ee3d7c7e06364722cefb95d0d340 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Mon, 3 Sep 2018 15:56:18 +0100 Subject: [PATCH] Bugfix word-level generation --- MarkovGrams/NGrams.cs | 5 ++--- MarkovGrams/UnweightedMarkovChain.cs | 4 ++-- MarkovGrams/WeightedMarkovChain.cs | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/MarkovGrams/NGrams.cs b/MarkovGrams/NGrams.cs index 6f2807c..fad2ce0 100644 --- a/MarkovGrams/NGrams.cs +++ b/MarkovGrams/NGrams.cs @@ -50,7 +50,7 @@ namespace MarkovGrams else { string[] parts = str.Split(" ".ToCharArray()); for (int i = 0; i < parts.Length; i++) - results.Add(string.Join(" ", str.Skip(i).Take(order))); + results.Add(string.Join(" ", str.Skip(i).Take(order)).Trim()); } return results.Distinct(); } @@ -72,7 +72,6 @@ namespace MarkovGrams GenerateWeighted(word, order, mode, ref results); i++; } - Console.WriteLine(" - done"); return results; } /// @@ -94,7 +93,7 @@ namespace MarkovGrams else { string[] parts = str.Split(" ".ToCharArray()); for (int i = 0; i < parts.Length - order; i++) { - string ngram = string.Join(" ", parts.Skip(i).Take(order)); + string ngram = string.Join(" ", parts.Skip(i).Take(order)).Trim(); if (!results.ContainsKey(ngram)) results[ngram] = 0; results[ngram]++; diff --git a/MarkovGrams/UnweightedMarkovChain.cs b/MarkovGrams/UnweightedMarkovChain.cs index a232f57..9e32271 100644 --- a/MarkovGrams/UnweightedMarkovChain.cs +++ b/MarkovGrams/UnweightedMarkovChain.cs @@ -72,7 +72,7 @@ namespace MarkovGrams while((Mode == GenerationMode.CharacterLevel ? result.Length : result.Split(' ').Length) < length) { // The substring that the next ngram in the chain needs to start with - string nextStartsWith = Mode == GenerationMode.CharacterLevel ? lastNgram.Substring(1) : lastNgram.Split(' ')[0]; + string nextStartsWith = Mode == GenerationMode.CharacterLevel ? lastNgram.Substring(1) : lastNgram.Split(' ').Last(); // Get a list of possible n-grams we could choose from next List nextNgrams = ngrams.FindAll(gram => gram.StartsWith(nextStartsWith)); // If there aren't any choices left, we can't exactly keep adding to the new string any more :-( @@ -84,7 +84,7 @@ namespace MarkovGrams if (Mode == GenerationMode.CharacterLevel) result += nextNgram[nextNgram.Length - 1]; else - result += string.Join(" ", nextNgram.Split(' ').Skip(1)); + result += ' ' + string.Join(" ", nextNgram.Split(' ').Skip(1)); lastNgram = nextNgram; } diff --git a/MarkovGrams/WeightedMarkovChain.cs b/MarkovGrams/WeightedMarkovChain.cs index 295f44d..091647f 100644 --- a/MarkovGrams/WeightedMarkovChain.cs +++ b/MarkovGrams/WeightedMarkovChain.cs @@ -87,7 +87,7 @@ namespace MarkovGrams { wrandom.ClearContents(); // The substring that the next ngram in the chain needs to start with - string nextStartsWith = Mode == GenerationMode.CharacterLevel ? lastNgram.Substring(1) : lastNgram.Split(' ')[0]; + string nextStartsWith = Mode == GenerationMode.CharacterLevel ? lastNgram.Substring(1) : lastNgram.Split(' ').Last(); // Get a list of possible n-grams we could choose from next Dictionary convNextNgrams = new Dictionary(); ngrams.Where(gram_data => gram_data.Key.StartsWith(nextStartsWith)) @@ -102,7 +102,7 @@ namespace MarkovGrams if (Mode == GenerationMode.CharacterLevel) result += nextNgram[nextNgram.Length - 1]; else - result += string.Join(" ", nextNgram.Split(' ').Skip(1)); + result += ' ' + string.Join(" ", nextNgram.Split(' ').Skip(1)); lastNgram = nextNgram; } wrandom.ClearContents();