Class: Analyzers::Utils::LetterFrequency

Inherits:
Object
  • Object
show all
Defined in:
lib/crypto-toolbox/analyzers/utils/letter_frequency.rb

Constant Summary collapse

FREQUENCIES =
{
  ' ' =>  20,  # ??
  'e' =>	12.02,
  't' =>	9.10,
  'a' =>	8.12,
  'o' =>	7.68,
  'i' =>	7.31,
  'n' =>	6.95,
  's' =>	6.28,
  'r' =>	6.02,
  'h' =>	5.92,
  'd' =>	4.32,
  'l' =>	3.98,
  'u' =>	2.88,
  'c' =>	2.71
}

Instance Method Summary collapse

Instance Method Details

#letter_count(str) ⇒ Object



23
24
25
26
27
# 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) ⇒ Object



29
30
31
32
33
# 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