-
Notifications
You must be signed in to change notification settings - Fork 3
/
two_letter_cumulative_strategy.rb
39 lines (30 loc) · 1.3 KB
/
two_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
require_relative 'cumulative_roundoff'
class TwoLetterCumulativeStrategy
include CumulativeRoundoff
# Will transform a non-cumulative probability table using two-letter pairs into a cumulative probability table
# using pairs. In a cumulative table, the probability percentages for keys starting with the same letter 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[1] != ' '}.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
# now take care of the special case of keys ending with a space
cumulative_value = 0.0
frequencies.select {|ok| ok[1] == ' '}.keys.sort.each do |key|
cumulative_value += frequencies[key]
# simple round-off to 1.0 to prevent any gaps
cumulative_value = roundoff(cumulative_value)
frequencies[key] = cumulative_value
end
end
end