#######################################################################################
#
# generator.pl (Description: markovises input text using two word prefixes.)
# Copyright (C) 2009 Wayne Clements
#
# The Markov Algorithm used below is Copyright (C) 1999 Lucent Technologies
# Excerpted from 'The Practice of Programming'
# by Brian W. Kernighan and Rob Pike.
#
# This program (but not the Markov Algorithm as stated above) is free software:
# you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# See .
#
# The program is presented as it is on my website.
# The html and links are as found in www.in-vacua.com. You can change this as necessary.
# Wayne Clements, www.in-vacua.com. email: invacua_at__btinternet[Do T]com
#
########################################################################################
#!/usr/bin/perl - wT
use strict;
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);
print "Content-type: text/html\n\n";
my ($MAXGEN, $NONWORD, $w1, $w2, $suf, $statetab, $r, $t, $i, %statetab, @array);
open(FILE1, "chapter1.txt")|| die; # This is the input text. You will create your own.
open(FILE2, ">chapter2.txt")|| die;
@array = ;
shuffle(\@array);
my $draw= join ' ', @array;
print FILE2 $draw;
close FILE1;
close FILE2;
open(FILE2, "chapter2.txt")|| die;
srand;
my $rand = int(rand 60) + 2;
my $html = "
markov text
home reload
";
$MAXGEN = 10000;
$NONWORD = "\n";
$w1 = $w2 = $NONWORD; # initial state
while () { # read each line of input
foreach (split) {
push(@{$statetab{$w1}{$w2}}, $_);
($w1, $w2) = ($w2, $_); # multiple assignment
}
}
push(@{$statetab{$w1}{$w2}}, $NONWORD); # add tail
print $html;
$w1 = $w2 = $NONWORD;
for ($i = 0; $i < $MAXGEN; $i++) {
$suf = $statetab{$w1}{$w2}; # array reference
$r = int(rand @$suf); # @$suf is number of elems
exit if (($t = $suf->[$r]) eq $NONWORD);
print ' '. $t; #concatenation with white space, Wayne's contribution
($w1, $w2) = ($w2, $t); # advance chain
}
close FILE2;
# fisher yates shuffle
sub shuffle {
my($array) = shift();
for (my $i = @$array; --$i; ) {
my($j) = int(rand($i + 1));
next() if ($i == $j);
@$array[$i, $j] = @$array[$j, $i];
}
} #EOSub