Skip to content

Commit

Permalink
Several key Syllabore classes now implement IRandomizable:
Browse files Browse the repository at this point in the history
- NameGenerator
- SyllableGenerator
- SyllableSet
- TransformSet
  • Loading branch information
kesac committed Jun 5, 2024
1 parent 82382e6 commit d954b1e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 45 deletions.
19 changes: 9 additions & 10 deletions Syllabore/Syllabore/NameGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;

namespace Syllabore
{
Expand All @@ -17,7 +13,7 @@ namespace Syllabore
/// individual syllables of the name.
/// </para>
/// </summary>
public class NameGenerator : INameGenerator
public class NameGenerator : INameGenerator, IRandomizable
{

/// <summary>
Expand All @@ -33,7 +29,10 @@ public class NameGenerator : INameGenerator
/// </summary>
private const int DefaultMaximumRetries = 1000;

private Random _random;
/// <summary>
/// Used to simulate randomness during name generation.
/// </summary>
public Random Random { get; set; }

/// <summary>
/// <para>
Expand Down Expand Up @@ -137,7 +136,7 @@ public NameGenerator(ISyllableGenerator provider, INameTransformer transformer,
this.UsingFilter(filter);
}

_random = new Random();
this.Random = new Random();
}

#endregion
Expand Down Expand Up @@ -419,7 +418,7 @@ public string Next()
throw new InvalidOperationException("The value of property MinimumSyllables must be less than or equal to property MaximumSyllables. Both values must be postive integers.");
}

var syllableLength = _random.Next(this.MinimumSyllables, this.MaximumSyllables + 1);
var syllableLength = this.Random.Next(this.MinimumSyllables, this.MaximumSyllables + 1);
return this.Next(syllableLength);
}

Expand Down Expand Up @@ -450,7 +449,7 @@ public Name NextName()
throw new InvalidOperationException("The value of property MinimumSyllables must be less than or equal to property MaximumSyllables. Both values must be postive integers.");
}

var syllableLength = _random.Next(this.MinimumSyllables, this.MaximumSyllables + 1);
var syllableLength = this.Random.Next(this.MinimumSyllables, this.MaximumSyllables + 1);
return this.NextName(syllableLength);
}

Expand Down Expand Up @@ -490,7 +489,7 @@ public Name NextName(int syllableLength)
}
}

if (this.Transformer != null && _random.NextDouble() < this.TransformerChance)
if (this.Transformer != null && this.Random.NextDouble() < this.TransformerChance)
{
result = this.Transformer.Apply(result);
}
Expand Down
48 changes: 25 additions & 23 deletions Syllabore/Syllabore/SyllableGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.Json.Serialization;

namespace Syllabore
{
Expand Down Expand Up @@ -102,7 +100,7 @@ public enum SyllablePosition
/// a <see cref="NameGenerator"/>.
/// </summary>
[Serializable]
public class SyllableGenerator : ISyllableGenerator
public class SyllableGenerator : ISyllableGenerator, IRandomizable
{
// These probabilities are applied if custom values are not specified
private const double DefaultChanceLeadingConsonantExists = 0.95; // Only applies if consonants are supplied
Expand All @@ -115,10 +113,14 @@ public class SyllableGenerator : ISyllableGenerator
private const double DefaultChanceFinalConsonantBecomesSequence = 0.25; // Only if consonant final sequences are supplied


private Random _random { get; set; }
private Context _context { get; set; } // For contextual command .Sequences()
private List<Grapheme> _lastChanges { get; set; } // For contextual command .Weight()

/// <summary>
/// Used to simulate randomness during syllable generation.
/// </summary>
public Random Random { get; set; }

/// <summary>
/// Leading consonants are consonants that appear before
/// the vowel within a syllable.
Expand Down Expand Up @@ -302,7 +304,7 @@ public bool LeadingVowelSequenceForStartingSyllableAllowed
/// </summary>
public SyllableGenerator()
{
_random = new Random();
this.Random = new Random();
this.Probability = new GeneratorProbability();
_context = Context.None;
_lastChanges = new List<Grapheme>();
Expand Down Expand Up @@ -623,7 +625,7 @@ private string NextLeadingConsonant()
if(this.LeadingConsonants.Count > 0)
{
//return this.LeadingConsonants[this.Random.Next(this.LeadingConsonants.Count)].Value;
return this.LeadingConsonants.RandomWeightedItem().Value;
return this.LeadingConsonants.RandomWeightedItem(this.Random).Value;
}
else
{
Expand All @@ -636,7 +638,7 @@ private string NextLeadingConsonantSequence()
if (this.LeadingConsonantSequences.Count > 0)
{
//return this.LeadingConsonantSequences[this.Random.Next(this.LeadingConsonantSequences.Count)].Value;
return this.LeadingConsonantSequences.RandomWeightedItem().Value;
return this.LeadingConsonantSequences.RandomWeightedItem(this.Random).Value;
}
else
{
Expand All @@ -649,7 +651,7 @@ private string NextVowel()
if (this.Vowels.Count > 0)
{
//return this.Vowels[this.Random.Next(this.Vowels.Count)].Value;
return this.Vowels.RandomWeightedItem().Value;
return this.Vowels.RandomWeightedItem(this.Random).Value;
}
else
{
Expand All @@ -662,7 +664,7 @@ private string NextVowelSequence()
if (this.VowelSequences.Count > 0)
{
// return this.VowelSequences[this.Random.Next(this.VowelSequences.Count)].Value;
return this.VowelSequences.RandomWeightedItem().Value;
return this.VowelSequences.RandomWeightedItem(this.Random).Value;
}
else
{
Expand All @@ -674,7 +676,7 @@ private string NextTrailingConsonant()
{
if (this.TrailingConsonants.Count > 0)
{
return this.TrailingConsonants.RandomWeightedItem().Value;
return this.TrailingConsonants.RandomWeightedItem(this.Random).Value;
}
else
{
Expand All @@ -686,7 +688,7 @@ private string NextTrailingConsonantSequence()
{
if (this.TrailingConsonantSequences.Count > 0)
{
return this.TrailingConsonantSequences.RandomWeightedItem().Value;
return this.TrailingConsonantSequences.RandomWeightedItem(this.Random).Value;
}
else
{
Expand All @@ -699,7 +701,7 @@ private string NextFinalConsonant()
{
if (this.FinalConsonants.Count > 0)
{
return this.FinalConsonants.RandomWeightedItem().Value;
return this.FinalConsonants.RandomWeightedItem(this.Random).Value;
}
else
{
Expand All @@ -711,7 +713,7 @@ private string NextFinalConsonantSequence()
{
if (this.FinalConsonantSequences.Count > 0)
{
return this.FinalConsonantSequences.RandomWeightedItem().Value;
return this.FinalConsonantSequences.RandomWeightedItem(this.Random).Value;
}
else
{
Expand Down Expand Up @@ -751,10 +753,10 @@ private string GenerateSyllable(SyllablePosition position)

if (position == SyllablePosition.Starting
&& this.LeadingVowelForStartingSyllableAllowed
&& _random.NextDouble() < this.Probability.ChanceStartingSyllableLeadingVowelExists)
&& this.Random.NextDouble() < this.Probability.ChanceStartingSyllableLeadingVowelExists)
{

if (this.VowelSequencesAllowed && _random.NextDouble() < this.Probability.ChanceStartingSyllableLeadingVowelIsSequence)
if (this.VowelSequencesAllowed && this.Random.NextDouble() < this.Probability.ChanceStartingSyllableLeadingVowelIsSequence)
{
output.Append(this.NextVowelSequence());
}
Expand All @@ -766,11 +768,11 @@ private string GenerateSyllable(SyllablePosition position)
else
{
// Determine if there is a leading consonant in this syllable
if (this.LeadingConsonantsAllowed && _random.NextDouble() < this.Probability.ChanceLeadingConsonantExists)
if (this.LeadingConsonantsAllowed && this.Random.NextDouble() < this.Probability.ChanceLeadingConsonantExists)
{

// If there is a leading consonant, determine if it is a sequence
if (this.LeadingConsonantSequencesAllowed && _random.NextDouble() < this.Probability.ChanceLeadingConsonantIsSequence)
if (this.LeadingConsonantSequencesAllowed && this.Random.NextDouble() < this.Probability.ChanceLeadingConsonantIsSequence)
{
output.Append(this.NextLeadingConsonantSequence());
}
Expand All @@ -781,11 +783,11 @@ private string GenerateSyllable(SyllablePosition position)
}

// Determine if there is a vowel in this syllable (by default, this probability is 100%)
if(this.VowelsAllowed && _random.NextDouble() < this.Probability.ChanceVowelExists)
if(this.VowelsAllowed && this.Random.NextDouble() < this.Probability.ChanceVowelExists)
{

// Then check if the vowel is a vowel sequence
if (this.VowelSequencesAllowed && _random.NextDouble() < this.Probability.ChanceVowelIsSequence)
if (this.VowelSequencesAllowed && this.Random.NextDouble() < this.Probability.ChanceVowelIsSequence)
{
output.Append(this.NextVowelSequence());
}
Expand All @@ -801,11 +803,11 @@ private string GenerateSyllable(SyllablePosition position)
// (as opposed to a 'trailing' consonant)
if(position == SyllablePosition.Ending
&& this.FinalConsonantsAllowed
&& _random.NextDouble() < this.Probability.ChanceFinalConsonantExists)
&& this.Random.NextDouble() < this.Probability.ChanceFinalConsonantExists)
{
// We need to append a final consonant,
// but we need to determine if the consonant is a sequence first
if (this.FinalConsonantSequencesAllowed && _random.NextDouble() < this.Probability.ChanceFinalConsonantIsSequence)
if (this.FinalConsonantSequencesAllowed && this.Random.NextDouble() < this.Probability.ChanceFinalConsonantIsSequence)
{
output.Append(this.NextFinalConsonantSequence());
}
Expand All @@ -815,11 +817,11 @@ private string GenerateSyllable(SyllablePosition position)
}
}
// Otherwise, roll for a trailing consonant
else if (this.TrailingConsonantsAllowed && _random.NextDouble() < this.Probability.ChanceTrailingConsonantExists)
else if (this.TrailingConsonantsAllowed && this.Random.NextDouble() < this.Probability.ChanceTrailingConsonantExists)
{
// If we need to append a trailing consonant, check if
// we need a sequence instead
if (this.TrailingConsonantSequencesAllowed && _random.NextDouble() < this.Probability.ChanceTrailingConsonantIsSequence)
if (this.TrailingConsonantSequencesAllowed && this.Random.NextDouble() < this.Probability.ChanceTrailingConsonantIsSequence)
{
output.Append(this.NextTrailingConsonantSequence());
}
Expand Down
17 changes: 11 additions & 6 deletions Syllabore/Syllabore/SyllableSet.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace Syllabore
{
Expand All @@ -18,8 +16,14 @@ namespace Syllabore
/// culture, historical period, etc.
/// </para>
/// </summary>
public class SyllableSet : ISyllableGenerator
public class SyllableSet : ISyllableGenerator, IRandomizable
{

/// <summary>
/// Used to simulate randomness during generation.
/// </summary>
public Random Random { get; set; }

/// <summary>
/// The syllable set size for starting syllables.
/// </summary>
Expand Down Expand Up @@ -72,6 +76,7 @@ public SyllableSet() : this(8, 8, 8)
/// </summary>
public SyllableSet(int startingSyllableCount, int middleSyllableCount, int endingSyllableCount)
{
this.Random = new Random();
this.StartingSyllables = new HashSet<string>();
this.MiddleSyllables = new HashSet<string>();
this.EndingSyllables = new HashSet<string>();
Expand Down Expand Up @@ -188,7 +193,7 @@ public string NextStartingSyllable()
}
}

return this.StartingSyllables.RandomItem<string>();
return this.StartingSyllables.RandomItem<string>(this.Random);
}

/// <summary>
Expand All @@ -205,7 +210,7 @@ public string NextSyllable()
}
}

return this.MiddleSyllables.RandomItem<string>();
return this.MiddleSyllables.RandomItem<string>(this.Random);
}

/// <summary>
Expand All @@ -221,7 +226,7 @@ public string NextEndingSyllable()
}
}

return this.EndingSyllables.RandomItem<string>();
return this.EndingSyllables.RandomItem<string>(this.Random);
}

}
Expand Down
13 changes: 7 additions & 6 deletions Syllabore/Syllabore/TransformSet.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Syllabore
Expand All @@ -22,9 +20,12 @@ namespace Syllabore
/// </para>
/// </summary>
[Serializable]
public class TransformSet : INameTransformer
public class TransformSet : INameTransformer, IRandomizable
{
private Random _random;
/// <summary>
/// Used to simulate randomness when <see cref="UseRandomSelection"/> is true.
/// </summary>
public Random Random { get; set; }

/// <summary>
/// The <see cref="Transform">Transforms</see> that make up this
Expand Down Expand Up @@ -56,7 +57,7 @@ public class TransformSet : INameTransformer
/// </summary>
public TransformSet()
{
_random = new Random();
this.Random = new Random();
this.Transforms = new List<Transform>();
this.UseRandomSelection = false;
this.RandomSelectionCount = 0;
Expand Down Expand Up @@ -91,7 +92,7 @@ private Name ApplyRandomTransforms(Name sourceName)

for (int i = 0; i < this.RandomSelectionCount && unusedTransforms.Count > 0; i++)
{
var transform = unusedTransforms.RandomWeightedItem<Transform>();
var transform = unusedTransforms.RandomWeightedItem<Transform>(this.Random);
unusedTransforms.Remove(transform);

if (this.CanTransformBeApplied(transform, sourceName))
Expand Down

0 comments on commit d954b1e

Please sign in to comment.