-
Notifications
You must be signed in to change notification settings - Fork 0
/
markov.rb
56 lines (49 loc) · 1.08 KB
/
markov.rb
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
def string_to_sorted_probability_hash(string, bool)
bool ? text = string.split(" ") : text = string.split("")
chars = {}
text.each_with_index do |a, i|
b = text[i+1]
chars[a] ||= {}
chars[a][b] ||= 1
chars[a][b] += 1.0
end
# Normalize
chars.each do |letter, keys|
total = 0
keys.each do |c, val|
total += val
end
keys.each do |c, val|
chars[letter][c] /= total
end
end
# Sort by ascending probabillity
sorted = {}
chars.each do |letter, keys|
sorted[letter] = keys.sort_by{|c, val| val}
end
#return sorted hash
sorted
end
def generate_markov_chain(sorted_hash, length, bool)
out = []
out << sorted_hash.keys.sample
length.times do
keys = sorted_hash[out.last]
acc = 0.0
r = rand
keys.each do |ch, val|
acc += val
if r < acc
out << ch
break
end
end
end
bool ? out.join(" ") : out.join("")
end
def run(string, length, bool)
sorted = string_to_sorted_probability_hash(string, bool)
result = generate_markov_chain(sorted, length, bool)
puts result
end