Apr-22-2020, 03:49 PM
Greetings people,
My name is nikolassj and I am an IT student.
First and foremost, I would like to thank you for having me as an member of your community. I hope that you and your loved ones are doing well in these hard times.
I am trying to implement a Python code into a custom made GUI (both were done by following two separate tutorials on the internet). After wrestling for 4-5 days with this, I've decided to ask for help.
I've been using PyCharm, Python version 3.7 I believe.
Detector of spam e-mails:
Kind regards,
nikolassj.
My name is nikolassj and I am an IT student.
First and foremost, I would like to thank you for having me as an member of your community. I hope that you and your loved ones are doing well in these hard times.
I am trying to implement a Python code into a custom made GUI (both were done by following two separate tutorials on the internet). After wrestling for 4-5 days with this, I've decided to ask for help.
I've been using PyCharm, Python version 3.7 I believe.
Detector of spam e-mails:
import os from collections import Counter from sklearn.naive_bayes import MultinomialNB from sklearn.model_selection import train_test_split as tts from sklearn.metrics import accuracy_score import _pickle as c def save(clf, name): with open(name, 'wb') as fp: c.dump(clf, fp) print("saved") def make_dict(): direc = "C:/emails/" files = os.listdir(direc) emails = [direc + email for email in files] words = [] c = len(emails) for email in emails: f = open(email, encoding="latin-1") blob = f.read() words += blob.split(" ") print(c) c -= 1 for i in range(len(words)): if not words[i].isalpha(): words[i] = "" dictionary = Counter(words) del dictionary[""] return dictionary.most_common(3000) def make_dataset(dictionary): direc = "C:/emails/" files = os.listdir(direc) emails = [direc + email for email in files] feature_set = [] labels = [] c = len(emails) for email in emails: data = [] f = open(email, encoding="latin-1") words = f.read().split(' ') for entry in dictionary: data.append(words.count(entry[0])) feature_set.append(data) if "ham" in email: labels.append(0) if "spam" in email: labels.append(1) print(c) c = c - 1 return feature_set, labels d = make_dict() features, labels = make_dataset(d) x_train, x_test, y_train, y_test = tts(features, labels, test_size=0.2) clf = MultinomialNB() clf.fit(x_train, y_train) preds = clf.predict(x_test) print(accuracy_score(y_test, preds)) save(clf, "text-classifier.mdl")Custom made GUI:
import tkinter as tk from PIL import ImageTk, Image HEIGHT = 500 WIDTH = 600 root = tk.Tk() root.title("Email Spam Detector") canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH) canvas.pack() background_image = ImageTk.PhotoImage(Image.open("mail.jpg")) background_label = tk.Label(root, image=background_image) background_label.place(relwidth=1, relheight=1) frame = tk.Frame(root, bg='#80c1ff', bd=5) frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n') label = tk.Label(frame, font=40) label.place(relwidth=0.65, relheight=1) lower_frame = tk.Frame(root, bg='#80c1ff', bd=10) lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n') e = tk.Entry(lower_frame, font=40, borderwidth=5) e.place(relwidth=1, relheight=1) def click(): label1 = tk.Label(label, font=40, text=e.get()) label1.pack() button = tk.Button(frame, text="Scan Mail", font=40, command=click) button.place(relx=0.7, relheight=1, relwidth=0.3) root.mainloop()Any help or advice will be highly appreciated. I must say, it is a lot easier to read the code (and try to understand it) than write it. Thank you in advance!
Kind regards,
nikolassj.