/// <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, nameof(WordGenerator)); Check.ArgumentOutOfRangeEx.GreaterEqual0(MinWords, nameof(MinWords)); Check.ArgumentOutOfRangeEx.GreaterEqual0(MaxWords, nameof(MaxWords)); Check.ArgumentOutOfRangeEx.GreaterEqualValue(MaxWords, MinWords, nameof(MaxWords), nameof(MinWords)); Check.ArgumentOutOfRangeEx.Between0And1(CommaChance, nameof(CommaChance)); Check.ArgumentNull(FinishPunctuation, nameof(FinishPunctuation)); Check.ArgumentEx.ArrayElementsRequired(FinishPunctuation, nameof(FinishPunctuation)); StringBuilder stringBuilder = new StringBuilder(); lock (MathEx._Random) { int words = MathEx._Random.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 (MathEx._Random.NextSingle() < CommaChance) { stringBuilder.Append(","); } stringBuilder.Append(" "); } } return(stringBuilder.Append(MathEx._Random.NextObject(FinishPunctuation)).ToString()); } }
/// <summary> /// Initializes a new instance of the <see cref="SentenceGenerator" /> class with the specified <see cref="WordGenerator" /> and with default values (<see cref="MinWords" /> = 3, <see cref="MaxWords" /> = 10, <see cref="CommaChance" /> = 0.2f, <see cref="FinishPunctuation" /> = "...?!"). /// </summary> /// <param name="wordGenerator">The <see cref="WordGenerator" /> to use for word generation.</param> public SentenceGenerator(WordGenerator wordGenerator) { Check.ArgumentNull(wordGenerator, nameof(wordGenerator)); WordGenerator = wordGenerator; MinWords = 3; MaxWords = 10; CommaChance = .2f; FinishPunctuation = "...?!".ToCharArray(); }