Markov chain text generator: Difference between revisions

m
→‎{{header|Perl}}: code improvements and strict compliance
(Added Sidef)
m (→‎{{header|Perl}}: code improvements and strict compliance)
Line 847:
 
=={{header|Perl}}==
<lang perl>$fileuse = shift || 'alice_oz.txt'strict;
use warnings;
$n = shift || 3;
 
$max = shift || 200;
my $file = shift || 'alice_oz.txt';
my $n = shift || 3;
my $max = shift || 200;
 
sub build_dict {
my ($n, @words) = @_;
my %dict;
for my $i (0 .. @words - $n) {
my @prefix = @words[$i .. $i+$n-1];
push @{$dict{join ' ', @prefix}}, $words[$i+$_] for 0..$n-1];
push @{$dict{ join ' ', @prefix }}, $words[$i+$n];
}
return %dict;
}
 
sub pick1 { return @$_[ rand @_ ] }
 
open F, "<$file"; my $text = <F>; closedo F;{
open my $fh, '<', $file;
local $/;
<$fh>;
};
 
my @words = split ' ', $text;
my %dict = build_dict($n, @words);
print join ' ',my @rotor = @words[0 .. $n - 1];
my @chain = @rotor;
 
for (1 .. $max) {
print join ' ', @rotor = @words[0..$n-1];
my $new = push pick1(@{$dict{ join ' ', @prefix rotor}}, $words[$i+$n]);
for (1..$max) {
print ' ' . ($new = pick1( @{$dict{join ' ', @rotor}} ));
shift @rotor;
push @rotor, $new;
push @chain, $new;
}</lang>
}
 
print join(' ', @chain) . "\n";</lang>
{{out}}
<pre>Alice was thoroughly puzzled. 'Does the boots and shoes,' the Gryphon whispered in a fight with another hedgehog, which seemed to extend to the South Country? To see the Great Oz was ready to sink into the garden, where Alice could see or feel was the end of you, as she chose, she ran after her. 'I've something important to say!' This sounded promising, certainly: Alice turned and walked through the forest very thick on this side, and it seemed to Alice as she chose, she ran out and shone brightly. So they all spoke at once, I'll chop it down, and the Dormouse sulkily remarked, 'If you please, sir-' The Rabbit started violently, dropped the white kid gloves and the four travelers walked up to the Land of Oz in less than no time to think about stopping herself before she made a dreadfully ugly child: but it is a man,' said the Stork, as she spoke. 'I must be a person of authority among them, called out, 'Sit down, all of them expected to come out of breath, and till the Pigeon in a sorrowful tone; 'at least there's no use to me they flew away with me,' thought Alice,</pre>
2,747

edits