Browse Source

Bugfix: Correct word-level flat n-gram generation

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

+ 2
- 1
MarkovGrams/NGrams.cs View File

@ -50,7 +50,8 @@ 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)).Trim());
results.Add(string.Join(" ", parts.Skip(i).Take(order)).Trim());
}
return results.Distinct();
}


+ 3
- 3
MarkovGrams/UnweightedMarkovChain.cs View File

@ -75,7 +75,7 @@ namespace MarkovGrams
string result = RandomNgram();
string lastNgram = result;
List<int> choiceCounts = new List<int>(); int i = 0;
while((Mode == GenerationMode.CharacterLevel ? result.Length : result.Split(' ').Length) < length)
while((Mode == GenerationMode.CharacterLevel ? result.Length : result.CountCharInstances(" ".ToCharArray()) + 1) < length)
{
// The substring that the next ngram in the chain needs to start with
string nextStartsWith = Mode == GenerationMode.CharacterLevel ? lastNgram.Substring(1) : string.Join(" ", lastNgram.Split(' ').Skip(1));
@ -91,8 +91,8 @@ namespace MarkovGrams
if (Mode == GenerationMode.CharacterLevel)
result += nextNgram[nextNgram.Length - 1];
else
result += ' ' + nextNgram.Split(' ').Last();
result += ' ' + nextNgram.Substring(nextNgram.LastIndexOf(' ') + 1);
lastNgram = nextNgram; i++;
}


Loading…
Cancel
Save