#!/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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |