GithubHelp home page GithubHelp logo

derhuerst / nbayes Goto Github PK

View Code? Open in Web Editor NEW
15.0 5.0 3.0 79 KB

A Naive Bayes classifier written in JavaScript.

Home Page: https://github.com/derhuerst/nbayes

License: ISC License

JavaScript 100.00%
bayes natural-language-processing nlp

nbayes's Introduction

nbayes

nbayes is a lightweight Naive Bayes Classifier written in vanilla JavaScript. It classifies a document (arbitrary piece of text) among the classes (arbitrarily named categories) it has been trained with before. This is all based on simple mathematics. As an example, you could use nbayes to answer the following questions.

npm version build status dependency status dev dependency status ISC-licensed support me via GitHub Sponsors chat with me on Twitter

  • Is an email spam, or not spam ?
  • Is a news article about technology, politics, or sports ?
  • Does a piece of text express positive emotions, or negative emotions?
const nbayes = require('nbayes')

let classifier = nbayes()
classifier.learn('happy',   nbayes.stringToDoc('amazing, awesome movie!! Yeah!! Oh boy.'))
classifier.learn('happy',   nbayes.stringToDoc('Sweet, this is incredibly amazing, perfect, great!!'))
classifier.learn('angry',   nbayes.stringToDoc('terrible, shitty thing. Damn. This Sucks!!'))
classifier.learn('neutral', nbayes.stringToDoc('I dont really know what to make of this.'))

classifier.classify(nbayes.stringToDoc('awesome, cool, amazing!! Yay.'))
// -> 'happy'

nbayes offers a simple and straightforward API, keeping it below 3kb (minified). It is a rewrite of ttezel/bayes and thoroughly tested.

Installing

npm install nbayes

API

nbayes.createDoc()

Creates a representation of a document, which can be used to track words and their frequencies.

Example

let d = nbayes.createDoc()
d.set('foo', 2)
d.add('bar')
d.increase('bar', 2)

d.has('FOO') // -> false
d.get('foo') // -> 2
d.get('bar') // -> 3
d.sum() // -> 5
d.words() // -> ['foo', 'bar']

Methods

  • has(word): If word has been added before.
  • get(word): Returns the count of word.
  • set(word, count): Sets the count of word.
  • add(word): Shorthand for increase(word, 1).
  • increase(word, d = 1): Adds d to the count of word.
  • sum: Returns the sum of all word counts.
  • words: Returns the distinct words.

nbayes.stringToDoc()

Returns a document from the string. Special characters will be ignored. Everything will be lowercase.

Note: It is probably a better idea to use a proper tokenizer/stemmer and to remove stopwords to support non-Latin languages and to get more accurate results.

nbayes.stringToDoc('awesome, amazing!! Yay.').words()
// -> ['awesome', 'amazing', 'yay']

nbayes()

Creates a classifier, which can learn and then classify documents into classes.

Example

let c = nbayes()
c.learn('happy',   nbayes.stringToDoc('amazing, awesome movie!! Yeah!! Oh boy.'))
c.learn('happy',   nbayes.stringToDoc('Sweet, this is incredibly amazing, perfect, great!!'))
c.learn('angry',   nbayes.stringToDoc('terrible, shitty thing. Damn. This Sucks!!'))
c.learn('neutral', nbayes.stringToDoc('I dont really know what to make of this.'))

c.classify(nbayes.stringToDoc('awesome, cool, amazing!! Yay.'))
// -> 'happy'
c.probabilities(nbayes.stringToDoc('awesome, cool, amazing!! Yay.'))
// -> { happy: 0.000001…,
//      angry: 2.384…e-7,
//      neutral: 1.665…e-7 }

Methods

  • learn(class, doc): Tags words of doc as being of class.
  • probabilities(doc): For each stored class, returns the probability of doc, given the class.
  • classify(doc): For doc, returns the class with the highest probability.
  • prior(class): Computes the probability of class out of all classes.
  • likelihood(class, doc): Computes the probability of doc, given class.

Contributing

If you have a question, found a bug or want to propose a feature, have a look at the issues page.

nbayes's People

Contributors

bendingbender avatar derhuerst avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

jokame tejzpr a1ip

nbayes's Issues

Enhance README.md with badges

![latest release](https://img.shields.io/github/release/derhuerst/nbayes.svg)
![npm version](https://img.shields.io/npm/v/nbayes.svg)
![node version](https://img.shields.io/node/v/nabyes.svg)
![bower version](https://img.shields.io/bower/v/nabyes.svg)
![codeclimate coverage](https://img.shields.io/codeclimate/coverage/github/derhuerst/nbayes.svg)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.