class Analyzers::Utils::LetterFrequency
Constants
- FREQUENCIES
Public Instance Methods
letter_count(str)
click to toggle source
# File lib/crypto-toolbox/analyzers/utils/letter_frequency.rb, line 23 def letter_count(str) str.downcase.each_char.with_object({}) do |c,h| h[c] = increment_letter_count(h,c) if countable?(c) end end
letter_freq(str)
click to toggle source
# File lib/crypto-toolbox/analyzers/utils/letter_frequency.rb, line 29 def letter_freq(str) counts = letter_count(str) total_chars = counts.values.reduce(&:+) Hash[reverse_hash(counts).map{|k,v| [k,calculate_frequency(v,total_chars)] } ] end
Private Instance Methods
calculate_frequency(value,total)
click to toggle source
# File lib/crypto-toolbox/analyzers/utils/letter_frequency.rb, line 41 def calculate_frequency(value,total) (value/total.to_f).round(4) end
countable?(char)
click to toggle source
# File lib/crypto-toolbox/analyzers/utils/letter_frequency.rb, line 49 def countable?(char) char =~ /[A-Za-z ]/ end
increment_letter_count(hsh,char)
click to toggle source
# File lib/crypto-toolbox/analyzers/utils/letter_frequency.rb, line 45 def increment_letter_count(hsh,char) (hsh.fetch(char,0) + 1) end
reverse_hash(hsh)
click to toggle source
# File lib/crypto-toolbox/analyzers/utils/letter_frequency.rb, line 38 def reverse_hash(hsh) hsh.sort_by{|k,v| -v} end