Compare commits
No commits in common. "6b79d8c5bb6dbcdb84c8cae7c46d28109176f35f" and "d99692ff1cf9ee3d7c7e06364722cefb95d0d340" have entirely different histories.
6b79d8c5bb
...
d99692ff1c
3 changed files with 12 additions and 40 deletions
|
@ -90,7 +90,6 @@ namespace MarkovGrams
|
||||||
return word.Split(' ');
|
return word.Split(' ');
|
||||||
return new string[] { word.Trim() };
|
return new string[] { word.Trim() };
|
||||||
});
|
});
|
||||||
List<float> choicePointRatios = new List<float>();
|
|
||||||
|
|
||||||
switch (operationMode)
|
switch (operationMode)
|
||||||
{
|
{
|
||||||
|
@ -102,11 +101,9 @@ namespace MarkovGrams
|
||||||
);
|
);
|
||||||
unweightedChain.StartOnUppercase = startOnUppercase;
|
unweightedChain.StartOnUppercase = startOnUppercase;
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++)
|
||||||
Console.WriteLine(unweightedChain.Generate(length, out float nextChoicePathRatio));
|
Console.WriteLine(unweightedChain.Generate(length));
|
||||||
choicePointRatios.Add(nextChoicePathRatio);
|
Console.Error.WriteLine($"{count} words in {utimer.ElapsedMilliseconds}ms");
|
||||||
}
|
|
||||||
Console.Error.WriteLine($"{count} words in {utimer.ElapsedMilliseconds}ms (average choice-point-ratio: {Math.Round(choicePointRatios.Sum()/count, 2)})");
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Mode.WeightedMarkov:
|
case Mode.WeightedMarkov:
|
||||||
|
@ -117,11 +114,9 @@ namespace MarkovGrams
|
||||||
);
|
);
|
||||||
weightedChain.StartOnUppercase = startOnUppercase;
|
weightedChain.StartOnUppercase = startOnUppercase;
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++)
|
||||||
Console.WriteLine(weightedChain.Generate(length, out float nextChoicePointRatio));
|
Console.WriteLine(weightedChain.Generate(length));
|
||||||
choicePointRatios.Add(nextChoicePointRatio);
|
Console.Error.WriteLine($"{count} words in {wtimer.ElapsedMilliseconds}ms");
|
||||||
}
|
|
||||||
Console.Error.WriteLine($"{count} words in {wtimer.ElapsedMilliseconds}ms (average choice-point-ratio: {Math.Round(choicePointRatios.Sum() / count, 2)})");
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Mode.NGrams:
|
case Mode.NGrams:
|
||||||
|
|
|
@ -66,21 +66,15 @@ namespace MarkovGrams
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>A new random string.</returns>
|
/// <returns>A new random string.</returns>
|
||||||
public string Generate(int length)
|
public string Generate(int length)
|
||||||
{
|
|
||||||
return Generate(length, out float noop);
|
|
||||||
}
|
|
||||||
public string Generate(int length, out float choicePointRatio)
|
|
||||||
{
|
{
|
||||||
string result = RandomNgram();
|
string result = RandomNgram();
|
||||||
string lastNgram = result;
|
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.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) : string.Join(" ", lastNgram.Split(' ').Skip(1));
|
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));
|
||||||
choiceCounts.Add(nextNgrams.Count);
|
|
||||||
// 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 :-(
|
||||||
if(nextNgrams.Count == 0)
|
if(nextNgrams.Count == 0)
|
||||||
break;
|
break;
|
||||||
|
@ -90,15 +84,10 @@ namespace MarkovGrams
|
||||||
if (Mode == GenerationMode.CharacterLevel)
|
if (Mode == GenerationMode.CharacterLevel)
|
||||||
result += nextNgram[nextNgram.Length - 1];
|
result += nextNgram[nextNgram.Length - 1];
|
||||||
else
|
else
|
||||||
result += ' ' + nextNgram.Split(' ').Last();
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,25 +80,18 @@ namespace MarkovGrams
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>A new random string.</returns>
|
/// <returns>A new random string.</returns>
|
||||||
public string Generate(int length)
|
public string Generate(int length)
|
||||||
{
|
|
||||||
return Generate(length, out float noop);
|
|
||||||
}
|
|
||||||
public string Generate(int length, out float choicePointRatio)
|
|
||||||
{
|
{
|
||||||
string result = RandomNgram();
|
string result = RandomNgram();
|
||||||
string lastNgram = result;
|
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.Split(' ').Length) < length)
|
||||||
{
|
{
|
||||||
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) : string.Join(" ", lastNgram.Split(' ').Skip(1));
|
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))
|
||||||
.ForEach((KeyValuePair<string, double> ngramData) => convNextNgrams.Add(ngramData.Key, ngramData.Value));
|
.ForEach((KeyValuePair<string, double> 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 there aren't any choices left, we can't exactly keep adding to the new string any more :-(
|
||||||
if(convNextNgrams.Count() == 0)
|
if(convNextNgrams.Count() == 0)
|
||||||
break;
|
break;
|
||||||
|
@ -109,15 +102,10 @@ namespace MarkovGrams
|
||||||
if (Mode == GenerationMode.CharacterLevel)
|
if (Mode == GenerationMode.CharacterLevel)
|
||||||
result += nextNgram[nextNgram.Length - 1];
|
result += nextNgram[nextNgram.Length - 1];
|
||||||
else
|
else
|
||||||
result += ' ' + nextNgram.Split(' ').Last();
|
result += ' ' + string.Join(" ", nextNgram.Split(' ').Skip(1));
|
||||||
lastNgram = nextNgram; i++;
|
lastNgram = nextNgram;
|
||||||
}
|
}
|
||||||
wrandom.ClearContents();
|
wrandom.ClearContents();
|
||||||
|
|
||||||
if (choiceCounts.Sum() > 0)
|
|
||||||
choicePointRatio = (float)choiceCounts.Sum() / (float)(i + 1);
|
|
||||||
else
|
|
||||||
choicePointRatio = 0;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue