## Trevor Bark - BB 499 Fall 2013
## Exercise 6

#Create a function that will read in an arbitrary text file and count the number of words in the file.
#a) Count the number of words in the file

#open the file

from sys import argv

#Check for user input as needed by argv, displays error with no input

def parse_user_input(argument):         #argument - supplied by the user
    try:
        input_userfile = argv[1]
    except IndexError:
        raise ValueError('Please input a file after specifying the script')

parse_user_input(argv)

input_userfile = argv[-1]
f = open(input_userfile,'U')

def count_words(f):                          #Create a function to count the words
	all_text = f.read()                       #all text is assigned
	all_words = all_text.split()              #all words is created with split
	
	amount = 0                                #iteration loop to print number of words
	for i in all_words:
		amount = amount + 1
	print 'There are ' + str(amount) + ' words in your file'
	
count_words(f)

#b) Create a function that will give you the character frequency count for that file in descending order

from operator import itemgetter

counts = dict()

for line in input_userfile:
	letters = list(line)
	for letter in letters:
		if letter isalpha():
			letter = letter.lower()
			counts[letter] = counts.get(letter,0) + 1
			
print counts
print sorted(counts.get)








