createSentence function

String createSentence({
  1. int sentenceLength = -1,
  2. int numSentences = 1,
})

Creates random sentences.

Sentences are either exactly sentenceLength words in length, or a randomly generated length. numSentences defines the number of sentences generated. Returned sentences are punctuated.

Implementation

String createSentence({int sentenceLength = -1, int numSentences = 1}) {
  int wordIndex;
  String sentence;

  if (numSentences > 1) return createParagraph(numSentences: numSentences);

  if (sentenceLength < 0) {
    sentenceLength = randomInt(5, 20);
  }

  wordIndex = randomInt(0, words.length - sentenceLength - 1);
  sentence = words.getRange(wordIndex, wordIndex + sentenceLength).join(" ");
  sentence = "${sentence[0].toUpperCase()}${sentence.substring(1)}.";

  return sentence;
}