Question

In: Computer Science

(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and...

(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:

  1. Upload text data

  2. Find by Receiver

  3. Download statistics

  4. Exit the program

Program Options

Option 1: Upload Text Data

If the user chooses this option, the program will

  1. Prompt the user for the file that contains the data.

  2. 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:

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:

  1. 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)

  2. Email address that received the most emails

  3. Number of emails within the same domain/organization (same address after the @)

  4. 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)

Solutions

Expert Solution

# 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


Related Solutions

Program must use Python 3 Your program must have a welcome message for the user. Your...
Program must use Python 3 Your program must have a welcome message for the user. Your program must have one class called CashRegister. Your program will have an instance method called addItem which takes one parameter for price. The method should also keep track of the number of items in your cart. Your program should have two getter methods. getTotal – returns totalPrice getCount – returns the itemCount of the cart Your program must create an instance of the CashRegister...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Instructions from your teacher: Write a Python program that does the following: Displays a welcome message...
Instructions from your teacher: Write a Python program that does the following: Displays a welcome message Prompts a user to enter a number (an integer), and then converts that value from inches to feet. If the user entered a negative number, display an error message telling the user that it was not a "positive " number and prompt the user to enter a number again. Displays the converted value, then " feet" Continue converting the user's valid values until the...
Complete the following in syntactically correct Python code. 1. The program should display a message indicating...
Complete the following in syntactically correct Python code. 1. The program should display a message indicating whether the person is an infant, a child, a teenager, or an adult. Following are the guidelines: a. If the person is older than 1 year old or less, he or she is an infant. b. If the person is older than 1 year, but younger than 13, he or she is a child c. If the person is at least 13 years old,...
Using python, produce code that mimics some ATM transactions: a. A welcome message must be displayed...
Using python, produce code that mimics some ATM transactions: a. A welcome message must be displayed initially reflecting the appropriate time of day (for example: Good Night, Welcome to Sussex Bank). b. Assume the user’s account balance is $5375.27. c. Allow the user to enter a pin number that does not have to be validated: def atm_login(): pin_number = input("Please enter your (4 digit) pin number: ") # display welcome message welcome_message() d. The message should be followed by a...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to Zion's Pizza Restaurant and ask the user how many people are in their dinner group. If the answer is more than eight (8), print a message saying they'll have to wait for a table. Otherwise, report that their table is ready and take their order. Assume the client orders one pizza, and ask what he/she would like on their pizza, include a loop that...
CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
Write a program that accept an integer input from the user and display the least number...
Write a program that accept an integer input from the user and display the least number of combinations of 500s, 100s, 50s, 20s, 10s, 5s, and 1s. Test your solution using this samples] a. Input: 250 Output: 1x200s, 1x50s b. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s c. Input: 1127 Output: 5x200s, 1x100s, 1x20s, 1x5s, 2x1s d. Input: 19 Output: 1x10s, 1x5s, 4x1s ​[Hints] o Use division to determine the number of occurrence of each element (i.e. 200, 100)...
Step by step in python Write a program that will keep asking for a user input...
Step by step in python Write a program that will keep asking for a user input (until a blank line is entered) and will inform me whether what I entered was a valid number or not (without crashing). The program should use at least one try/except loop The program should include at least two custom written functions (a main() function can count as one of the two)
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT