File size: 9,731 Bytes
f4481f7
 
 
 
 
 
 
 
 
 
7fad461
 
198444d
 
 
f4481f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import pickle
import sys
import os
import re
import mailbox
import argparse
import csv
from html import unescape
from bs4 import BeautifulSoup

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

# Apply the BASE_DIR to the paths so they resolve absolutely!
MODEL_PATH   = os.path.join(BASE_DIR, "models", "SVM_model.pkl")
FEATURE_PATH = os.path.join(BASE_DIR, "models", "vectorizer.pkl")

# ── text helpers ─────────────────────────────────────────────────────────────
def clean_text(text):
    if not isinstance(text, str):
        return text
    text = re.sub(r'[\x00-\x08\x0B-\x0C\x0E-\x1F\u200B-\u200F\uFEFF]', '', text)
    text = text.encode("utf-16", "surrogatepass").decode("utf-16", "ignore")
    return text[:32767]

def extract_body(msg):
    texts = []
    if msg.is_multipart():
        for part in msg.walk():
            if part.get_content_type() in ("text/plain", "text/html"):
                payload = part.get_payload(decode=True)
                if payload:
                    text = unescape(payload.decode(errors="ignore"))
                    text = BeautifulSoup(text, "html.parser").get_text(" ")
                    texts.append(text)
    else:
        payload = msg.get_payload(decode=True)
        if payload:
            text = unescape(payload.decode(errors="ignore"))
            text = BeautifulSoup(text, "html.parser").get_text(" ")
            texts.append(text)
    combined = " ".join(texts)
    combined = re.sub(r'[\r\n\t]+', ' ', combined)
    combined = re.sub(r'\s+', ' ', combined)
    return combined.strip()

# ── model ─────────────────────────────────────────────────────────────────────
def load_models():
    for path in (MODEL_PATH, FEATURE_PATH):
        if not os.path.exists(path):
            print(f"[ERROR] File not found: {path}")
            sys.exit(1)
    vectorizer = pickle.load(open(FEATURE_PATH, "rb"))
    model      = pickle.load(open(MODEL_PATH,   "rb"))
    return vectorizer, model

def sigmoid(x):
    import math
    return 1 / (1 + math.exp(-x))

def predict(text, vectorizer, model):
    """
    Returns:
      label      : "Spam" or "Ham"
      confidence : probability % if model supports it, else None
      spam_score : 0-100 spam intensity score (always available)
    """
    cleaned  = clean_text(text)
    features = vectorizer.transform([cleaned])
    pred     = model.predict(features)[0]
    label    = "Spam" if str(pred) == "0" else "Ham"

    # confidence via predict_proba (not all SVMs support this)
    confidence = None
    try:
        proba      = model.predict_proba(features)
        # index 0 = spam class (label 0), index 1 = ham class (label 1)
        confidence = round(float(proba[0][0]) * 100, 1)
    except Exception:
        pass

    # spam score via decision function (always works for SVM)
    # decision_function > 0 means ham, < 0 means spam for binary SVC
    # we flip and sigmoid-scale so higher = more spammy
    spam_score = None
    try:
        df_val = float(model.decision_function(features)[0])
        # flip: spam has negative decision value in sklearn SVC (class 0)
        spam_score = round(sigmoid(-df_val) * 100, 1)
    except Exception:
        pass

    # fall back score to confidence if decision_function unavailable
    if spam_score is None and confidence is not None:
        spam_score = confidence

    return label, confidence, spam_score

# ── display helpers ───────────────────────────────────────────────────────────
def score_bar(score, width=20):
    """Visual bar: [████████░░░░░░░░░░░░] 42.3"""
    if score is None:
        return "N/A"
    filled = round(score / 100 * width)
    bar = "█" * filled + "░" * (width - filled)
    return f"[{bar}] {score:.1f}%"

def risk_level(score):
    if score is None:
        return "Unknown"
    if score >= 80:
        return "HIGH RISK"
    if score >= 50:
        return "MEDIUM RISK"
    return "LOW RISK"

def print_result(label, confidence, spam_score):
    verdict = "*** SPAM ***" if label == "Spam" else "Ham (Safe) "
    print(f"\n  Verdict    : {verdict}")
    print(f"  Spam Score : {score_bar(spam_score)}")
    if confidence is not None:
        print(f"  Confidence : {confidence:.1f}%")
    print(f"  Risk Level : {risk_level(spam_score)}")
    print()

