Some times when I am testing applications I have need random sentences or text files to be generated on the fly of various lengths. This post will show how one way to do that.
Generating the Sentences
To generate a sentence the following steps need to be taken.
- Split a list of words
- Take a word at random from the list and put in collection.
- Repeat step 2 until you have enough words for you sentence.
- Repeat steps 2 and 3 for number of sentences and paragraphs that you require.
A more detailed description generating test sentences can be found at http://www.dotnetperls.com/random-paragraphs-sentences
I have published a package on NUGET for ease of use at https://www.nuget.org/packages/System.Diagnostics.TestData/
var target = new RandomText(); const int numberParagraphs = 1; const int minSentences = 4; const int maxSentences = minSentences; const int minWords = 4; const int maxWords = minWords; target.AddContentParagraphs(numberParagraphs, minSentences, maxSentences, minWords, maxWords); //Get your randomly generated phrase from here string randomPhrase = target.Content;
http://www.dotnetperls.com/random-paragraphs-sentences – where the core part of the code came from.