diff --git a/MarkovGrams/Program.cs b/MarkovGrams/Program.cs index b7d7871..2d3134d 100644 --- a/MarkovGrams/Program.cs +++ b/MarkovGrams/Program.cs @@ -90,6 +90,7 @@ namespace MarkovGrams return word.Split(' '); return new string[] { word.Trim() }; }); + List choicePointRatios = new List(); switch (operationMode) { @@ -101,9 +102,11 @@ namespace MarkovGrams ); unweightedChain.StartOnUppercase = startOnUppercase; - for (int i = 0; i < count; i++) - Console.WriteLine(unweightedChain.Generate(length)); - Console.Error.WriteLine($"{count} words in {utimer.ElapsedMilliseconds}ms"); + for (int i = 0; i < count; i++) { + Console.WriteLine(unweightedChain.Generate(length, out float nextChoicePathRatio)); + choicePointRatios.Add(nextChoicePathRatio); + } + Console.Error.WriteLine($"{count} words in {utimer.ElapsedMilliseconds}ms (average choice-point-ratio: {Math.Round(choicePointRatios.Sum()/count, 2)})"); break; case Mode.WeightedMarkov: @@ -114,9 +117,11 @@ namespace MarkovGrams ); weightedChain.StartOnUppercase = startOnUppercase; - for (int i = 0; i < count; i++) - Console.WriteLine(weightedChain.Generate(length)); - Console.Error.WriteLine($"{count} words in {wtimer.ElapsedMilliseconds}ms"); + for (int i = 0; i < count; i++) { + Console.WriteLine(weightedChain.Generate(length, out float nextChoicePointRatio)); + choicePointRatios.Add(nextChoicePointRatio); + } + Console.Error.WriteLine($"{count} words in {wtimer.ElapsedMilliseconds}ms (average choice-point-ratio: {Math.Round(choicePointRatios.Sum() / count, 2)})"); break; case Mode.NGrams: diff --git a/MarkovGrams/UnweightedMarkovChain.cs b/MarkovGrams/UnweightedMarkovChain.cs index 9e32271..2b97803 100644 --- a/MarkovGrams/UnweightedMarkovChain.cs +++ b/MarkovGrams/UnweightedMarkovChain.cs @@ -66,15 +66,21 @@ namespace MarkovGrams /// /// A new random string. public string Generate(int length) + { + return Generate(length, out float noop); + } + public string Generate(int length, out float choicePointRatio) { string result = RandomNgram(); string lastNgram = result; + List choiceCounts = new List(); int i = 0; 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(' ').Last(); // Get a list of possible n-grams we could choose from next List nextNgrams = ngrams.FindAll(gram => gram.StartsWith(nextStartsWith)); + choiceCounts.Add(nextNgrams.Count); // If there aren't any choices left, we can't exactly keep adding to the new string any more :-( if(nextNgrams.Count == 0) break; @@ -85,9 +91,14 @@ namespace MarkovGrams result += nextNgram[nextNgram.Length - 1]; else result += ' ' + string.Join(" ", nextNgram.Split(' ').Skip(1)); - lastNgram = nextNgram; + + lastNgram = nextNgram; i++; } + if (choiceCounts.Sum() > 0) + choicePointRatio = (float)choiceCounts.Sum() / (float)(i + 1); + else + choicePointRatio = 0; return result; } } diff --git a/MarkovGrams/WeightedMarkovChain.cs b/MarkovGrams/WeightedMarkovChain.cs index 091647f..73c7cd8 100644 --- a/MarkovGrams/WeightedMarkovChain.cs +++ b/MarkovGrams/WeightedMarkovChain.cs @@ -80,9 +80,14 @@ namespace MarkovGrams /// /// A new random string. public string Generate(int length) + { + return Generate(length, out float noop); + } + public string Generate(int length, out float choicePointRatio) { string result = RandomNgram(); string lastNgram = result; + List choiceCounts = new List(); int i = 0; while((Mode == GenerationMode.CharacterLevel ? result.Length : result.Split(' ').Length) < length) { wrandom.ClearContents(); @@ -92,6 +97,8 @@ namespace MarkovGrams Dictionary convNextNgrams = new Dictionary(); ngrams.Where(gram_data => gram_data.Key.StartsWith(nextStartsWith)) .ForEach((KeyValuePair ngramData) => convNextNgrams.Add(ngramData.Key, ngramData.Value)); + + choiceCounts.Add(convNextNgrams.Count); // If there aren't any choices left, we can't exactly keep adding to the new string any more :-( if(convNextNgrams.Count() == 0) break; @@ -103,9 +110,14 @@ namespace MarkovGrams result += nextNgram[nextNgram.Length - 1]; else result += ' ' + string.Join(" ", nextNgram.Split(' ').Skip(1)); - lastNgram = nextNgram; + lastNgram = nextNgram; i++; } wrandom.ClearContents(); + + if (choiceCounts.Sum() > 0) + choicePointRatio = (float)choiceCounts.Sum() / (float)(i + 1); + else + choicePointRatio = 0; return result; } }