Home
C#
Random Paragraphs and Sentences
This page was last reviewed on Dec 22, 2023.
Dot Net Perls
Random paragraphs. It is possible to generate random paragraphs and sentences. The text must look like regular English writing. A C# method can generate language that appears natural.
Some uses. This code can be used for testing or for spam detection. It is also an interesting exercise in how to create random sequences of data.
Example. First, you must create the class instance with the constructor, which accepts the list of strings that comprise the words you want to insert into the text.
Constructor
Array
Here This example will generate two paragraphs that contain between 2 and 4 sentences. Each sentence will contain between 5 and 12 words.
Detail The class outputs text that has a certain number of paragraphs, separated by newlines.
And Within those paragraphs, there are random numbers of sentences. Within those sentences, there is a random number of words.
using System; class Program { static void Main() { string[] words = { "anemone", "wagstaff", "man", "the", "for", "and", "a", "with", "bird", "fox" }; RandomText text = new RandomText(words); text.AddContentParagraphs(2, 2, 4, 5, 12); string content = text.Content; Console.WriteLine(content); } }
And the a anemone for bird. Anemone with man and man the and with fox fox the. With the for man the. Anemone with the a wagstaff and man for fox. A anemone a bird anemone anemone anemone bird wagstaff a bird.
Example 2. This class contains the logic for the random text generator. It has an AddContentParagraphs method. And you can fetch the resulting string from the Content property.
Property
Detail We specify the options first in the constructor, which accepts the list of words to use.
Detail This uses the Random instance's Next method. It adds the paragraph. After the paragraph, it inserts 2 line breaks.
Random
StringBuilder
Detail The 2 private methods in the class simply add a variable number of sentences and then the sentence itself.
public, private
public class RandomText { static Random _random = new Random(); StringBuilder _builder; string[] _words; public RandomText(string[] words) { _builder = new StringBuilder(); _words = words; } public void AddContentParagraphs(int numberParagraphs, int minSentences, int maxSentences, int minWords, int maxWords) { for (int i = 0; i < numberParagraphs; i++) { AddParagraph(_random.Next(minSentences, maxSentences + 1), minWords, maxWords); _builder.Append("\n\n"); } } void AddParagraph(int numberSentences, int minWords, int maxWords) { for (int i = 0; i < numberSentences; i++) { int count = _random.Next(minWords, maxWords + 1); AddSentence(count); } } void AddSentence(int numberWords) { StringBuilder b = new StringBuilder(); // Add n words together. for (int i = 0; i < numberWords; i++) // Number of words { b.Append(_words[_random.Next(_words.Length)]).Append(" "); } string sentence = b.ToString().Trim() + ". "; // Uppercase sentence sentence = char.ToUpper(sentence[0]) + sentence.Substring(1); // Add this sentence to the class _builder.Append(sentence); } public string Content { get { return _builder.ToString(); } } }
AddSentence. This loops through the number of words you want to generate. It randomly selects each word from the words array. It appends a space after each word.
StringBuilder Append
Finally The AddSentence method trims the output and adds a period at the end of your sentence.
And It uses the char.ToUpper method to capitalize the first letter of the sentence. It appends the sentence to the internal buffer.
char.ToLower, ToUpper
A summary. We generated random text. The class uses object-oriented design methodology and outputs variable-length sentences and paragraphs. You can specify any number of possible words.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 22, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.