-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
33 lines (28 loc) · 999 Bytes
/
main.py
File metadata and controls
33 lines (28 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from stats import getWordsCount, getCharacterFreq, getDictionaryList
import sys
def sort_on(list):
return list['num']
def get_book_text(path_to_file):
with open(path_to_file) as f:
fileContents = f.read()
return fileContents
def printDictionaryList(list):
for x in list:
print(f'{x['char']}: {x['num']}')
def main():
if len(sys.argv) == 1:
print('Usage: python3 main.py <path_to_book>')
exit(1)
bookPath = sys.argv[1]
fileContents = get_book_text(bookPath)
totalWordCount = getWordsCount(fileContents)
print('============ BOOKBOT ============')
print(f'Analyzing book found at {bookPath}...')
print('----------- Word Count ----------')
print(f'Found {totalWordCount} total words')
charFreqDict = getCharacterFreq(fileContents)
dictList = getDictionaryList(charFreqDict)
dictList.sort(key=sort_on, reverse=True)
print('--------- Character Count -------')
printDictionaryList(dictList)
main()