Ejemplo n.º 1
0
        public void Provider_WithCustomVowelProbability_AffectsNameGenerationSuccess()
        {
            // Defining at least one vowel sequence, but no individual vowels also shouldn't work if
            // the probability of vowel sequences is not set to 100%
            var provider  = new SyllableProvider().WithVowelSequences("aa");
            var generator = new NameGenerator().UsingProvider(provider);

            Assert.ThrowsException <InvalidOperationException>(() => // Some will succeed, but expecting some to fail
            {
                for (int i = 0; i < 1000; i++)
                {
                    generator.Next();
                }
            });

            // Defining at least one vowel sequence, set to 100% probability
            // without any possibility of vowels starting name shoudl work
            // provider.WithVowelSequenceProbability(1.0).WithStartingSyllableLeadingVowelSequenceProbability(1.0);
            //provider.WithProbability(x => x.OfVowelSequences(1.0).OfStartingSyllableLeadingVowelSequence(1.0));
            provider.WithProbability(vowelIsSequence: 1.0, vowelBeginsStartingSyllableAndIsSequence: 1.0);

            for (int i = 0; i < 1000; i++)
            {
                Assert.IsNotNull(generator.Next());
            }

            // The normal scenario where at least one individual vowel is defined
            //provider.WithVowels("a").WithProbability(x => x.OfVowelSequences(0));
            provider.WithVowels("a").WithProbability(vowelIsSequence: 0);

            for (int i = 0; i < 1000; i++)
            {
                Assert.IsNotNull(generator.Next());
            }
        }
Ejemplo n.º 2
0
        private bool EachOutputContainsAnyOf(SyllableProvider p, params string[] validSubstrings)
        {
            bool outputAlwaysAppears = true;

            for (int i = 0; i < 1000; i++)
            {
                var s = p.NextSyllable();
                if (!s.ContainsAny(validSubstrings))
                {
                    outputAlwaysAppears = false;
                    break;
                }
            }

            return(outputAlwaysAppears);
        }
Ejemplo n.º 3
0
        public void Provider_WithOnlyVowelsDefined_SyllablesGeneratedCorrectly()
        {
            // Define all vowels
            var provider = new SyllableProvider().WithVowels("aeiou");

            // Check that every syllable-generating method works and generates nothing but the vowels
            for (int i = 0; i < 1000; i++)
            {
                var startingSyllable = provider.NextStartingSyllable();
                var syllable         = provider.NextStartingSyllable();
                var endingSyllable   = provider.NextEndingSyllable();

                Assert.IsTrue(!string.IsNullOrEmpty(startingSyllable) && Regex.IsMatch(startingSyllable, "^[aeiouAEIOU]+$"));
                Assert.IsTrue(!string.IsNullOrEmpty(syllable) && Regex.IsMatch(syllable, "^[aeiouAEIOU]+$"));
                Assert.IsTrue(!string.IsNullOrEmpty(endingSyllable) && Regex.IsMatch(endingSyllable, "^[aeiouAEIOU]+$"));
            }
        }
Ejemplo n.º 4
0
        public void Provider_WithNoVowelsDefined_ProviderThrowsInvalidOperationException()
        {
            // Define everything except vowel and vowel sequences
            var provider = new SyllableProvider()
                           .WithLeadingConsonants("b")
                           .WithLeadingConsonantSequences("bb", "bbb")
                           .WithTrailingConsonants("b")
                           .WithTrailingConsonantSequences("bbbb", "bbbbb");

            // Syllables always require a vowel so syllable generation should fail
            for (int i = 0; i < 1000; i++)
            {
                Assert.ThrowsException <InvalidOperationException>(() => provider.NextStartingSyllable());
                Assert.ThrowsException <InvalidOperationException>(() => provider.NextSyllable());
                Assert.ThrowsException <InvalidOperationException>(() => provider.NextEndingSyllable());
            }
        }
