Here's a bit of random nonsense. Create a random number of lines with a random number of random words from a dictionary. Repeated words are acceptable.

use strict; use warnings; my @words = <DATA>; chomp @words; my $number_of_sentences = 3 + int( 10*rand() ); for (1..$number_of_sentences) { my $sentence_length = 2 + int( 9*rand() ); my @sentence = map {$words[int(rand($#words+1))]} 1..$sentence_len +gth; $sentence[0] = ucfirst $sentence[0]; print join(" ", @sentence), ".\n\n"; } __DATA__ abnormal blah crazy dolt dolthead doltish doltishly doltishness eccentric fallacious galling hapless illogical jabber kooky lame misguided nuisance officious pretense questionable resentful shaky tenuous untenable vague warp yawn zombie

The output from a run was:

Doltishly yawn yawn. Shaky warp yawn tenuous misguided illogical doltish. Warp blah officious kooky dolthead untenable eccentric untenable offic +ious. Galling officious. Abnormal doltish hapless tenuous. Galling crazy. Abnormal galling doltish. Kooky crazy doltishly doltishness. Eccentric blah fallacious galling. Pretense untenable questionable abnormal kooky zombie dolt jabber.

Replies are listed 'Best First'.
Re: Creating random sentences from a dictionary
by tybalt89 (Monsignor) on Sep 04, 2018 at 22:28 UTC

    Add parts of speech, it makes it more interesting.

    #!/usr/bin/perl # https://perlmonks.org/?node_id=1221711 use strict; use warnings; my $pattern = "art adj{0,2} noun adverb{0,1} verb art adj{0,2} noun.\n +"; my %words; while( <DATA> ) { my ($word, $part) = /(\S+)\s+(\S+)/ or next; push $words{$part}->@*, $word; } for (1 .. 3 + rand 10) { print ucfirst $pattern =~ s/(\w+)(?:\{(\d+),(\d+)\})?/ my @avail = $words{$1}->@*; join ' ', map { $avail[rand @avail] } $2 ? 1 .. $2 + int $3 - $2 +: 1 /ger } __DATA__ abnormal adj blah adj crazy adj dolt noun dolthead noun doltish adj doltishly adverb doltishness adj eccentric adj fallacious adj galling adj hapless adj illogical adj jabber verb kooky adj lame adj misguided adj nuisance noun officious adj pretense noun questionable adj resentful adj shaky adj tenuous adj the art a art untenable adj vague adj warp verb yawn verb zombie noun

    Sample Output

    A misguided pretense doltishly jabber a fallacious dolthead. The crazy zombie doltishly yawn a kooky nuisance. The misguided dolthead doltishly warp a doltishness dolt. A kooky nuisance doltishly jabber a shaky zombie. A lame dolt doltishly yawn a vague zombie. The crazy dolt doltishly jabber the abnormal zombie. The resentful dolt doltishly warp a lame zombie. A crazy nuisance doltishly warp a illogical dolthead. A questionable dolthead doltishly jabber the abnormal dolthead. The illogical dolthead doltishly yawn the hapless nuisance. The galling dolthead doltishly warp the lame nuisance. A lame zombie doltishly warp the officious dolthead.

      Ai you need more adverbs

Re: Creating random sentences from a dictionary
by zentara (Cardinal) on Sep 05, 2018 at 13:36 UTC
    Hi, a bit of an upgrade to tybalt89's script...... added audio output with the flite speech synthesizer. It could almost be used to generate political speeches. :-) The padsp program is PulseAudio's utility to allow access to /dev/dsp. If your system dosn't use PulseAudio, you don't need it.
    #!/usr/bin/perl # adapted from https://perlmonks.org/?node_id=1221711 use strict; use warnings; # padsp is the PulseAudio /dev/dsp utility, # you may not need it on your system my $cmd = "padsp flite"; my $pid = open( OH, "| $cmd 2>&1"); my $pattern = "art adj{0,2} noun adverb{0,1} verb art adj{0,2} noun.\n +"; my %words; while( <DATA> ) { my ($word, $part) = /(\S+)\s+(\S+)/ or next; push $words{$part}->@*, $word; } for (1 .. 3 + rand 10) { print OH ucfirst $pattern =~ s/(\w+)(?:\{(\d+),(\d+)\})?/ my @avail = $words{$1}->@*; join ' ', map { $avail[rand @avail] } $2 ? 1 .. $2 + int $3 - $2 +: 1 /ger } __DATA__ abnormal adj blah adj crazy adj dolt noun dolthead noun doltish adj doltishly adverb doltishness adj eccentric adj fallacious adj galling adj hapless adj illogical adj jabber verb kooky adj lame adj misguided adj nuisance noun officious adj pretense noun questionable adj resentful adj shaky adj tenuous adj the art a art untenable adj vague adj warp verb yawn verb zombie noun

    I'm not really a human, but I play one on earth. ..... an animated JAPH

      this is what i call opening a door, cool!

      Cool...

Re: Creating random sentences from a dictionary
by bliako (Abbot) on Sep 05, 2018 at 01:24 UTC

    I was always fascinated by sorting words in a dictionary starting from their end. That would be the ultimate poet's companion. Alas, pronounciation and spelling are often very different.

    The following sentences were some of the 100 produced after reading Shelley's Frankestein. Thank you Shelley...

    Victim Prometheus Petersburgh dark
    Doubt listlessly fruitlessly grievously paroxysm streams dreams
    Corpse view hardly Henry
    Sex forget universal dirt
    Grey dungeon peaks Even even heaven
    Uri persuade made traveller dread instead read
    Devil sobs papa music
    Mantel autumn Autumn piece sympathy
    Lie say Greece plan
    Delirium Turkey occur free degree
    ...
    Waves branch French entrench Greek doubt
    

    The recipe for the above is:

    read a text or a dictionary find each word and filter out rubbish (numbers, initials, etc) enter the words in a hash keyed on their last N chars - an attempt to rhyme. TeX::Hyphen hyphenates the words so it's better but misses out a lot of words. create a pattern for the sentence, (0,1,0,0,1,0) meaning: pick random word for first word, second word change the ending, (1) so pick up a random word with different ending. 3rd word continue with same ending as previous (0) etc.

    and the code is here (too lazy I am to tidy it up)

Re: Creating random sentences from a dictionary
by Your Mother (Archbishop) on Sep 05, 2018 at 07:42 UTC