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
17 changes: 14 additions & 3 deletions frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ def get_word_list(file_name):
returns a list of the words used in the book as a list.
All words are converted to lower case.
"""
pass
with open(file_name,'r') as f:
text = f.read()
text = text.translate(None, string.punctuation).lower() #Formatting
return string.split(text)

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 @@ -19,6 +22,14 @@ def get_top_n_words(word_list, n):
punctuation
n: the number of words to return
returns: a list of n most frequently occurring words ordered from most
frequently to least frequentlyoccurring
frequently to least frequently occurring
"""
pass
word_dict = {}
for word in word_list:
word_dict[word] = word_dict.get(word, 0) + 1
ordered_list = sorted(word_dict, key=word_dict.get, reverse=True)
return ordered_list[:n]

if __name__ == '__main__':
lisht = get_word_list('pg32325.txt')
print get_top_n_words(lisht, 100)
Loading