This program tries to create a random, yet coherent text using real text as a model. There are some constants you can play with at the top of the program. With DEPTH set between 8 and 10, you get something coherent, but relatively unlike the starting text. With DEPTH around 5-7, it starts to sound like "All your base are belong to us". Around 3-4, you get a strange pseudo-English jibberish, and at 1-2, it's very alien. It's set to read from the fortune files as installed in RedHat -- change the path as necessary. Enjoy!
#!/usr/bin/perl -w use strict; use constant DEPTH=>9; use constant ENDCHANCE=>0.5; use constant MINLENGTH=>20; use constant MAXLENGTH=>200; # change the depth to change the quality of the output, higher values # are more faithful to the input text. Endchance determines the probab +ility # that the output will stop at a period. You can change your input fil +e # as desired below. Minlength prevents end of output before sending th +at many # characters. Maxlength forces end of output at next period after that + many # characters. open FILE, "/usr/share/games/fortune/wisdom" || die "Find your fortune + file!\n"; my $text=""; print "\n\nHail, O Aspirant! I am the almighty Oracle. I don't have ti +me for\n"; print "pleasantries, so just type the topic of your consultation in on +e\n"; print qq~or two words.\n\n--O Oracle, tell me about: ~; while (<FILE>) { if ((!/--/)&&(!/^%/)) { # This is specifically for fortune files, which have -- before citatio +ns # and % separating lines. Change as necessary for your input. chomp; $text.="$_ "; } } my $start=<STDIN>; chomp($start); if (index($text,$start)<0) { my $position=int rand (length($text)/2); $position=index($text," ",$position); $start=substr($text,$position+1,DEPTH); } if (length($start)<DEPTH) { my $pos= index($text,$start); $start=substr($text,$pos,DEPTH); } else { $start=substr($start,0,DEPTH); } my $length=DEPTH; my $newchar='~'; print "\n\nThe Oracle speaks: \n\n"; print ucfirst($start); while (($newchar ne ".") || ($length<MINLENGTH) || (($length<=MAXLENGT +H) && (rand()<ENDCHANCE))) { $newchar = &get_another_char($start); $start= substr($start,1) . $newchar; print $newchar; $length++; } print "\n\n"; undef $text; sub get_another_char { my $pattern=shift; my $offset=int(rand length($text)); my $spot=0; my $cha; $spot=index($text,$pattern,$offset); $spot=index($text,$pattern) if $spot==-1; die "Oops: $pattern\n\n" if $spot==-1; $cha=substr($text,$spot+DEPTH,1); return $cha; }

Edit: chipmunk 2001-05-17


In reply to Random oracularities by mpolo

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.