make a python dictionary that counts the number of times each letter occurs in the the book below
book_url = 'http://www.gutenberg.org/cache/epub/2680/pg2680.txt'
Here is the python code to do so. Before looking at the code you
must know that in the code below I have only counted the frequency
of English letters only. Here I have initialized the dictionary and
incremented the respective key-value as I found a
letter.
import urllib.request
url = "http://www.gutenberg.org/cache/epub/2680/pg2680.txt"
file = urllib.request.urlopen(url)
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
freqMap = {}
for letter in letters:
freqMap[letter] = 0
for line in file:
decoded_line = line.decode("utf-8")
for i in decoded_line:
if i in freqMap:
freqMap[i] = freqMap[i]+1
print("Count of all the letters present are: ")
print(freqMap)
Get Answers For Free
Most questions answered within 1 hours.