-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfrequencies.py
More file actions
18 lines (17 loc) · 929 Bytes
/
frequencies.py
File metadata and controls
18 lines (17 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"""Code Fragment 10.1: A program for counting word frequencies in a document,
and reporting the most frequent word. We use Python’s dict class for the map.
We convert the input to lowercase and ignore any non-alphabetic characters."""
def get_word_frequency(filename):
freq = {}
for piece in open(filename).read().lower().split():
word = "".join(c for c in piece if c.isalpha()) # Only consider alphabetic characters within piece
if word: # Require at least one alphabetic character
freq[word] = 1 + freq.get(word, 0)
max_word = ""
max_count = 0
for (w,c) in freq.items(): # (key, value) tuples represent (word, count)
if c > max_count:
max_word = w
max_count = c
print(f"The most frequent word is: {max_word}")
print(f"Its number of occurences is {max_count}")