-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTextGenerator.cs
96 lines (87 loc) · 3.69 KB
/
TextGenerator.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
89
90
91
92
93
94
95
96
using BytecodeApi.Mathematics;
using System.Text;
namespace BytecodeApi.LanguageGenerator;
/// <summary>
/// Class that generates a random text sequence that match the pattern of real language.
/// A <see cref="LanguageGenerator.SentenceGenerator" /> is used to generate the sentences that generated text are composed of.
/// </summary>
public class TextGenerator : ILanguageStringGenerator
{
/// <summary>
/// Gets or sets the <see cref="LanguageGenerator.SentenceGenerator" /> that is used to generate sentences.
/// </summary>
public SentenceGenerator SentenceGenerator { get; set; }
/// <summary>
/// Gets or sets the minimum number of sentences used to build text.
/// <para>The default value is 10</para>
/// </summary>
public int MinSentenceCount { get; set; }
/// <summary>
/// Gets or sets the maximum number of sentences used to build text.
/// <para>The default value is 20</para>
/// </summary>
public int MaxSentenceCount { get; set; }
/// <summary>
/// Gets or sets a <see cref="double" /> value that specifies the chance of a linebreak being inserted after a sentence, where 0.0 means no linebreaks and 1.0 means a linebreak between every sentence.
/// <para>The default value is 0.0</para>
/// </summary>
public double LineBreakChance { get; set; }
/// <summary>
/// Gets or sets a <see cref="double" /> value that specifies the chance of a paragraph being inserted after a sentence, where 0.0 means no paragraphs and 1.0 means a paragraph between every sentence. Randomly picked paragraphs have precedence over linebreaks and do not occur consecutively.
/// <para>The default value is 0.1</para>
/// </summary>
public double ParagraphChance { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="TextGenerator" /> class with default values.
/// </summary>
public TextGenerator() : this(new())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TextGenerator" /> class with the specified <see cref="LanguageGenerator.SentenceGenerator" /> and with default values (<see cref="MinSentenceCount" /> = 10, <see cref="MaxSentenceCount" /> = 20, <see cref="LineBreakChance" /> = 0.0, <see cref="ParagraphChance" /> = 0.1).
/// </summary>
/// <param name="sentenceGenerator">The <see cref="LanguageGenerator.SentenceGenerator" /> to use for sentence generation.</param>
public TextGenerator(SentenceGenerator sentenceGenerator)
{
Check.ArgumentNull(sentenceGenerator);
SentenceGenerator = sentenceGenerator;
MinSentenceCount = 10;
MaxSentenceCount = 20;
LineBreakChance = 0;
ParagraphChance = .1;
}
/// <summary>
/// Generates a random sequence of sentences using the specified parameters of this <see cref="TextGenerator" /> instance.
/// </summary>
/// <returns>
/// A new <see cref="string" /> with dynamically generated text.
/// </returns>
public string Generate()
{
Check.ArgumentNull(SentenceGenerator);
Check.ArgumentOutOfRangeEx.GreaterEqual0(MinSentenceCount);
Check.ArgumentOutOfRangeEx.GreaterEqual0(MaxSentenceCount);
Check.ArgumentOutOfRangeEx.GreaterEqualValue(MaxSentenceCount, MinSentenceCount);
Check.ArgumentOutOfRangeEx.Between0And1(LineBreakChance);
Check.ArgumentOutOfRangeEx.Between0And1(ParagraphChance);
StringBuilder stringBuilder = new();
int sentences = MathEx.Random.Next(MinSentenceCount, MaxSentenceCount + 1);
for (int i = 0; i < sentences; i++)
{
stringBuilder.Append(SentenceGenerator.Generate());
if (MathEx.Random.NextDouble() < ParagraphChance)
{
stringBuilder.AppendLine().AppendLine();
}
else if (MathEx.Random.NextDouble() < LineBreakChance)
{
stringBuilder.AppendLine();
}
else
{
stringBuilder.Append(' ');
}
}
return stringBuilder.ToString().Trim();
}
}