In: Computer Science
(CODE IN PYTHON)
Program Input:
Your program will display a welcome message to the user and a menu of options for the user to choose from.
Welcome to the Email Analyzer program. Please choose from the following options:
|
Program Options
Option 1: Upload Text Data
If the user chooses this option, the program will
Prompt the user for the file that contains the data.
Read in the records in an appropriate structure/class
Input File Format:
To_Email From_Email Time
Sample Input File:
[email protected] [email protected] 19:24
[email protected] [email protected] 12:17
[email protected] [email protected] 03:58
[email protected] [email protected] 05:45
Write a function called UploadTextData to do this task. Choose the appropriate parameters and return type.
Once done reading in the file, the main menu will be displayed again.
Option 2: Find by Receiver
If the user chooses this option, the program will ask the user for the receiver’s email. The program will list the time and sender of all emails this email received
Sample Output:
[email protected] received the following emails:
From [email protected] at 19:24
From [email protected] at 12:17
From [email protected] at 05:45
Write a function called PrintEmails to do this task. Choose the appropriate parameters and return type.
Once done printing, the main menu will be displayed again.
Option 3: Download Statistics
If the user chooses this option, the program will create a statistics file with the following data:
A print out of all the emails received. Each line should have a To, From and Time. (Example: From [email protected] to [email protected] at 19:24)
Email address that received the most emails
Number of emails within the same domain/organization (same address after the @)
A list of all domains that sent emails (From field) and the number of emails received from each of these domains.
The statistics file will have the same name as the input file but with _stats.txt appended to it.
For example, if the input file was named data.txt, the stats file will be named data_stats.txt.
Note: It will remove the .txt from data.txt, before adding the _stats.txt. It will NOT create a file with the name data.txt_stats.txt. You can use the string substr method to remove the last 4 characters from the file name.
Write a function called CreateStatsFile to do this task. Choose the appropriate parameters and return type.
Once done creating the statistics file, the main menu will be displayed again.
Option 4: Exit the Program
If the user chooses this option, the program will exit.
Note: If any other option (other than exit) is chosen, the task will be performed and then the menu will be displayed again.
(CODE IN PYTHON)
# TEXT CODE STARTS HERE
# definign class to store the sender receiver and time
class Record:
# defining constructor
def __init__(self, receiver, sender, time):
self.receiver = receiver
self.sender = sender
self.time = time
# definition of record class ends here
# intialize list to contain each record
data_list = []
# initialize a variable to store input data file name
# it will be used for naming outputfile
input_file_name = ""
# defining function UploadTextData
def UploadTextData(data_file_name):
# create a data_list to store each record in
file
data_list = []
#open file in read mode
file = open(data_file_name, "r")
for line in file:
# create an object of record
class
# here replace("\n", "") replace
new line of time with null string
record = Record(line.split(" ")[0],
line.split(" ")[1], line.split(" ")[2].replace("\n", ""))
#store record in local
data_list
data_list.append(record)
#return data_list
return data_list
#end of UploadTextData definition
# defining function print email
def PrintEmails(receiver_email):
#initialize count variable to store number of mail
received
count = 0
for record in data_list:
if(receiver_email ==
record.receiver):
count = count +
1
if(count ==
1):
print(receiver_email ," received the following
emails: ")
print("From ",
record.sender ," at", record.time , "\n")
if count == 0:
print(receiver_email ," received
zero email\n")
# end of definition of method PrintEnails
while(True):
#display the menu
print("Welcome to the Email Analyzer program. Please
choose from the following options:\n")
print("1. Upload text data.\n")
print("2. Find by Receiver.\n")
print("3. Download statistics.\n")
print("4. Exit the program.\n")
#get the choice
choice = int(input("Enter your option number: "))
if(choice == 1):
# propmpt the user for data file
name
data_file_name = input("Enter data
file name: ")
# store input file name
input_file_name =
data_file_name
#call the function
UploadTextData and store return value in global data_list
data_list =
UploadTextData(data_file_name)
if(len(data_list) != 0):
print("file
uploaded secceessfully...\n")
if(choice == 2):
# ask the user to enter receiver
email
receiver_email = input("Enter
receiver's email: ")
PrintEmails(receiver_email)
if(choice == 3):
# create a file with by appending
_stats.txt to input file name
outputfile_name =
input_file_name.replace(".txt", "_stats.txt")
# create a statistic file and open
in write mode
statistic_file =
open(outputfile_name, "w+")
# printout all the email
received in statistic_file
for record in data_list:
string_list =
["From ", record.sender, " to " , record.receiver, " at ",
record.time]
statistic_file.writelines(string_list)
statistic_file.write("\n")
# Email address that received
the most emails
frequency = {}
for record in data_list:
if
record.receiver in frequency:
frequency[record.receiver] =
frequency[record.receiver] + 1
else:
frequency[record.receiver] = 1
# write the Email address that
received the most emails into output file
statistic_file.write(max(frequency,
key=frequency.get))
# finding Number of emails within
the same domain/organization (same address after the @)
same_domain = {}
for record in data_list:
if
record.receiver.split("@")[1].rstrip() in same_domain:
same_domain[record.receiver.split("@")[1].rstrip()] =
same_domain[record.receiver.split("@")[1].rstrip()] + 1;
else:
same_domain[record.receiver.split("@")[1].rstrip()] = 1
if
record.sender.split("@")[1].rstrip() in same_domain:
same_domain[record.sender.split("@")[1].rstrip()] =
same_domain[record.sender.split("@")[1].rstrip()] + 1;
else:
same_domain[record.sender.split("@")[1].rstrip()] = 1
# writing number of emails in
same domain into output file
for domain in same_domain:
statistic_file.writelines(["Domain ", domain, " has ",
str(same_domain.get(domain)) ," number of emails"])
statistic_file.write("\n")
# finding list of domains that
sends the mail and number of mails received from each of these
domains
sender_domain = {}
for record in data_list:
if
record.sender.split("@")[1].rstrip() in sender_domain:
sender_domain[record.sender.split("@")[1].rstrip()] =
sender_domain[record.sender.split("@")[1].rstrip()] + 1;
else:
sender_domain[record.sender.split("@")[1].rstrip()] = 1
domain_list = []
for domain in sender_domain:
domain_list.append(domain)
domain_list.append(" ")
# write this domain_list to
file
statistic_file.writelines(domain_list)
statistic_file.write("\n")
#writing number of eamils recieved
from each of these emails
for domain in sender_domain:
list1 = ["Nmber
of emails received from domain : " , domain, " is ",
str(sender_domain[domain])]
# write into
file
statistic_file.writelines(list1)
statistic_file.write("\n")
# close the output file
statistic_file.close()
if(choice == 4):
break
#TETXT CODE ENDS HERE
SCREENSHOTS
code:
#input data file data.txt
#RUN THE CODE:
#OUTPUT DATA FILE data_stats.txt