Author Topic: [Perl] Sentences generator.  (Read 548 times)

0 Members and 1 Guest are viewing this topic.

Offline Freak

  • /dev/null
  • *
  • Posts: 17
  • Cookies: 3
    • View Profile
[Perl] Sentences generator.
« on: December 02, 2015, 08:39:54 am »
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.

Code: (perl) [Select]
#!/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!
« Last Edit: December 02, 2015, 08:42:45 am by Freak »

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: [Perl] Sentences generator.
« Reply #1 on: December 02, 2015, 06:57:28 pm »
* kenjoe41 waiting on the finished version. Love Markov chains.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]