-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathWordGenerator.cs
88 lines (79 loc) · 3.24 KB
/
WordGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using BytecodeApi.Extensions;
using BytecodeApi.Mathematics;
using BytecodeApi.Text;
namespace BytecodeApi.LanguageGenerator;
/// <summary>
/// Class that generates random words that match the pattern of real language.
/// Word generation is typically used to create arbitrary text that looks like language.
/// </summary>
public class WordGenerator : ILanguageStringGenerator
{
/// <summary>
/// Gets or sets the minimum length of a generated word.
/// <para>The default value is 3</para>
/// </summary>
public int MinLength { get; set; }
/// <summary>
/// Gets or sets the maximum length of a generated word.
/// <para>The default value is 10</para>
/// </summary>
public int MaxLength { get; set; }
/// <summary>
/// Gets or sets a <see cref="double" /> value that specifies the chance of a consonant being inserted twice, where 0.0 means no double consonants and 1.0 means consonants are always inserted twice.
/// <para>The default value is 0.1f</para>
/// </summary>
public double DoubleConsonantChance { get; set; }
/// <summary>
/// Gets or sets a <see cref="double" /> value that specifies the chance of a vovel being inserted twice, where 0.0 means no double vovels and 1.0 means vovels are always inserted twice.
/// <para>The default value is 0.1</para>
/// </summary>
public double DoubleVovelChance { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="WordGenerator" /> class with default values.
/// </summary>
public WordGenerator()
{
MinLength = 3;
MaxLength = 10;
DoubleConsonantChance = .1;
DoubleVovelChance = .1;
}
/// <summary>
/// Generates a random word in camel case character casing using the specified parameters of this <see cref="WordGenerator" /> instance.
/// </summary>
/// <returns>
/// A new <see cref="string" /> with a dynamically generated word in camel case character casing.
/// </returns>
public string Generate()
{
return Generate(StringCasing.CamelCase);
}
/// <summary>
/// Generates a random word in the specified <see cref="StringCasing" /> using the specified parameters of this <see cref="WordGenerator" /> instance.
/// </summary>
/// <param name="casing">The <see cref="StringCasing" /> to be used for characters in the generated word.</param>
/// <returns>
/// A new <see cref="string" /> with a dynamically generated word in the specified <see cref="StringCasing" />.
/// </returns>
public string Generate(StringCasing casing)
{
Check.ArgumentOutOfRangeEx.GreaterEqual0(MinLength);
Check.ArgumentOutOfRangeEx.GreaterEqual0(MaxLength);
Check.ArgumentOutOfRangeEx.GreaterEqualValue(MaxLength, MinLength);
Check.ArgumentOutOfRangeEx.Between0And1(DoubleConsonantChance);
Check.ArgumentOutOfRangeEx.Between0And1(DoubleVovelChance);
string word = "";
int length;
length = MathEx.Random.Next(MinLength, MaxLength + 1);
bool consonant = MathEx.Random.NextBoolean();
while (word.Length < length)
{
string charset = consonant ? TextResources.Consonants : TextResources.Vovels;
double chance = consonant ? DoubleConsonantChance : DoubleVovelChance;
consonant = !consonant;
char c = MathEx.Random.NextObject(charset.ToCharArray());
word += c.Repeat(MathEx.Random.NextDouble() < chance ? 2 : 1);
}
return word[..length].ChangeCasing(casing);
}
}