assisted-mindfulness/naive-bayes
A lightweight Naive Bayes classifier for PHP and Laravel. Train on labeled text and classify new documents for spam detection, sentiment analysis, or category prediction with a simple, easy-to-use API.
This is a small, framework-agnostic Naive Bayes text classifier — not a Laravel package with a service provider, config file or facade. Expect to wire it up yourself, which is usually 20 lines of code.
composer require tabuna/naive-bayes. Version 2.x targets modern PHP (8.1+) and pulls in brick/math for the probability arithmetic — you do not interact with it directly.config/naive-bayes.php, no migrations, no published assets. If you want container access, bind a configured classifier instance in a AppServiceProvider or a small dedicated service class.train() it with [label, text] pairs (or one label at a time), then call predict() / predictProba() with new text.Classifier + tokenizer classes. The tokenizer is the single most influential knob; start with the bundled whitespace/regex tokenizer before writing your own.Train in a command, predict in a request. Training is CPU-bound and should never happen in an HTTP request. Put it in an artisan command or queued job that reads labeled rows from your database, and persist the trained model (the package serialises the classifier) to storage or a cache key. Controllers then load the model and only call predict().
Version your model artifacts. Because the model is a snapshot of your training corpus, store it with a hash or timestamp (e.g. storage/app/models/tickets-{corpus_hash}.model) and let deploys write a new one rather than mutating in place. This makes rollback trivial and keeps your predictions reproducible across environments.
Wrap it in one service. A TicketClassifier class with train(array $samples) and classify(string $text): string means the rest of the app never touches package classes. That makes swapping tokenizers, adding pre-processing (lowercasing, stripping emails/URLs), or replacing the library entirely a one-file change.
Feed it from an Eloquent query, not from fixtures. Ticket::whereNotNull('category_id')->get(['subject','body','category_id']) is your training set. Use a chunkById loop if the corpus is large so you do not hold everything in memory while tokenizing.
Seed-bootstrap for cold start. Ship a small curated dataset (a few hundred labeled examples per class) in a seeder so the classifier is useful on day one, then retrain from real data on a schedule. A nightly job that retrains on the last N months is the common production shape.
Test against a frozen corpus. Commit a small labeled fixture set and assert exact predictions plus relative confidence ordering. Naive Bayes is fast enough to train inside the test suite, so you get real regression coverage without mocking anything.
brick/math now accepts ^0.18 || ^0.20 (previously effectively pinned to ^0.18). Nothing in the public API changed. The practical risk is Composer resolution: if another package of yours depends on brick/math, running composer update may now pull 0.20 where it previously couldn't, so re-run your test suite and check that any direct Brick\Math usage you have is unaffected. If you need to stay on 0.18, pin it explicitly in your root composer.json.not_ onto the following token) before training and predicting. Apply exactly the same pipeline in both paths; a mismatch here is the number one source of "it worked in training" bugs.How can I help you explore Laravel packages today?