Browse Source

Bugfix word-level generation

master
Starbeamrainbowlabs 2 years ago
parent
commit
d99692ff1c
Signed by: sbrl GPG Key ID: 1BE5172E637709C2
3 changed files with 6 additions and 7 deletions
  1. +2
    -3
      MarkovGrams/NGrams.cs
  2. +2
    -2
      MarkovGrams/UnweightedMarkovChain.cs
  3. +2
    -2
      MarkovGrams/WeightedMarkovChain.cs

+ 2
- 3
MarkovGrams/NGrams.cs 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]++;


+ 2
- 2
MarkovGrams/UnweightedMarkovChain.cs 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;
}


+ 2
- 2
MarkovGrams/WeightedMarkovChain.cs 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();


Loading…
Cancel
Save