PHPackages                             sphamster/bayes - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sphamster/bayes

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sphamster/bayes
===============

Bayes machine learning

v1.1.0(6mo ago)18[3 issues](https://github.com/sphamster/bayes/issues)MITPHPPHP ^8.2CI passing

Since Nov 6Pushed 3mo agoCompare

[ Source](https://github.com/sphamster/bayes)[ Packagist](https://packagist.org/packages/sphamster/bayes)[ RSS](/packages/sphamster-bayes/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (3)Used By (0)

[![Bayes Logo](./arts/logo.png)](https://github.com/sphamster/bayes)

Bayes: Naive Bayes Classifier for PHP
=====================================

[](#bayes-naive-bayes-classifier-for-php)

 **A powerful machine learning library for text classification, sentiment analysis, and multi-label categorization**

[![Latest Version on Packagist](https://camo.githubusercontent.com/bfe31b0bb2b8973729e9ef321ab3026c4968445675ffd2a18a1b4809d1a79a3b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f737068616d737465722f62617965732e7376673f7374796c653d666c61742d737175617265266c6162656c3d4c617465737425323056657273696f6e)](https://packagist.org/packages/sphamster/bayes)[![Total Downloads](https://camo.githubusercontent.com/93cdb53654c8713ade43e2a1e3d80193859c1af3387de8252e3dd30f7127ed53/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f737068616d737465722f62617965732e7376673f7374796c653d666c61742d737175617265266c6162656c3d446f776e6c6f616473)](https://packagist.org/packages/sphamster/bayes)[![GitHub Actions Status](https://camo.githubusercontent.com/2bc2246abd81022d689d1a696cf5186659ac364876d065b5b6538abbaf34e4cb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f737068616d737465722f62617965732f746573742e796d6c3f7374796c653d666c61742d737175617265266c6162656c3d7465737473)](https://github.com/sphamster/bayes/actions)[![Code Coverage](https://camo.githubusercontent.com/8723afcaa006ef3b7f694fb3682ade00af89b1da0d08210aefa9c7cf84a68f97/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f737068616d737465722f62617965733f7374796c653d666c61742d737175617265)](https://codecov.io/gh/sphamster/bayes)
[![PHP Version](https://camo.githubusercontent.com/5fb26bb9e2a92ba280360a3dbe7b23a94817b8c97c2260032d49d3f0d7f6af9b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f737068616d737465722f62617965732f7068702e7376673f7374796c653d666c61742d737175617265266c6162656c3d504850)](https://packagist.org/packages/sphamster/bayes)[![PHPStan Level](https://camo.githubusercontent.com/cfad767368c5b88c30d3a7087a14099cb45895b5ded11e2ae54aa298b7ba5893/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c2532306d61782d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://phpstan.org/)[![MIT License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/sphamster/bayes/blob/master/LICENSE)[![Maintained](https://camo.githubusercontent.com/d46f48b041ea6110c1e1dc2b81616fce29ff9422ee4c11a27b56fb8d5e0dc231/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4d61696e7461696e65642533462d7965732d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/sphamster/bayes)

---

About Bayes
-----------

[](#about-bayes)

**Bayes** is a high-performance **Naive Bayes classifier** for **PHP 8.2+** that leverages **machine learning** to automatically categorize text documents into arbitrary categories. Built with modern PHP practices and rigorous code quality standards, it's the perfect solution for **natural language processing (NLP)** tasks in PHP applications.

Whether you're building **spam filters**, **sentiment analysis systems**, **content recommendation engines**, or **multi-label text categorization** tools, Bayes provides a simple yet powerful API backed by solid mathematical foundations.

> **Fork Notice:** This library is an enhanced fork of [niiknow/bayes](https://github.com/niiknow/bayes), rewritten with modern PHP 8.2 features, comprehensive test coverage, and strict type safety.

Table of Contents
-----------------

[](#table-of-contents)

- [About Bayes](#about-bayes)
- [Table of Contents](#table-of-contents)
- [Key Features](#key-features)
- [Use Cases](#use-cases)
- [Setup](#setup)
    - [Requirements](#requirements)
- [Quick Start](#quick-start)
- [Single-Label Classification](#single-label-classification)
    - [Batch Training](#batch-training)
    - [Persistence](#persistence)
- [Multi-Label Classification](#multi-label-classification)
    - [Choosing Between Single-Label and Multi-Label](#choosing-between-single-label-and-multi-label)
    - [Available Prediction Filters](#available-prediction-filters)
    - [Custom Filters](#custom-filters)
- [Advanced Usage](#advanced-usage)
    - [Customizing the Tokenizer](#customizing-the-tokenizer)
    - [Working with Probabilities](#working-with-probabilities)
    - [Running Tests](#running-tests)
- [Credits](#credits)
- [License](#license)

Key Features
------------

[](#key-features)

- 🚀 **High Performance** - Optimized for speed with efficient probability calculations
- 🎯 **Multi-Label Support** - Classify documents into multiple categories simultaneously
- 🔧 **Customizable Tokenizers** - Plug in your own tokenization logic for any language
- 💾 **State Persistence** - Export and import trained classifiers as JSON
- 📊 **Multiple Filtering Strategies** - Threshold, Top-K, and Above-Mean filters included
- ✅ **Production Ready** - PHPStan max level, 100% type coverage, comprehensive test suite
- 🌍 **Framework Agnostic** - Works with Laravel, Symfony, or standalone PHP applications
- 📦 **Zero Dependencies** - Pure PHP implementation, no external libraries required

Use Cases
---------

[](#use-cases)

Bayes excels at a wide range of **text classification** and **machine learning** tasks like:

- Spam Detection &amp; Email Filtering
- Sentiment Analysis
- Content Categorization
- Intent Recognition
- Multi-Label Tagging
- Language Detection

Setup
-----

[](#setup)

You can install the package via Composer:

```
composer require sphamster/bayes
```

### Requirements

[](#requirements)

- **PHP 8.2** or higher
- No other dependencies required

Quick Start
-----------

[](#quick-start)

Get up and running with Bayes in less than 5 minutes:

```
