Question

In: Computer Science

Make a python dictionary that counts the number of times each word occurs in the the...

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.

Solutions

Expert Solution

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.

  • maketrans() method help us to remove unwanted symbols from text and we can replace where we want to replace something

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.

"""


Related Solutions

Make a python dictionary that counts the number of times each word occurs in the the...
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'
Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a',...
Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a', 'b', 'c','e','j']; list2 = ['a', 'c', 'd', 'b','e','z']; {'a': 1, 'c': 1, 'b': 1, 'e': 1, 'j': 0} How do I get to this to work without the Collections counter?
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Write a RIMS-compatible C-language for-loop that counts the number of times a bit of A is...
Write a RIMS-compatible C-language for-loop that counts the number of times a bit of A is followed by a bit of the opposite parity (01 or 10) and writes the value to B. For example 00100110 has 4 cases: 00100110, 00100110, 00100110, 00100110.
How do I make a dictionary in Python language? Can somebody please provide me with an...
How do I make a dictionary in Python language? Can somebody please provide me with an example code of a Dictionary in Python Language? Thank you in advance.
Add a new function that takes a phrase as an argument and counts each unique word...
Add a new function that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
Toss 5 coins 25 times and note on each throw the number of heads. Make a...
Toss 5 coins 25 times and note on each throw the number of heads. Make a probability distribution of the number of heads. Find mean and variance of that distribution and compare it with the mean and variance of theoretical probability distribution using binomial probability distribution.
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
Define the Poisson distribution when you are interested in the number of times an event occurs...
Define the Poisson distribution when you are interested in the number of times an event occurs in a given area of opportunity. Elaborate how the Area of opportunity is a continuous unit or interval of time, volume, or such area in which more than one occurrence of an event can occur?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT