{"id":18926772,"url":"https://github.com/ladjs/naivebayes","last_synced_at":"2025-07-03T23:33:29.995Z","repository":{"id":42367813,"uuid":"269808748","full_name":"ladjs/naivebayes","owner":"ladjs","description":"A ladjs naivebayes package forked from `https://github.com/surmon-china/naivebayes`","archived":false,"fork":false,"pushed_at":"2023-12-07T01:49:26.000Z","size":502,"stargazers_count":12,"open_issues_count":1,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-03T23:51:31.327Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ladjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-05T23:32:30.000Z","updated_at":"2025-01-10T20:01:29.000Z","dependencies_parsed_at":"2024-06-19T20:03:33.935Z","dependency_job_id":"14989929-11e8-4cdc-a7a2-b42d2cfec978","html_url":"https://github.com/ladjs/naivebayes","commit_stats":{"total_commits":10,"total_committers":1,"mean_commits":10.0,"dds":0.0,"last_synced_commit":"bf6fae7244fba2713d67098a76049d72747446d6"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/ladjs/naivebayes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladjs%2Fnaivebayes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladjs%2Fnaivebayes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladjs%2Fnaivebayes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladjs%2Fnaivebayes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ladjs","download_url":"https://codeload.github.com/ladjs/naivebayes/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ladjs%2Fnaivebayes/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262419807,"owners_count":23308101,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T11:17:08.423Z","updated_at":"2025-07-03T23:33:29.853Z","avatar_url":"https://github.com/ladjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [**@ladjs/naivebayes**](https://github.com/ladjs/naivebayes)\n\n[![build status](https://img.shields.io/travis/com/shaunwarman/naivebayes.svg)](https://travis-ci.com/shaunwarman/naivebayes)\n[![code coverage](https://img.shields.io/codecov/c/github/shaunwarman/naivebayes.svg)](https://codecov.io/gh/shaunwarman/naivebayes)\n[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)\n[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://lass.js.org)\n[![npm downloads](https://img.shields.io/npm/dt/@ladjs/naivebayes.svg)](https://npm.im/@ladjs/naivebayes)\n\n\u003e A ladjs naivebayes package forked from surmon-china/naivebayes\n\n\n## Table of Contents\n\n* [What can I use this for](#what-can-i-use-this-for)\n* [Install](#install)\n  * [npm](#npm)\n  * [yarn](#yarn)\n* [Usage](#usage)\n* [API](#api)\n  * [Class](#class)\n  * [Learn](#learn)\n  * [Probabilities](#probabilities)\n  * [Categorize](#categorize)\n  * [ToJson](#tojson)\n  * [ToJsonObject](#tojsonobject)\n  * [FromJson](#fromjson)\n  * [Debug](#debug)\n* [Contributors](#contributors)\n\n\n## What can I use this for\n\nNaive-Bayes classifier for JavaScript.\n\n`naivebayes` takes a document (piece of text), and tells you what category that document belongs to.\n\nYou can use this for categorizing any text content into any arbitrary set of **categories**. For example:\n\n* Is an email **spam**, or **not spam** ?\n* Is a news article about **technology**, **politics**, or **sports** ?\n* Is a piece of text expressing **positive** emotions, or **negative** emotions?\n\n\n## Install\n\n### npm\n\n```sh\nnpm install @ladjs/naivebayes\n```\n\n### yarn\n\n```sh\nyarn add @ladjs/naivebayes\n```\n\n\n## Usage\n\n```javascript\nconst NaiveBayes = require('naivebayes')\n\nconst classifier = new NaiveBayes()\n\n// teach it positive phrases\nclassifier.learn('amazing, awesome movie!! Yeah!! Oh boy.', 'positive')\nclassifier.learn('Sweet, this is incredibly, amazing, perfect, great!!', 'positive')\n\n// teach it a negative phrase\nclassifier.learn('terrible, cruddy thing. Damn. Sucks!!', 'negative')\n\n// now ask it to categorize a document it has never seen before\nclassifier.categorize('awesome, cool, amazing!! Yay.')\n// =\u003e 'positive'\n\n// serialize the classifier's state as a JSON string.\nconst stateJson = classifier.toJson()\n\n// load the classifier back from its JSON representation.\nconst revivedClassifier = NaiveBayes.fromJson(stateJson)\n\n```\n\n```javascript\nconst NaiveBayes = require('naivebayes')\n\nconst Segment = require('segment')\nconst segment = new Segment()\n\nsegment.useDefault()\n\nconst classifier = new NaiveBayes({\n\n    tokenizer(sentence) {\n\n        const sanitized = sentence.replace(/[^(a-zA-Z\\u4e00-\\u9fa50-9_)+\\s]/g, ' ')\n\n        return segment.doSegment(sanitized, { simple: true })\n    }\n})\n```\n\n\n## API\n\n### Class\n\n```javascript\nconst classifier = new NaiveBayes([options])\n```\n\nReturns an instance of a Naive-Bayes Classifier.\n\n#### Options\n\n* `tokenizer(text)` - (type: `function`) -  Configure your own tokenizer.\n* `vocabularyLimit` - (type: `number` default: 0) - Reference a max word count where `0` is the default, meaning no limit.\n* `stopwords` - (type: `boolean` default: false) - To remove [stopwords](https://en.wikipedia.org/wiki/Stop_words) from text\n\nEg.\n\n```javascript\nconst classifier = new NaiveBayes({\n    tokenizer(text) {\n        return text.split(' ')\n    }\n})\n```\n\n### Learn\n\n```javascript\nclassifier.learn(text, category)\n```\n\nTeach your classifier what `category` the `text` belongs to. The more you teach your classifier, the more reliable it becomes. It will use what it has learned to identify new documents that it hasn't seen before.\n\n### Probabilities\n\n```javascript\nclassifier.probabilities(text)\n```\n\nReturns an array of `{ category, probability }` objects with probability calculated for each category. Its judgement is based on what you have taught it with `.learn()`.\n\n### Categorize\n\n```javascript\nclassifier.categorize(text ,[probability])\n```\n\nReturns the `category` it thinks `text` belongs to. Its judgement is based on what you have taught it with `.learn()`.\n\n### ToJson\n\n```javascript\nclassifier.toJson()\n```\n\nReturns the JSON representation of a classifier. This is the same as `JSON.stringify(classifier.toJsonObject())`.\n\n### ToJsonObject\n\n```javascript\nclassifier.toJsonObject()\n```\n\nReturns a JSON-friendly representation of the classifier as an `object`.\n\n### FromJson\n\n```javascript\nconst classifier = NaiveBayes.fromJson(jsonObject)\n```\n\nReturns a classifier instance from the JSON representation. Use this with the JSON representation obtained from `classifier.toJson()`.\n\n### Debug\n\nTo run `naivebayes` in debug mode simply set `DEBUG=naivebayes` when running your script.\n\n\n## Contributors\n\n| Name             | Website                    |\n| ---------------- | -------------------------- |\n| **Surmon**       | \u003chttp://surmon.me/\u003e        |\n| **Shaun Warman** | \u003chttps://shaunwarman.com/\u003e |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fladjs%2Fnaivebayes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fladjs%2Fnaivebayes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fladjs%2Fnaivebayes/lists"}