Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
#!/usr/bin/perl -w # # based on kernighan and pike's markov chain program # in _the practice of programming_, chapter 3 # (http://cm.bell-labs.com/cm/cs/tpop/markov.pl) # # Usage: markov.pl [prefix length] [max words to print] <infile use strict; my @words; # a line of text my %wordlist; # key: prefix, value: array of suffixes my $pref_len = shift @ARGV || 2; my $maxwords = shift @ARGV || 10000; # build word list # # 'Blessed is the man that walketh not in the counsel' # %wordlist = ( 'blessed is' => [ 'the', ], # 'is the' => [ 'man', ], # 'the man' => [ 'that', ], # ); while (<>) { push @words, split; while ( @words > $pref_len ) { # build prefix of $pref_len words # join(' ', @array) is faster than qq(@array) or "@array" # my $pref = join(' ', @words[0..($pref_len-1)]); # add suffix to list # push @{ $wordlist{$pref} }, $words[$pref_len]; shift @words; # next word on this line } } # starting point # my $pref = (keys %wordlist)[rand keys %wordlist]; print "$pref"; # dump out listings # for (0..($maxwords-1)) { last if not defined($wordlist{$pref}); my $index = rand @{ $wordlist{$pref} }; my $suf = $wordlist{$pref}[$index]; print ' '. $suf; print "\n" if ( $_ % 10 == 0); # skip past first word in prefix # #$pref = (split(' ', $pref))[1..$pref_len-1] . ' ' . $suf; $pref =~ s/^[^ ]+ (.+)$/$1 $suf/; } __END__ two good samples generated from the book of psalms: "For, lo, thine enemies, and the horn of David the son of thine house hath eaten me up" "His mouth is full of troubles"

In reply to Markov Chain Program by sacked

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (3)
    As of 2014-09-21 14:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (172 votes), past polls