File size: 3,491 Bytes
13748f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
language: en
library_name: sklearn
pipeline_tag: text-classification
tags:
- sms
- spam-detection
- smishing
- phishing
- tf-idf
- logistic-regression
- explainable-ai
- in-browser
datasets:
- ucirvine/sms_spam
metrics:
- accuracy
- precision
- recall
- f1
model-index:
- name: smishield
  results:
  - task:
      type: text-classification
      name: SMS spam / smishing detection
    dataset:
      name: SMS Spam Collection v.1 (held-out 20% stratified split, seed 42)
      type: ucirvine/sms_spam
    metrics:
    - {type: accuracy, value: 0.9901}
    - {type: precision, value: 0.9792}
    - {type: recall, value: 0.9463}
    - {type: f1, value: 0.9625}
---

# smishield

A small, explainable SMS smishing and spam classifier built to run **entirely in the browser**. The whole model is one 140 KB JSON file (`model.json`). Plain TypeScript scores messages on the device, so no text ever leaves the user's tab.

- **Live demo:** [Space: Battlettt/smishield](https://huggingface.co/spaces/Battlettt/smishield)
- **Source and tests:** [github.com/battlettt/smishield](https://github.com/battlettt/smishield)

## Results

Evaluated on a held-out, stratified 20% test split (1,115 messages: 966 legitimate, 149 spam) of the SMS Spam Collection.

| Metric | Value |
|---|---|
| Accuracy | 0.9901 |
| Precision (spam) | 0.9792 |
| Recall (spam) | 0.9463 |
| F1 (spam) | 0.9625 |

|                     | Predicted legitimate | Predicted spam |
|---------------------|---------------------:|---------------:|
| Actually legitimate | 963 | 3 |
| Actually spam       | 8 | 141 |

Full numbers: [`metrics.json`](metrics.json).

## How it works

1. **Tokenizer.** Lowercase, split on `[a-z0-9]+`. URLs, shortened links, currency amounts and long numbers become special tokens.
2. **TF-IDF.** Word unigrams and bigrams, sublinear term frequency, smoothed IDF, L2 norm. Vocabulary capped at 3,000 terms (min document frequency 2).
3. **Logistic regression.** scikit-learn 1.9.1, `liblinear`, balanced class weights. `C = 10`, picked by 5-fold cross-validated F1 (0.9624) on the training split only.
4. **Export.** `train.py` writes vocabulary, IDF values, coefficients, intercept and threshold to `model.json`.
5. **Scoring.** `P(spam) = sigmoid(intercept + sum(tfidf_i * coef_i))`, flagged at 0.5. Each verdict lists the words that pushed the score most, plus pattern-based warning signs (shortened links, urgency, prize wording).

A parity test in the GitHub repo checks that the TypeScript scorer reproduces scikit-learn's tokens and probabilities on 100 held-out messages.

## Use it

`model.json` holds everything needed to score a message: vocabulary, IDF values, coefficients, intercept and threshold. The tokenizer is in `train.py` (Python) and mirrored in `src/tokenizer.ts` in the GitHub repo.

To retrain: `pip install scikit-learn && python train.py` (downloads the dataset from UCI).

## Limitations

- **2012-era data.** The dataset was collected around 2011-2012, mostly from UK and Singapore. Its spam is mostly premium-rate prize, ringtone and dating texts. Modern smishing (parcel fees, bank and toll alerts) looks different, so real-world accuracy will be lower than the table above.
- **English only.**
- A learning and demo project. It does not replace carrier spam filtering, and "likely legitimate" is not a safety guarantee.

## Data

SMS Spam Collection v.1, Almeida and Gomez Hidalgo, UCI Machine Learning Repository (CC BY 4.0). 5,574 messages: 4,827 ham, 747 spam.