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 {
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;
}
/// <summary>
@ -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]++;

View file

@ -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<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 :-(
@ -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;
}

View file

@ -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<string, double> convNextNgrams = new Dictionary<string, double>();
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();