C# Random Paragraphs

Random

Random paragraphs and sentences can be generated. The text must look like regular English writing. It is called word salad. An algorithm can generate language that appears natural. This is for testing or for spam detection.

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.
For the program here,
I use simple words
and few of them.

ConstructorString Array

This example will generate two paragraphs that contain between two and four sentences. Each sentence will contain between 5 and 12 words. This is the random output it generated for me.

C# program that creates random text

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);
    }
}

Output

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.

Discussion

Squares

The required output here is similar to spamming programs, but it is entirely implemented in C# and simple for you to use. This code is not for spamming but for generating tests for your software.

Paragraphs. The class outputs text that has a certain number of paragraphs, separated by newlines. Within those paragraphs, there are random numbers of sentences. And within those sentences, there is a random number of words.

How to specify the length. The code accepts a first parameter that indicates the desired number of paragraphs. The next two int parameters indicate the minimum number of sentences and the maximum number of sentences in the paragraphs.

And:The final two parameters specify the maximum number and minimum number of words in each sentence.

Example 2

Note

Here I show the class that contains the logic for the random text generator. You can see that it has an AddContentParagraphs method. And you can fetch the resulting string from the Content property.

Property

We specify the options first in the constructor, which accepts the list of words to use. Your program could extract these words from a web page or database. The exact words are not important.

Class for generating text: C#

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();
	}
    }
}
Class shapes

AddContentParagraphs. This is where you must specify the numeric ranges for your desired output. Internally, it uses the Random instance's Next method, and then adds the paragraph. After the paragraph, it inserts two line breaks.

RandomStringBuilder

Private methods:The two private methods in the class simply add a variable number of sentences and then the sentence itself.

PrivateLetters of the alphabet: ABC

Sentence logic. The AddSentence method is the most interesting. It loops through the number of words you want to generate. It randomly selects each word from the words array you specified. 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.ToUpper

Summary

We generated random text with the C# language. The class uses the object-oriented design methodology and outputs variable-length sentences and paragraphs. You can specify any number of possible words to use.


C#: Method