#!/usr/bin/env python3

# shebang, hashbang

import string, sys

text = 'hello there'
'''
# goal
counts = {
'a' : 3,
'b' : 2, 
'c' : 5, 
...
'z' : 0
}
print(counts['a'])
'''

def counts_init():
  # start with 0 counts
  counts = {}
  for letter in string.ascii_lowercase:
    counts[letter] = 0
  return counts

def counts_print(counts):
  print(counts)

def counts_add(counts, text):
  'update counts based on text'
  for letter in text.lower():
    if not letter.isalpha(): continue
    counts[letter] += 1

def get_text(filename):
  text = ''
  with open(filename, 'r') as f:
    text = f.read()
  return text

def counts_make_freq(counts):
  '''take counts and make frequencies, 0 to 100%'''
  total = 0
  freq = {}
  for k in counts:
    total += counts[k]
  #print(total)
  for k in counts:
    freq[k] = (counts[k] / total) * 100.0
  return freq

def freq_print(freq):
  for k in freq:
    print(f'{k} : {freq[k]:6.2f}')

#text = get_text('/var/junk/kinne/text-samples/austen.txt')
text = get_text('/var/junk/kinne/text-samples/shakespeare.txt')

#counts = counts_init()
#counts_add(counts, text)

import collections
counts = collections.Counter(text.lower())

counts_print(counts)
freq = counts_make_freq(counts)
freq_print(freq)