For Phton language 3. You are to create a decoder program that takes a tweet as
ID: 3819195 • Letter: F
Question
For Phton language 3.
You are to create a decoder program that takes a tweet as input and turns it into an English sentence. A tweet will contain common acronyms and abbreviations, and your program will need to have a way to decode them (dictionary).
For example) “lol” => “laughing out loud”, “brb” => “be right back”
Provided is a file with common tweet abbreviations and associated meanings, your program is to use this
csv file (tweet_decoder.csv) to create its dictionary.
Start by creating a framework for the program that contains a dictionary with a few abbreviations
such as
decoder = { “brb” : ”be right back”, ”b4” : ”before” ...}
Ask for a user to input a tweet.
Lookup abbreviations within the tweet, decode and replace them.
Print out the decoded string.
Offer the option to run again.
Your program will need to open up the “tweet_decoder.csv” file. Make sure that this file is
copied into the same directory where your python file is saved.
Review slide 16 from week 12 (part1, Record example 2) as a reference.
Each line of the CSV file represents a key and a value pair – These needs to be added to the
dictionary.
Explanation / Answer
# pastebin link for code:https://pastebin.com/bFF6txL4
word_dict = {}
with open("tweet_decoder.csv", "r") as fh:
for line in fh:
(abbr, meaning) = line.split(",", 1)
print(abbr, meaning)
word_dict[abbr] = meaning.strip()
word_dict[abbr.lower()] = meaning.strip()
def tweet_decoder(tweet):
tweet_split = tweet.split()
result_tweet = []
for tweet in tweet_split:
if tweet in word_dict:
result_tweet.append(word_dict[tweet])
else:
result_tweet.append(tweet)
return " ".join(result_tweet)
tweet = input("Enter a tweet: ")
tweet = tweet_decoder(tweet)
print("Decoded tweet: " + tweet)
'''
File used
brb,be right back
b4, before
lol, laghing out loud
'''
# Sample run
# Enter a tweet: This is an example tweet brb to complete it b4 you die lol
# Decoded tweet: This is an example tweet be right back to complete it before you die laghing out loud
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.