Ejemplo n.º 5
0
        public void Provider_WithNoVowelsDefined_NameGeneratorThrowsInvalidOperationException()
        {
            // Same as Provider_WithNoVowelsDefined_ProviderThrowsInvalidOperationException
            // except it applies to a NameGenerator

            var provider = new SyllableProvider()
                           .WithLeadingConsonants("b")
                           .WithLeadingConsonantSequences("bb", "bbb")
                           .WithTrailingConsonants("b")
                           .WithTrailingConsonantSequences("bbbb", "bbbbb");

            var generator = new NameGenerator().UsingProvider(provider);

            for (int i = 0; i < 1000; i++)
            {
                Assert.ThrowsException <InvalidOperationException>(() => generator.Next());
            }
        }
Ejemplo n.º 6
0
        private bool AllOutputContainsAtLeastOnce(SyllableProvider p, params string[] validSubstrings)
        {
            bool[] substringAppeared = new bool[validSubstrings.Length];

            for (int i = 0; i < 1000; i++)
            {
                var s = p.NextSyllable();

                for (int j = 0; j < validSubstrings.Length; j++)
                {
                    if (s.Contains(validSubstrings[j]))
                    {
                        substringAppeared[j] = true;
                    }
                }
            }

            return(substringAppeared.All(x => x));
        }
Ejemplo n.º 7
0
        public void Provider_WhenCustomVowelsDefined_OnlyCustomVowelsAppearInProviderOutput()
        {
            // Define one and only one vowel then check name output
            var provider = new SyllableProvider()
                           .WithVowels("a")
                           .WithLeadingConsonants("bcdfg")
                           .WithTrailingConsonants("bcdfg");

            for (int i = 0; i < 1000; i++)
            {
                var startingSyllable = provider.NextStartingSyllable();
                var syllable         = provider.NextSyllable();
                var endingSyllable   = provider.NextEndingSyllable();
                Assert.IsTrue(startingSyllable.Contains("a"));
                Assert.IsTrue(syllable.Contains("a"));
                Assert.IsTrue(endingSyllable.Contains("a"));
                Assert.IsFalse(Regex.IsMatch(startingSyllable, "[e|i|o|u]"));
                Assert.IsFalse(Regex.IsMatch(syllable, "[e|i|o|u]"));
                Assert.IsFalse(Regex.IsMatch(endingSyllable, "[e|i|o|u]"));
            }

            // Change the one and only vowel and check name output again
            provider = new SyllableProvider()
                       .WithVowels("y")
                       .WithLeadingConsonants("bcdfg")
                       .WithTrailingConsonants("bcdfg");

            for (int i = 0; i < 1000; i++)
            {
                var startingSyllable = provider.NextStartingSyllable();
                var syllable         = provider.NextSyllable();
                var endingSyllable   = provider.NextEndingSyllable();
                Assert.IsTrue(startingSyllable.Contains("y"));
                Assert.IsTrue(syllable.Contains("y"));
                Assert.IsTrue(endingSyllable.Contains("y"));
                Assert.IsFalse(Regex.IsMatch(startingSyllable, "[a|e|i|o|u]"));
                Assert.IsFalse(Regex.IsMatch(syllable, "[a|e|i|o|u]"));
                Assert.IsFalse(Regex.IsMatch(endingSyllable, "[a|e|i|o|u]"));
            }
        }
Ejemplo n.º 8
0
        public static void Main(string[] args)
        {
            {
                // Quickest way to use Syllabore's name generator
                // without specifying any configuration. This instance
                // will default to using StandaloneSyllableProvider for
                // name generator and will not use any NameValidator to
                // improve output.
                var g = new NameGenerator();

                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(g.Next());
                }
            }
            {
                // Normally the constructor takes a SyllableProvider
                // and NameValidator. There are "Standalone" classes
                // available for quick and dirty use. It is recommended
                // you create your own by using ISyllableProvider/INameValidator
                // or inheriting from ConfigurableSyllableProvider/ConfigurableNameValidator.

                var provider  = new DefaultSyllableProvider();
                var validator = new NameValidator()
                                .DoNotAllowPattern(@"[j|p|q|w]$")             // Invalidate these awkward endings
                                .DoNotAllowPattern(@"(\w)\1\1")               // Invalidate any sequence of 3 or more identical letters
                                .DoNotAllowPattern(@"([^aeiouAEIOU])\1\1\1"); // Invalidate any sequence of 4 or more consonants

                var g = new NameGenerator(provider, validator);

                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(g.Next());
                }
            }
            {
                // You can choose to build name generators programmatically.
                var g = new NameGenerator()
                        .UsingProvider(x => x
                                       .WithLeadingConsonants("str")
                                       .WithVowels("ae"))
                        .LimitSyllableCount(3);

                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(g.Next());
                }

                Console.WriteLine();
            }

            {
                // Creating variations of a single name
                var g = new NameGenerator().UsingMutator(new VowelMutator());

                for (int i = 0; i < 3; i++)
                {
                    var name = g.NextName();
                    Console.WriteLine(name);

                    for (int j = 0; j < 4; j++)
                    {
                        var variation = g.Mutate(name);
                        Console.WriteLine(variation);
                    }
                }
            }

            {
                Console.WriteLine();
                var g = new NameGenerator()
                        .UsingProvider(p => p
                                       .WithVowels("aeoy")
                                       .WithLeadingConsonants("vstlr")
                                       .WithTrailingConsonants("zrt")
                                       .WithVowelSequences("ey", "ay", "oy"))
                        .UsingMutator(m => m
                                      .WithMutation(x => x.ReplaceSyllable(0, "Gran"))
                                      .WithMutation(x => x.ReplaceSyllable(0, "Bri"))
                                      .WithMutation(x => x.InsertSyllable(0, "Deu").AppendSyllable("gard").WithWeight(2))
                                      .WithMutation(x => x.When(-2, "[aeoyAEOY]$").ReplaceSyllable(-1, "opolis"))
                                      .WithMutation(x => x.When(-2, "[^aeoyAEOY]$").ReplaceSyllable(-1, "polis"))
                                      .WithMutationCount(1))
                        .UsingValidator(v => v
                                        .DoNotAllowPattern(
                                            @".{12,}",
                                            @"(\w)\1\1",             // Prevents any letter from occuring three times in a row
                                            @".*([y|Y]).*([y|Y]).*", // Prevents double y
                                            @".*([z|Z]).*([z|Z]).*", // Prevents double z
                                            @"(zs)",                 // Prevents "zs"
                                            @"(y[v|t])"))            // Prevents "yv" and "yt"
                        .LimitMutationChance(0.99)
                        .LimitSyllableCount(2, 4);

                ConfigurationFile.Save(g, "city-name-generator.txt");
                var g2 = ConfigurationFile.Load("city-name-generator.txt");

                for (int i = 0; i < 50; i++)
                {
                    var name = g.NextName();
                    Console.WriteLine(name);
                }

                Console.WriteLine();
            }
            {
                var provider = new SyllableProvider();
                provider.WithVowels("a", "e", "o", "y");
                provider.WithLeadingConsonants("v", "s", "t", "l", "r");
                provider.WithTrailingConsonants("z", "r", "t");
                provider.WithVowelSequences("ey", "ay", "oy");
                provider.DisallowLeadingConsonantSequences();
                provider.DisallowTrailingConsonantSequences();

                var shifter = new VowelMutator("a", "e", "o", "y");

                var validator = new NameValidator();
                validator.DoNotAllowPattern(@"(\w)\1\1");
                validator.DoNotAllowPattern(@"([^aeoyAEOY])\1");
                validator.DoNotAllowPattern(@".*([y|Y]).*([y|Y]).*");
                validator.DoNotAllowPattern(@".*([z|Z]).*([z|Z]).*");
                validator.DoNotAllowPattern(@"(zs)");
                validator.DoNotAllowPattern(@"(y[v|t])");

                var g = new NameGenerator(provider, shifter, validator);
                g.LimitSyllableCount(2, 3);
            }
            {
                var name    = new Name("syl", "la", "bore");
                var mutator = new NameMutator()
                              .Join(new DefaultNameMutator())
                              .Join(new VowelMutator());

                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine(mutator.Mutate(name));
                }
            }
        }
