#!/usr/bin/perl use strict; use warnings; use v5.10; my @sentences; while(<>) { chomp; my @sentence_frags = grep /\S/, split /[.?!]/; # $sentences[-1] .= shift @sentence_frags # if @sentences && @sentence_frags; push @sentences, @sentence_frags; } my %words; for (@sentences) { my @words = grep /\S/, split /\W/; my $last = ''; for my $word (@words) { $word = lc $word; $words{$last}{$word}++; $last = $word; } $words{$last}{''}++; } #say for @sentences; #use Data::Dumper; #print Dumper %words; for my $si (0..10) { my $word = ''; while(1) { my $next = $words{$word}; my @next2 = map { ($_) x $next->{$_} } keys %$next; $word = $next2[int rand @next2]; last if $word eq ''; print "$word "; } print "\n"; }