In: Computer Science
Make a python dictionary that counts the number of times each word occurs in the the book below book_url = 'http://www.gutenberg.org/cache/epub/2680/pg2680.txt'
without downloading to computer.
To read a text file from weblink use requests.get() method for that we need to import requests, after getting response as <class 'requests.models.Response'> use .text method to extract text.
syntax:- maketrans(what you want to replace, with what you want to replace, what you want to delete )
example:-
str="abcbep"
table=str.maketrans("ab","cf","p")
print(str.traslate(table))
O/P:-cfcfe a will replace with c, b will replace with f, p will be deleted
note:- translate uses the translation mapping specified using the maketrans()
IN our code text.maketrans("\n\t", " ", string.punctuation) ----- all string punctuations will be deleted (ex:-@#$%,./) and \n and \t will be replaced with spaces.
words=words.split() will make list of words.
using for loop we will iterate each word and if that word is present in the dictionary we will simply increment the count else new word will be included. before that to avoid confusion we are changing all letters into lowercase and will check is that word is alphabet or not.
Code:-
import requests
import string
response = requests.get("http://www.gutenberg.org/cache/epub/2680/pg2680.txt")
text = response.text
# Create an empty dictionary
d = dict()
table=text.maketrans("\n\t", " ", string.punctuation)
words=text.translate(table)
words=words.split()
for word in words:
# Check if the word is already in dictionary
word=word.strip().lower()
word="".join([i for i in word if(i.isalpha())])
if word in d:
# Increment count of word by 1
d[word] = d[word] + 1
else:
# Add the word to dictionary with count 1
d[word] = 1
# Print the contents of dictionary
for key in list(d.keys()):
print(key, ":", d[key])
Output:-
"""
If you have any further queries feel free to ask.
and if you like this work give me a like.
"""