import pandas as pd 
from collections import defaultdict 
 
data=[ 
    ('I am the winner of a free prize!','spam'), 
    ('Please provide your bank details for reward.','spam'), 
    ('Hello, How are you today?','ham'), 
    ('Free dinner for all staff this Friday','ham'), 
    ('You have won a lottery prize!','spam'), 
] 
 
df=pd.DataFrame(data,columns=['text','label']) 
 
spam_words = defaultdict(int) 
ham_words = defaultdict(int) 
total_spam_emails = 0 
total_ham_emails = 0 
total_words_in_spam = 0 
total_words_in_ham = 0 
 
for _,row in df.iterrows(): 
    text=row['text'].lower().replace('.','').replace(',','').replace('!','') 
    words=text.split() 
 
    if row['label']=='spam': 
        total_spam_emails+=1 
        for word in words: 
            spam_words[word]+=1 
            total_words_in_spam+=1 
    else: 
        total_ham_emails+=1 
        for word in words: 
            ham_words[word]+=1 
            total_words_in_ham+=1 
 
total_emails = total_spam_emails+total_ham_emails 
 
p_spam = total_spam_emails/total_emails 
p_ham = total_ham_emails/total_emails 
 
def classify_email(text): 
    words = text.lower().replace('.','').replace(',','').replace('!','').split() 
 
    p_spam_given_email = p_spam 
    for word in words: 
        p_word_given_spam = (spam_words[word] + 1) / (total_words_in_spam + len(spam_words) + len(ham_words)) 
        p_spam_given_email *= p_word_given_spam 
 
    p_ham_given_email=p_ham 
    for word in words: 
        if word in spam_words: 
            p_word_given_ham=(ham_words[word] + 1) / (total_words_in_ham + len(spam_words) + len(ham_words)) 
            p_ham_given_email *= p_word_given_ham 
 
    print(f"P(Spam|Email): {p_spam_given_email}") 
    print(f"P(Ham|Email): {p_ham_given_email}") 
 
    if p_spam_given_email > p_ham_given_email: 
        return "Spam" 
    else: 
        return "Not Spam(Ham)" 
 
new_email = input("Enter the mail text: ") 
print(f"New email: '{new_email}'") 
result = classify_email(new_email) 
print(f"This email is classified as: {result}") 