Python Forum
Implementing spam detector code into a GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Implementing spam detector code into a GUI
#1
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:
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.
Reply
#2
Please supply:
  • Intent of software
  • What's going wrong
  • Show all error traces (if any), complete and unaltered.
Reply
#3
(Apr-22-2020, 06:24 PM)Larz60+ Wrote: Please supply:
  • Intent of software
  • What's going wrong
  • Show all error traces (if any), complete and unaltered.

Hey there,

Thanks for replying!

The intent of the software is to scan a textual input and predict if it is spam or not spam. It is an e-mail spam detector. That is the first code, the second one is the GUI. Both are perfectly fine and working.

The first code is a part of a school project, the second one is an attempt to make it even better by implementing the first code into a simple GUI.

The thing that's going wrong is that I just can't figure out how to implement the first code into the second (which is the GUI). I've tried alone, without looking at tutorials but I've been getting a dozen of errors and headaches.

Here's how I picture it:
I'd like to input a text and with the click of the defined button, I'd love the GUI to display if it is "Spam" or "Not Spam".

I've tried defining the button and its function. I just don't know how to define it, the entry and the label.

Any kind of advice or help is appreciated, I do not expect anyone to do this instead of me. The only experience of mine with programming a GUI is a GUI calculator. It can't be compared to this.

Regards,
nikolassj.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help implementing an input with file operations Gaijin 3 2,016 Jun-08-2022, 05:50 PM
Last Post: Gaijin
Question Implementing classes , RSS feed program Miraclefruit 1 3,496 Oct-16-2017, 04:11 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020