Jump to content

Markov chain text generator: Difference between revisions

Added Perl example
(Add Swift)
(Added Perl example)
Line 733:
she comes, to-' At this moment Five, who had been greatly interested in
</pre>
 
=={{header|Perl}}==
<lang perl>$file = shift || 'alice_oz.txt';
$n = shift || 3;
$max = shift || 200;
 
sub build_dict {
my($n, @words) = @_;
my %dict;
for $i (0 .. @words-$n) {
my @prefix;
push @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>; close F;
 
my @words = split ' ', $text;
%dict = build_dict($n, @words);
 
print join ' ', @rotor = @words[0..$n-1];
for (1..$max) {
print ' ' . ($new = pick1( @{$dict{join ' ', @rotor}} ));
shift @rotor;
push @rotor, $new;
}</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>
 
=={{header|Perl 6}}==
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.