Skip to content

Simple naive bayes text classifier for spam and ham, with library, CLI and server

License

Notifications You must be signed in to change notification settings

mailsac/spam-classifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spam-classifier - CLI trainer and API server

This is a naive bayes spam classifier, trainer, and API server all rolled into one. It is not sophisticated but it works well for blocking basic email, comment or other text based spam.

Usage

Classifier Library

const Classifier = require('spam-classifier').Classifier

const classifier = new Classifier()
classifier.save('path/to/model.json') // creates when not existing
classifier.load('path/to/model.json')

const spammyText = `Subject: hey there\nYou won the lottery!`
const hammyText = `Subject: check out my npm module\nIt is published live!`

classifier.trainSpam(spammyText)
classifier.trainHam(hammyText)

const result = classifier.predict('Am I a spam bot?') // true or false

classifier.save('path/to/model.json')

CLI tool

# must be a path to a sqlite database
node cli.js train data/model.json --db=~/spam.sqlite --table=emails

node cli.js predict data/model.json file1.eml file2.eml file3.eml

Spam API Server

$ MODEL=data/model.json PORT=3000 node server.js

Loaded classifier from data/model.json
spam server is listening on port 3000

Request example:

POST http://localhost:3000/api/spam-checks
{
    to: '[email protected]',
    from: '[email protected]',
    subject: 'hey',
    text: 'yo'
}

response example:

{
    spam: false,
    spamminess: 0.193776
}

License

MIT

See LICENSE file in this repository.

About

Simple naive bayes text classifier for spam and ham, with library, CLI and server

Resources

License

Stars

Watchers

Forks

Packages

No packages published