tm = open('pg35.txt','r')
dict = {}
arr_v = []
arr_k = []
placeholder = None
itera = True
# split all text in the book in words and save the count of same word
for line in tm:
	line = line.strip()
	line = line.translate(None,'!@#$%^&*()_+`~{}[\\]:"|\';<>?/.,<>-=')
	line = line.lower()
	list = line.split(' ')
	for word in list:
		if word in dict:
			count = dict[word]
			count += 1
			dict[word] = count
		else:
			dict[word] = 1

# assing dictonary to array for sort
for word,count in dict.iteritems():
	arr_v.append(count)
	arr_k.append(word)

# dictionary sort desc
while itera:
	itera = False
	for item in range(0,len(arr_v)-1):
		if arr_v[item] < arr_v[item+1]:
			itera = True
			placeholder=arr_v[item+1]			
			arr_v[item+1]=arr_v[item]
			arr_v[item]=placeholder
			
			placeholder=arr_k[item+1]			
			arr_k[item+1]=arr_k[item]
			arr_k[item]=placeholder

# write ordered result to file
res = open('result.txt','w')
for item in arr_k:
	res.write("The word \"" + item + "\" is present " + str(dict[item]) + " times. \n")
res.close()