Question
(PYTHON) Your task this week is to write a very simple spam classifier in Python. It will classify messages as either SPAM (unwanted) or HAM
(PYTHON)
Your task this week is to write a very simple spam classifier in Python. It will classify messages as either SPAM (unwanted) or HAM (wanted).
The program will have a set of SPAM_WORDS, words that are known to appear in spam messages.
You will also define a spam threshold which reflects the allowed percentage of spam words in the message. You'll compute a 'spam indicator', which is the ratio of spam words to the total number of unique words in the message. You will round the spam indicator to two decimals. If the spam indicator exceeds the spam threshold, the message is classified as spam. Otherwise it is classified as ham. We'll assume that the spam threshold is a constant and has a value of 0.10.
Your program will prompt the user for a message and then will print the corresponding classification.
The program will be case insensitive. The spam words are detected whether they are in lower case or upper case or mixed case.
For simplicity, we'll ignore punctuation.
Testing: (MAKE SURE TO FOLLOW THIS)
Make sure that you test your solution before you submit it. Here are a few test cases with the expected output. Feel free to add your own.
Test case 1 - classify message correctly as SPAM - Make sure the SPAM indicator is correct
Please enter your message: The widow of a deposed dictator wants your help in getting his money out of the country
SPAM indicator: 0.27
This message is: SPAM
Test case 2 - classify message correctly as HAM
Please enter your message: I got a new job offer today. It looks good. Are you free for lunch tomorrow? We can meet downtown at noon.
SPAM indicator: 0.09
This message is: HAM
Test case 3 - classify message correctly regardless of the case
Please enter your message: Do not miss out on this once in a lifetime OPPORTUNITY call NOW
SPAM indicator: 0.23
This message is: SPAM
Test case 4 - classify message correctly based on the number of unique words
Please enter your message: It is urgent that you call us immediately yada yada yada yada yada yada
SPAM indicator: 0.11
This message is: SPAM
Test case 5 - A message with a SPAM indicator 0.1 is classified as HAM.
Please enter your message: Congratulations on your new job! I hope you like it.
SPAM indicator: 0.1
This message is: HAM
Use the following template:
# ----------------------------------------------------------------------------- # Name: spam # Purpose: # # Author: # Date: # ----------------------------------------------------------------------------- """ Enter your module docstring with a one-line overview here and a more detailed description here. """ SPAM_WORDS = {'opportunity', 'inheritance', 'money', 'rich', 'dictator', 'discount', 'save', 'free', 'offer', 'credit', 'loan', 'winner', 'warranty', 'lifetime', 'medicine', 'claim', 'now', 'urgent', 'expire', 'top', 'plan', 'prize', 'congratulations', 'help', 'widow'} def spam_indicator(text): """ Enter your function docstring here """ # This function returns the spam indicator rounded to two decimals def classify(indicator): """ Enter your function docstring here """ # This function prints the spam classification def get_input(): """ Enter your function docstring here """ # Prompt the user for input and return the input def main(): # Get the user input and save it in a variable # Call spam_indicator to compute the spam indicator and save it # Print the spam_indicator # Call classify to print the classification if __name__ == '__main__': main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started