-
Notifications
You must be signed in to change notification settings - Fork 3
/
three_letter_cumulative_strategy.rb
46 lines (35 loc) · 1.49 KB
/
three_letter_cumulative_strategy.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
require_relative 'cumulative_roundoff'
class ThreeLetterCumulativeStrategy
include CumulativeRoundoff
# Will transform a non-cumulative probability table using three-letter triplets into a cumulative probability table
# using triplets. In a cumulative table, the probability percentages for keys starting with the same two letters are
# cumulative with the last key of the group having a probability of 1.0.
def execute!(frequencies)
last_key_read = nil
cumulative_value = 0.0
# for all keys except those ending with a space (which are a special case)
frequencies.select {|ok| ok[2] != ' '}.keys.sort.each do |key|
if last_key_read != nil && (last_key_read[0] != key[0] || last_key_read[1] != key[1])
cumulative_value = 0.0
end
cumulative_value += frequencies[key]
# simple round-off to 1.0 to prevent any gaps
cumulative_value = roundoff(cumulative_value)
frequencies[key] = cumulative_value
last_key_read = key
end
# now take care of the special case of keys ending with a space
last_key_read = nil
cumulative_value = 0.0
frequencies.select {|ok| ok[2] == ' '}.keys.sort.each do |key|
if last_key_read != nil && last_key_read[0] != key[0]
cumulative_value = 0.0
end
cumulative_value += frequencies[key]
# simple round-off to 1.0 to prevent any gaps
cumulative_value = roundoff(cumulative_value)
frequencies[key] = cumulative_value
last_key_read = key
end
end
end