aboutsummaryrefslogtreecommitdiff
path: root/markov-run.rb
blob: 991c5e773d74e2b61f82f245a687e24f88b9f711 (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
#!/usr/bin/env ruby

PRNG = Random.new(Time.now.to_i)

STATS = Marshal::load(File.open(ARGV[0]).read)

CHUNK = STATS["__SETTINGS__"]["__CHUNK__"]

NUM = (ARGV[2] or "1").to_i

def cfd(hash)
	tot = 0
	hash.each_pair {|k, v|
		hash[k] = tot += v
	}
end

def sel(hash)
	max = hash.values.max
	r = PRNG.rand(max*100000) % max
	hash.each_pair {|k, v|
		return k if r < v
	}
end

def aword(chunk, hash)
	sel(cfd(hash[chunk]))
end

NUM.times do
	current = STATS.keys.select{|k| k != "__SETTINGS__" }.sample

	(ARGV[1].to_i - CHUNK).times do
		current << aword(current[-CHUNK..-1], STATS)
	end

	puts current.join(" ")
	puts
end