# bayes-classifier-multigram

> Naive Bayes classifier - MultiGrams - inspired by https://github.com/miguelmota/bayes-classifier.git

Latest version **0.0.6** (published 2018-06-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install bayes-classifier-multigram
pnpm add bayes-classifier-multigram
yarn add bayes-classifier-multigram
bun add bayes-classifier-multigram
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2018-06-18 |
| First published | 2018-06-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 203.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Naveen Jafer - naveen.n.jafer@gmail.com Thanks to - Miguel Mota for his base work. |
| Maintainers | naveenjafer |
| Keywords | naive, bayes, classifier |

## Links

- npm: https://www.npmjs.com/package/bayes-classifier-multigram
- Homepage: https://github.com/naveenjafer/bayes-classifier
- Issues: https://github.com/naveenjafer/bayes-classifier/issues
- npm.io page: https://npm.io/package/bayes-classifier-multigram

## Recent versions

- 0.0.6 (latest) — 2018-06-18
- 0.0.4 — 2018-06-18
- 0.0.3 — 2018-06-18
- 0.0.2 — 2018-06-18
- 0.0.1 — 2018-06-18

## README

# Naive Bayes classifier

This is a [Naive Bayes classifier](http://en.wikipedia.org/wiki/Naive_Bayes_classifier) implementation written in JavaScript.

I have used the existing code by Miguel https://github.com/miguelmota/bayes-classifier.git and modified it to work with uni,bi and trigrams when calculating the score. I have also expanded the API to be able to give percentage directly for a query.

Algorithms used - algorithms from the [appratus](https://github.com/NaturalNode/apparatus) and [natural](https://github.com/NaturalNode/natural) modules, and also the [Porter stemmer](https://github.com/NaturalNode/natural/tree/master/lib/natural/stemmers) algorithm. All credit goes to them.

# Demo
Refer to the demo by Miguel here.
[https://lab.miguelmota.com/bayes-classifier](https://lab.miguelmota.com/bayes-classifier)

# Install

```bash
npm install bayes-classifier-multigram
```

```bash
bower install bayes-classifier-multigram
```

# Usage

```javascript
var BayesClassifier = require('bayes-classifier')
var classifier = new BayesClassifier()

var positiveDocuments = [
  `I love tacos.`,
  `Dude, that burrito was epic!`,
  `Holy cow, these nachos are so good and tasty.`,
  `I am drooling over the awesome bean and cheese quesadillas.`
]

var negativeDocuments = [
  `Gross, worst taco ever.`,
  `The buritos gave me horrible diarrhea.`,
  `I'm going to puke if I eat another bad nacho.`,
  `I'd rather die than eat those nasty enchiladas.`
]

classifier.addDocuments(positiveDocuments, `positive`)
classifier.addDocuments(negativeDocuments, `negative`)

classifier.train()

console.log(classifier.classify(`I heard the mexican restaurant is great!`)) // "positive"
console.log(classifier.classify(`I don't want to eat there again.`)) // "negative"
console.log(classifier.classify(`The torta is epicly bad.`)) // "negative"
console.log(classifier.classify(`The torta is tasty.`)) // "positive"

console.log(classifier.getClassifications(`Burritos are the meaning of life.`))
/*
 [ { label: 'positive', value: 0.22222222222222224 },
   { label: 'negative', value: 0.11111111111111112 } ]
*/
console.log(classifier.getClassificationsAsPercent('Burritos are the meaning of life.'));
[ { label: 'positive', value: '66.67%' },
  { label: 'negative', value: '33.33%' } ]
```

Restoring a classifier to avoid re-training data

```javascript
// Storing classifier
var storeFile = `${__dirname}/store.json`
fs.writeFileSync(storeFile, JSON.stringify(classifier))

// ...

// Restoring classifier
var classifier = new BayesClassifier()
var storedClassifier = require(storeFile)
classifier.restore(storedClassifier)
```

# API

#### classifier.addDocument(doc, class)

#### classifier.addDocuments([docs], class)

#### classifier.train()

#### classifier.classify(doc)

#### classifier.getClassifications(doc)

#### classifier.getClassificationsAsPercent(doc)

#### classifier.restore(classifier)

# Test

```bash
npm test
```

# License

MIT

---
_Source: https://npm.io/package/bayes-classifier-multigram · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
