Skip to content

Instantly share code, notes, and snippets.

@jfarmer
Forked from Katzy/textalyzer
Created August 13, 2014 17:52
Show Gist options
  • Save jfarmer/05316a231f4caff70632 to your computer and use it in GitHub Desktop.
Save jfarmer/05316a231f4caff70632 to your computer and use it in GitHub Desktop.
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