Ejemplo n.º 9
0
        public void Provider_CustomConsonantProbabilityDefined_AffectsSyllableGenerationCorrectly()
        {
            var provider = new SyllableProvider()
                           .WithVowels("a")
                           .WithVowelSequences("ee")
                           .WithLeadingConsonants("b")
                           .WithLeadingConsonantSequences("cc")
                           .WithTrailingConsonants("d")
                           .WithTrailingConsonantSequences("ff");

            // Disable all leading and trailing consonants
            Assert.IsTrue(EachOutputNeverContainsAnyOf(
                              provider.WithProbability(
                                  consonantBeginsSyllable: 0.0,
                                  consonantBeginsSyllableAndIsSequence: 0.0,
                                  consonantEndsSyllable: 0.0,
                                  consonantEndsSyllableAndIsSequence: 0.0),
                              "b", "cc", "d", "ff"));

            // Consonant sequence probability doesn't matter
            // if the consonant probability is zero
            Assert.IsTrue(EachOutputNeverContainsAnyOf(
                              provider.WithProbability(
                                  consonantBeginsSyllable: 0.0,
                                  consonantBeginsSyllableAndIsSequence: 1.0,
                                  consonantEndsSyllable: 0.0,
                                  consonantEndsSyllableAndIsSequence: 1.0),
                              "b", "cc", "d", "ff"));

            // Consonant sequence probability only matters
            // if the consonant probability is not zero

            provider.WithProbability(
                consonantBeginsSyllable: 1.0,
                consonantBeginsSyllableAndIsSequence: 0.0,
                consonantEndsSyllable: 1.0,
                consonantEndsSyllableAndIsSequence: 0.0);

            // There should be no consonant sequences
            Assert.IsTrue(EachOutputContainsAnyOf(provider, "b", "d"));
            Assert.IsTrue(EachOutputNeverContainsAnyOf(provider, "cc", "ff"));

            provider.WithProbability(
                consonantBeginsSyllable: 1.0,
                consonantBeginsSyllableAndIsSequence: 1.0,
                consonantEndsSyllable: 1.0,
                consonantEndsSyllableAndIsSequence: 1.0);

            // There should always be consonant sequences
            Assert.IsTrue(EachOutputNeverContainsAnyOf(provider, "b", "d"));
            Assert.IsTrue(EachOutputContainsAnyOf(provider, "cc", "ff"));

            // Test whether a value between 0 and 1 ouputs both consonants and consonant sequences
            provider.WithProbability(
                consonantBeginsSyllable: 1.0,
                consonantBeginsSyllableAndIsSequence: 0.5,
                consonantEndsSyllable: 1.0,
                consonantEndsSyllableAndIsSequence: 0.5);

            Assert.IsTrue(AllOutputContainsAtLeastOnce(provider, "b", "d", "cc", "ff"));

            // Not all output will have a consonant
            provider.WithProbability(
                consonantBeginsSyllable: 0.5,
                consonantBeginsSyllableAndIsSequence: 0.5,
                consonantEndsSyllable: 0.5,
                consonantEndsSyllableAndIsSequence: 0.5);

            Assert.IsTrue(AllOutputContainsAtLeastOnce(provider, "b", "d", "cc", "ff"));
        }
