Take-home test for Unit 5Intro
@
with open('nytimes. tweets') as lines:
for line in lines:
print(line)
Mentions
You are given a collection of text files, each containing 1000 recent tweets posted by several popular Twitter accounts.
Each line in a file is one tweet, so if we read the files the usual way, each line will corresponds to a separate tweet:
If you are getting a decoding error, to correctly open UTF-8 encoded text files, use a variant of the function open:
with open('nytimes. tweets', encoding='utf-8') as lines:
for line in lines:
print(line)
A username prefixed with @is called a mention. For example, the tweet below contains four mentions
(@voguemagazine, @FentyOfficial, @ethjgreen, and @yusefhairnyc):
Assignment details | Check files under references 9, 2019/nA username prefixed with @ is called a mention. For example, the tweet below contains four mentions
(@voguemagazine, @FentyOfficial, @ethjgreen, and @yusefhairnyc):
Rihanna @rihanna - Oct 9, 2019
So proud to be on another cover of @voguemagazine wearing my
own designs from @FentyOfficial !!! On stands October 16th!
Photographer: @ethjgreen
Fashion Editor: Tonne Goodman
Hair: @yusefhairnyc
Makeup: Kanako Takase
vogue.cm/Tp750AW
The above tweet is represented by the following string in the text file rihanna. tweets, any mention can be identified
a word that starts with @:
https://mediatb.blob.core.windows.net/media//64800cc5e9dd9350167930d5/questions/takehome-test-for-unit-5_16861138134... 1/3
07/06/2023, 10:53
Take-home test for Unit 5
So proud to be on another cover of @voguemagazine wearing my own designs from @FentyOfficial !!! On stands
October 16th! Photographer: @ethjgreen Fashion Editor: Tonne Goodman Hair: @yusefhairnyc Makeup: Kanako Takase
https//vogue.cm/Tp750AW pic.twitter.com/LU9TEe1NEh
Task
We will write a program for finding the most frequently mentioned usernames in each of the provided files. To
correctly identify mentions, we have to cleanup each tweet, keeping only letters, digits, and symbols @ and _. After
each tweet is cleaned up, we have to go through its words, and if the word starts with @, it is a mention.
Step-by-step:
1. Modify function cleanedup so that it keeps not only letters, but also digits 0123456789 and symbols @ and
Assignment details | Check files under references/n07/06/2023, 10:53
Take-home test for Unit 5
So proud to be on another cover of @voguemagazine wearing my own designs from @Fentyofficial !!! On stands
October 16th! Photographer: @ethjgreen Fashion Editor: Tonne Goodman Hair: @yusefhairnyc Makeup: Kanako Takase
https//vogue.cm/Tp750AW pic.twitter.com/LU9TEe1NEh
Task
We will write a program for finding the most frequently mentioned usernames in each of the provided files. To
correctly identify mentions, we have to cleanup each tweet, keeping only letters, digits, and symbols @ and _. After
each tweet is cleaned up, we have to go through its words, and if the word starts with @, it is a mention.
Step-by-step:
1. Modify function cleanedup so that it keeps not only letters, but also digits 0123456789 and symbols and
2. Write a new function findMentions that takes a filename as a parameter and reports 3 usernames most
frequently mentioned in that file. The function should create a dictionary of counts for all username mentions
(words starting with @). After reading through the file and accumulating the counts for all mentioned usernames,
use the dictionary to create a list like this:
[[15: @alice'], [20, '@bob'], [7, @carol'], ... ]
Use sort to sort the above list and print out 3 most frequently mentioned usernames.
3. Check each file in the current folder (using os.listdir('.') ), if the file name ends with .tweets, call
findMentions on the file to find its most frequent mentions.
Example output
If you copy all provided .tweets files in the folder with your script, its output will look as follows: