Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,33 @@

import string

def get_word_list(file_name):
""" Reads the specified project Gutenberg book. Header comments,
punctuation, and whitespace are stripped away. The function
returns a list of the words used in the book as a list.
All words are converted to lower case.
def get_word_list(filename):
"""
pass
Reads the specified project Gutenberg book. Header comments,
punctuation, and whitespace are stripped away. The function
returns a list of the words used in the book as a list.
All words are converted to lower case.
"""
bookraw = open(filename, 'r')
bookstring = ''
for line in bookraw:
bookstring = bookstring + line
bookstring = bookstring.replace("\n", " ")
bookstring = bookstring.replace(string.punctuation, "")
bookstring = bookstring.lower()
booklist = bookstring.split()

counter = 0
while counter < 2:
if booklist[0] == '***':
counter += 1
print counter
del booklist[0]
else:
del booklist[0]
return booklist



def get_top_n_words(word_list, n):
""" Takes a list of words as input and returns a list of the n most frequently
Expand All @@ -21,4 +41,14 @@ def get_top_n_words(word_list, n):
returns: a list of n most frequently occurring words ordered from most
frequently to least frequentlyoccurring
"""
pass
histogram = {}
for word in word_list:
histogram[word] = histogram.get(word, 0) + 1
hist_list = []
ordered_by_frequency = sorted(histogram, key=histogram.get, reverse=True)
return ordered_by_frequency[0:n]

def word_frequency_stuff(filename, n)
word_list = get_word_list(filename)
print get_top_n_words(word_list, n)

Loading