Last active
August 29, 2015 14:05
-
-
Save Katzy/91dd3d9fd80959a17c71 to your computer and use it in GitHub Desktop.
Textalyzer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
def textalyzer (string) | |
file_name = ARGV[0] | |
string = open(file_name).read | |
histogram(freq(count(str_char(sanitize(string))))) | |
return | |
end | |
def count (string) | |
to_count = {} | |
string.each do |element| | |
if to_count[element] | |
to_count[element] += 1 | |
else to_count[element] = 1 | |
end | |
end | |
to_count | |
end | |
def freq (count_hash) | |
frequency = {} | |
frequency_scrub = {} | |
tally = 0 | |
count_hash.each do |element, value| | |
if element < "a" || element > "z" | |
frequency_scrub[element] = 1 | |
else | |
tally += value | |
end | |
end | |
count_hash.each do |element, value| | |
if element < "a" || element > "z" | |
frequency_scrub[element] = 1 | |
else | |
frequency[element] = value.to_f/tally | |
frequency[element] *= 100 | |
end | |
end | |
frequency.sort | |
end | |
def histogram (freq_hash) | |
freq_hash.each do |element, value| | |
puts element + " [" + value.round(2).to_s + "%]" + "=" * (value/0.2) | |
end | |
end | |
def str_char (string) | |
string.chars | |
end | |
def sanitize (string) | |
string.downcase | |
end | |
if __FILE__ == $0 | |
p textalyzer(ARGV) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment