Click here to Skip to main content
15,566,096 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The following script extracts the text from the https://norvig.com/big.txt file, stores them in a string object, then assigns this string to a variable called data using the requests third-party library.

#!pip install requests
import requests
response = requests.get('https://norvig.com/big.txt')
data = response.text

Run the script above, uncomment the first line if you need to install the library on your machine, then define and implement a function that returns the followings:

A) The total number of unique words and unique characters
B) The top ten most frequent words and the number of times these words appear in the text.

What I have tried:

#!pip install requests
import requests
response = requests.get('https://norvig.com/big.txt')
data = response.text

data = data.lower()
words = data.split()
words = [word.strip('.,!;()[]') for word in words]
words = [word.replace("'s", '') for word in words]
unique = []
for word in words:
    if word not in unique:
        unique.append(word)
unique.sort()
print("The total number of unique words and unique characters are:",unique.count(value))

I have read on the internet that if you want to count the total number of unique characters just put 'value' in the count function but this gave me an error. What is the right way of doing this? I appreciate your input, thanks
Posted
Updated 15-Mar-22 0:00am
Comments
CHill60 15-Mar-22 4:57am    
You have to tell us the error and where "on the internet" did you read this? Personally I would use a set

1 solution

Firstly you have not define the variable value. Secondly you cannot get the count of characters from an array that contains words. You need to create two unique lists as you read the text, one for complete words and one for characters. You also need a frequency list to count the words which appear most often. You can get the counts for question A by using the len built-in function. See Built-in Functions — Python 3.10.2 documentation[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900