Text::Travesty (Chapter 17)

The Text::Travesty module implements "travesty," a Markov chain algorithm that analyzes a text document and generates a new document that preserves all the word-pair (tuple) frequencies of the original. The result is an incomprehensible document that has an eerie similarity to the writing style of the original.

 0 package Text::Travesty; 1 use strict; 2 use Text::Wrap qw(fill); 3 use IO::File; 4 sub new { 5 my $pack = shift; 6 return bless { 7 words => [], 8 lookup => {}, 9 num => {}, 10 a => ' ', p=> ' ', n=>' ', 11 },$pack; 12 } 13 sub add { 14 my $self = shift; 15 my $string = shift; 16 my ($words,$lookup,$num,$a,$p,$n) = 17 @{$self}{qw(words lookup num a p n)}; 18 for my $w (split /\s+/,$string) { 19 ($a,$p) = ...

Get Network Programming with Perl now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.