Bugfix word-level generation

This commit is contained in:
Starbeamrainbowlabs 2018-09-03 15:56:18 +01:00
parent face693554
commit d99692ff1c
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 6 additions and 7 deletions

View file

@ -50,7 +50,7 @@ namespace MarkovGrams
else { else {
string[] parts = str.Split(" ".ToCharArray()); string[] parts = str.Split(" ".ToCharArray());
for (int i = 0; i < parts.Length; i++) 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(); return results.Distinct();
} }
@ -72,7 +72,6 @@ namespace MarkovGrams
GenerateWeighted(word, order, mode, ref results); GenerateWeighted(word, order, mode, ref results);
i++; i++;
} }
Console.WriteLine(" - done");
return results; return results;
} }
/// <summary> /// <summary>
@ -94,7 +93,7 @@ namespace MarkovGrams
else { else {
string[] parts = str.Split(" ".ToCharArray()); string[] parts = str.Split(" ".ToCharArray());
for (int i = 0; i < parts.Length - order; i++) { 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)) if (!results.ContainsKey(ngram))
results[ngram] = 0; results[ngram] = 0;
results[ngram]++; results[ngram]++;

View file

@ -72,7 +72,7 @@ namespace MarkovGrams
while((Mode == GenerationMode.CharacterLevel ? result.Length : result.Split(' ').Length) < length) while((Mode == GenerationMode.CharacterLevel ? result.Length : result.Split(' ').Length) < length)
{ {
// The substring that the next ngram in the chain needs to start with // 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 // Get a list of possible n-grams we could choose from next
List<string> nextNgrams = ngrams.FindAll(gram => gram.StartsWith(nextStartsWith)); List<string> 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 :-( // 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) if (Mode == GenerationMode.CharacterLevel)
result += nextNgram[nextNgram.Length - 1]; result += nextNgram[nextNgram.Length - 1];
else else
result += string.Join(" ", nextNgram.Split(' ').Skip(1)); result += ' ' + string.Join(" ", nextNgram.Split(' ').Skip(1));
lastNgram = nextNgram; lastNgram = nextNgram;
} }

View file

@ -87,7 +87,7 @@ namespace MarkovGrams
{ {
wrandom.ClearContents(); wrandom.ClearContents();
// The substring that the next ngram in the chain needs to start with // 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 // Get a list of possible n-grams we could choose from next
Dictionary<string, double> convNextNgrams = new Dictionary<string, double>(); Dictionary<string, double> convNextNgrams = new Dictionary<string, double>();
ngrams.Where(gram_data => gram_data.Key.StartsWith(nextStartsWith)) ngrams.Where(gram_data => gram_data.Key.StartsWith(nextStartsWith))
@ -102,7 +102,7 @@ namespace MarkovGrams
if (Mode == GenerationMode.CharacterLevel) if (Mode == GenerationMode.CharacterLevel)
result += nextNgram[nextNgram.Length - 1]; result += nextNgram[nextNgram.Length - 1];
else else
result += string.Join(" ", nextNgram.Split(' ').Skip(1)); result += ' ' + string.Join(" ", nextNgram.Split(' ').Skip(1));
lastNgram = nextNgram; lastNgram = nextNgram;
} }
wrandom.ClearContents(); wrandom.ClearContents();