forked from IsaacLewis/Shintolin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
markov.cgi
executable file
·55 lines (50 loc) · 1.2 KB
/
markov.cgi
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
#!/usr/bin/ruby
print "Content-type: text/html\r\n\r\n"
require 'cgi'
require 'cgi/session'
load 'functions.cgi'
$cgi = CGI.new
user_id = get_validated_id
class Markov
def initialize(source)
source = source.split(' ')
@hash = {}
source.each_with_index do
|word, i|
if @hash[word]
@hash[word] << source[i + 1]
else
@hash[word] = [source[i + 1]]
end
end
end
def chain(n=20)
last_word = @hash.keys.rand_select
text = last_word
(n - 1).times do
new_word = @hash[last_word].rand_select
new_word = @hash.keys.rand_select unless new_word
text += ' ' + new_word
last_word = new_word
end
text
end
end
class Array
def rand_select
self[rand(self.length)]
end
end
if user_id == $cgi['id']
result = mysql_select('messages',
{'speaker_id'=>$cgi['id'],
'type'=>['chat','talk','whisper','shout']})
else
result = mysql_select('messages',
{'speaker_id'=>$cgi['id'],
'type'=>'chat'})
end
str = ''
result.each_hash {|row| str += ' ' + row['message']}
markov = Markov.new str
5.times {puts '<p>' + markov.chain(50) + '</p>'}