Hello,
I'm working on a little project, and I decided to share my code from the initial concept. (When I finish the final awesome version, I'll share that too).
Basically I saw /r/subredditsimulator which is a subreddit comprised entirely of bots meant to simulate a real subreddit by using Markov chains to create realistic sounding text. It caught my attention and I absolutely love stuff like this. I decided to make something similar, and started by just writing a program to create realistic-ish sentences.
This program takes in a file with sample text, chooses a random word, then chooses every next word by making a list of words that follow that word in the sample (including repeats) and picking one of those at random until it reaches the set sentence length.
This is a very rough program, it doesn't do capitalization or filter punctuation or anything, but it's a start.
#!/usr/bin/perl -w
use strict;
my(@sample, $name, @sentence, $num, $lastWord, @nextWords);
print "Enter the name of the file: ";
chomp($name = <stdin>);
open FILE, "<$name" or die "Error: $!\n";
while(<FILE>){
push(@sample, split(" ", lc($_)));
}
close FILE;
for(;;){
@sentence = ();
print "Enter the number of words for the sentence (Enter nothing to quit): ";
chomp($num = <stdin>);
if($num eq ""){
last;
}else{
push(@sentence, $sample[int(rand(scalar(@sample)))]." ");
for(my $i = 1;$i<$num;++$i){
$lastWord = $sentence[$i-1];
$lastWord =~ s/ //g;
@nextWords = ();
for(my $e = 0; $e<scalar(@sample); ++$e){
if($sample[$e] eq $lastWord){
push(@nextWords, $sample[$e+1]);
}
}
push(@sentence, $nextWords[int(rand(scalar(@nextWords)))]." ");
}
print @sentence;
print "\n\n";
}
}
I took a screenshot of me running it 5 times so you can see the result.
Enjoy!