# ── modes ─────────────────────────────────────────────────────────────────────
def mode_interactive(vectorizer, model):
    print("\n=== Spam Email Detector ===")
    print("Paste your email text. Press Enter twice to classify.")
    print("Type 'quit' to exit.\n")
    while True:
        print("─" * 44)
        lines = []
        blank_count = 0
        while True:
            try:
                line = input()
            except EOFError:
                break
            if line.strip().lower() in ("quit", "exit"):
                print("Goodbye.")
                sys.exit(0)
            if line.strip() == "":
                blank_count += 1
                if blank_count >= 2:
                    break
            else:
                blank_count = 0
            lines.append(line)

        text = "\n".join(lines).strip()
        if not text:
            print("[!] No text entered. Try again.\n")
            continue

        label, confidence, spam_score = predict(text, vectorizer, model)
        print_result(label, confidence, spam_score)

def mode_single(text, vectorizer, model):
    label, confidence, spam_score = predict(text, vectorizer, model)
    print_result(label, confidence, spam_score)

def mode_mbox(mbox_path, vectorizer, model, output_csv):
    if not os.path.exists(mbox_path):
        print(f"[ERROR] File not found: {mbox_path}")
        sys.exit(1)
    print(f"Loading: {mbox_path}")
    mbox     = mailbox.mbox(mbox_path)
    messages = list(mbox)
    print(f"Found {len(messages)} emails. Classifying...\n")

    results    = []
    spam_count = 0
    scores     = []

    for i, msg in enumerate(messages, 1):
        subject = clean_text(msg.get("Subject", "(no subject)"))
        body    = extract_body(msg)
        label, confidence, spam_score = predict(body, vectorizer, model)

        if label == "Spam":
            spam_count += 1
        if spam_score is not None:
            scores.append(spam_score)

        conf_str  = f"{confidence:.1f}%" if confidence is not None else "N/A"
        score_str = f"{spam_score:.1f}%"  if spam_score  is not None else "N/A"
        risk      = risk_level(spam_score)
        marker    = "SPAM" if label == "Spam" else "Ham "

        results.append({
            "Index":      i,
            "Subject":    subject,
            "Prediction": label,
            "SpamScore":  score_str,
            "Confidence": conf_str,
            "RiskLevel":  risk,
        })
        print(f"  [{i:>4}] {marker}  Score:{score_str:>6}  {risk:<11}  {subject[:50]}")

    ham_count = len(results) - spam_count
    avg_score = round(sum(scores) / len(scores), 1) if scores else 0

    print(f"\n{'─'*44}")
    print(f"  Total emails : {len(results)}")
    print(f"  Spam         : {spam_count}")
    print(f"  Ham          : {ham_count}")
    print(f"  Avg Spam Score : {avg_score}%")

    if output_csv:
        with open(output_csv, "w", newline="", encoding="utf-8") as f:
            writer = csv.DictWriter(f, fieldnames=["Index","Subject","Prediction","SpamScore","Confidence","RiskLevel"])
            writer.writeheader()
            writer.writerows(results)
        print(f"\n  Saved to: {output_csv}")

# ── entry point ───────────────────────────────────────────────────────────────
def main():
    parser = argparse.ArgumentParser(
        description="Spam Email Detector — CMD",
        formatter_class=argparse.RawTextHelpFormatter,
        epilog="""
Examples:
  python cli.py                                          # interactive
  python cli.py --text "You won a prize! Click now."    # inline text
  python cli.py --file email.txt                        # from file
  python cli.py --mbox inbox.mbox --output results.csv  # batch mbox
"""
    )
    parser.add_argument("--text",   help="Email text to classify (inline)")
    parser.add_argument("--file",   help="Path to a .txt file with email content")
    parser.add_argument("--mbox",   help="Path to an .mbox file for batch classification")
    parser.add_argument("--output", help="CSV file to save mbox results (optional)")
    args = parser.parse_args()

    print("Loading models...", end=" ", flush=True)
    vectorizer, model = load_models()
    print("OK\n")

    if args.mbox:
        mode_mbox(args.mbox, vectorizer, model, args.output)
    elif args.text:
        mode_single(args.text, vectorizer, model)
    elif args.file:
        if not os.path.exists(args.file):
            print(f"[ERROR] File not found: {args.file}")
            sys.exit(1)
        with open(args.file, "r", encoding="utf-8", errors="ignore") as f:
            text = f.read()
        mode_single(text, vectorizer, model)
    else:
        mode_interactive(vectorizer, model)

if __name__ == "__main__":
    main()