-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSentenceGenerator.cs
More file actions
93 lines (84 loc) · 3.65 KB
/
Copy pathSentenceGenerator.cs
File metadata and controls
93 lines (84 loc) · 3.65 KB
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
89
90
91
92
93
using BytecodeApi.Extensions;
using System.Text;
namespace BytecodeApi.LanguageGenerator;
/// <summary>
/// Class that generates random sentences that match the pattern of real language.
/// A <see cref="LanguageGenerator.WordGenerator" /> is used to generate the words that generated sentences are composed of.
/// </summary>
public class SentenceGenerator : ILanguageStringGenerator
{
/// <summary>
/// Gets or sets the <see cref="LanguageGenerator.WordGenerator" /> that is used to generate words.
/// </summary>
public WordGenerator WordGenerator { get; set; }
/// <summary>
/// Gets or sets the minimum number of words used to build sentences.
/// <para>The default value is 3</para>
/// </summary>
public int MinWords { get; set; }
/// <summary>
/// Gets or sets the maximum number of words used to build sentences.
/// <para>The default value is 10</para>
/// </summary>
public int MaxWords { get; set; }
/// <summary>
/// Gets or sets a <see cref="double" /> value that specifies the chance of a comma being inserted after a word, where 0.0 means no commas and 1.0 means a comma between every word.
/// <para>The default value is 0.2</para>
/// </summary>
public double CommaChance { get; set; }
/// <summary>
/// Gets or sets the characters that are used for punctuation after a sentence. Including a character multiple times increases the chance for this character. For example, this can be used to increase the likelihood of a period over a question mark or exclamation mark. Character order is not relevant.
/// <para>The default value is "...?!"</para>
/// </summary>
public char[] FinishPunctuation { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="SentenceGenerator" /> class with default values.
/// </summary>
public SentenceGenerator() : this(new())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SentenceGenerator" /> class with the specified <see cref="LanguageGenerator.WordGenerator" /> and with default values (<see cref="MinWords" /> = 3, <see cref="MaxWords" /> = 10, <see cref="CommaChance" /> = 0.2, <see cref="FinishPunctuation" /> = "...?!").
/// </summary>
/// <param name="wordGenerator">The <see cref="WordGenerator" /> to use for word generation.</param>
public SentenceGenerator(WordGenerator wordGenerator)
{
Check.ArgumentNull(wordGenerator);
WordGenerator = wordGenerator;
MinWords = 3;
MaxWords = 10;
CommaChance = .2;
FinishPunctuation = "...?!".ToCharArray();
}
/// <summary>
/// Generates a random sentence using the specified parameters of this <see cref="SentenceGenerator" /> instance.
/// </summary>
/// <returns>
/// A new <see cref="string" /> with a dynamically generated sentence.
/// </returns>
public string Generate()
{
Check.ArgumentNull(WordGenerator);
Check.ArgumentOutOfRangeEx.GreaterEqual0(MinWords);
Check.ArgumentOutOfRangeEx.GreaterEqual0(MaxWords);
Check.ArgumentOutOfRangeEx.GreaterEqualValue(MaxWords, MinWords);
Check.ArgumentOutOfRangeEx.Between0And1(CommaChance);
Check.ArgumentNull(FinishPunctuation);
Check.ArgumentEx.ArrayElementsRequired(FinishPunctuation);
StringBuilder stringBuilder = new();
int words = Random.Shared.Next(MinWords, MaxWords + 1);
for (int i = 0; i < words; i++)
{
stringBuilder.Append(WordGenerator.Generate(i == 0 ? StringCasing.CamelCase : StringCasing.Lower));
if (i < words - 1)
{
if (Random.Shared.NextDouble() < CommaChance)
{
stringBuilder.Append(',');
}
stringBuilder.Append(' ');
}
}
return stringBuilder.Append(Random.Shared.GetObject(FinishPunctuation)).ToString();
}
}