aboutsummaryrefslogtreecommitdiff
path: root/process.pl
blob: e1f1fae01229a17839a16e9c9727a39e1195f400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env perl

use strict;
use warnings;

use utf8;
binmode(STDOUT, ":utf8");

$| = 1;

use Data::Dumper;

sub some {
	return $_[rand(@_)];
}

# Takes plain text lines in on stdin, performs analysis, outputs custom markov
# file on stdout

# Global markov data
my %markov_data;

print "\"" x 80;
print "\nLearning words...\n";

while (<STDIN>) {
	chomp;
	utf8::upgrade($_);
	my @words = split /\s+/, $_;
	
	# Leaning is the same for all but last word
	for (my $i = 0; $i < @words - 1; $i++) {
		my $word = $words[$i];
		my $next_word = $words[$i + 1]; # +1 safe beacuse of loop bounds

		push @{$markov_data{$word}}, $next_word;
	}

	# Now handle special case; last word must be learned as being followed by EOL ("")
	push @{$markov_data{$words[@words - 1]}}, "";
}

print "Word Patterns:\n";
#print Dumper(%markov_data);
print "\"" x 80;
print "\n";


while (1) {
	my $word = $ARGV[0] || some(keys %markov_data);
	print "Taking \"$word\" as the seed\n";
	my $i = 0;
	do {
		$i++;
		print "$word";
		$word = some(@{$markov_data{$word}});
	} until($word eq "" or $i == 100);
	print "\n";
	sleep 1;
}

print "\n";
print "\"" x 80;
print "\n";