Ejemplo n.º 10
0
        public void Provider_WithCustomComponents_AllComponentsAppearInProviderOutput()
        {
            // In this test we define one instance of a vowel, vowel sequence
            // leading consonant, leading consonant sequence, trailing consonant
            // and trailing consonant sequence, then check that each instance occurs
            // at least once in the provider's syllable generation.

            var provider = new SyllableProvider()
                           .WithVowels("a")
                           .WithVowelSequences("ee")
                           .WithLeadingConsonants("b")
                           .WithLeadingConsonantSequences("cc")
                           .WithTrailingConsonants("d")
                           .WithTrailingConsonantSequences("ff")
                           .WithProbability(
                vowelIsSequence: 0.5,
                vowelBeginsStartingSyllable: 0.25,
                consonantBeginsSyllable: 0.50,
                consonantEndsSyllable: 0.50);

            /*
             * .WithProbability(y => y
             *  .OfVowelSequences(0.5)
             *  .OfStartingSyllableLeadingVowels(0.25)
             *  .OfLeadingConsonantSequences(0.50)
             *  .OfTrailingConsonants(0.50));
             */

            bool foundVowel                     = false;
            bool foundVowelSequence             = false;
            bool foundStartingConsonant         = false;
            bool foundStartingConsonantSequence = false;
            bool foundEndingConsonant           = false;
            bool foundEndingConsonantSequence   = false;

            // Starting syllables only
            for (int i = 0; i < 500; i++)
            {
                var syllable = provider.NextStartingSyllable();
                if (syllable.Contains("a"))
                {
                    foundVowel = true;
                }
                if (syllable.Contains("ee"))
                {
                    foundVowelSequence = true;
                }
                if (syllable.Contains("b"))
                {
                    foundStartingConsonant = true;
                }
                if (syllable.Contains("cc"))
                {
                    foundStartingConsonantSequence = true;
                }
                if (syllable.Contains("d"))
                {
                    foundEndingConsonant = true;
                }
                if (syllable.Contains("ff"))
                {
                    foundEndingConsonantSequence = true;
                }
            }

            Assert.IsTrue(foundVowel);
            Assert.IsTrue(foundVowelSequence);
            Assert.IsTrue(foundStartingConsonant);
            Assert.IsTrue(foundStartingConsonantSequence);
            Assert.IsTrue(foundEndingConsonant);
            Assert.IsTrue(foundEndingConsonantSequence);

            // Reset
            foundVowel                     = false;
            foundVowelSequence             = false;
            foundStartingConsonant         = false;
            foundStartingConsonantSequence = false;
            foundEndingConsonant           = false;
            foundEndingConsonantSequence   = false;

            // All syllables
            for (int i = 0; i < 500; i++)
            {
                var syllable = provider.NextSyllable();
                if (syllable.Contains("a"))
                {
                    foundVowel = true;
                }
                if (syllable.Contains("ee"))
                {
                    foundVowelSequence = true;
                }
                if (syllable.Contains("b"))
                {
                    foundStartingConsonant = true;
                }
                if (syllable.Contains("cc"))
                {
                    foundStartingConsonantSequence = true;
                }
                if (syllable.Contains("d"))
                {
                    foundEndingConsonant = true;
                }
                if (syllable.Contains("ff"))
                {
                    foundEndingConsonantSequence = true;
                }
            }

            Assert.IsTrue(foundVowel);
            Assert.IsTrue(foundVowelSequence);
            Assert.IsTrue(foundStartingConsonant);
            Assert.IsTrue(foundStartingConsonantSequence);
            Assert.IsTrue(foundEndingConsonant);
            Assert.IsTrue(foundEndingConsonantSequence);

            // Reset
            foundVowel                     = false;
            foundVowelSequence             = false;
            foundStartingConsonant         = false;
            foundStartingConsonantSequence = false;
            foundEndingConsonant           = false;
            foundEndingConsonantSequence   = false;

            // Ending syllables only
            for (int i = 0; i < 500; i++)
            {
                var syllable = provider.NextEndingSyllable();
                if (syllable.Contains("a"))
                {
                    foundVowel = true;
                }
                if (syllable.Contains("ee"))
                {
                    foundVowelSequence = true;
                }
                if (syllable.Contains("b"))
                {
                    foundStartingConsonant = true;
                }
                if (syllable.Contains("cc"))
                {
                    foundStartingConsonantSequence = true;
                }
                if (syllable.Contains("d"))
                {
                    foundEndingConsonant = true;
                }
                if (syllable.Contains("ff"))
                {
                    foundEndingConsonantSequence = true;
                }
            }

            Assert.IsTrue(foundVowel);
            Assert.IsTrue(foundVowelSequence);
            Assert.IsTrue(foundStartingConsonant);
            Assert.IsTrue(foundStartingConsonantSequence);
            Assert.IsTrue(foundEndingConsonant);
            Assert.IsTrue(foundEndingConsonantSequence);
        }