simplebayes
A memory-based, optional-persistence naive Bayesian text classification package and web API for Python.
Why?
Bayesian text classification is useful for things like spam detection,
sentiment determination, and general category routing.
You gather representative samples for each category, train the model,
then classify new text based on learned token patterns.
Once the model is trained, you can:
- classify input into a best-fit category
- inspect relative per-category scores
- persist and reload model state
Installation
Requires Python 3.10 or newer.
$ git clone https://github.com/hickeroar/simplebayes.git
$ cd simplebayes
$ python3 -m venv .venv
$ source .venv/bin/activate
$ pip install -e .
If you only want to use simplebayes as a library:
$ pip install simplebayes
Run as an API Server
$ simplebayes-server --port 8000
CLI options:
--host Host interface to bind. (default: 0.0.0.0)
--port Port to bind. (default: 8000)
--auth-token Optional bearer token for non-probe endpoints.
--language Language code for stemmer and stop words. (default: english)
--remove-stop-words Filter common stop words (the, is, and, etc.).
--verbose Log requests, responses, and classifier operations to stderr.
--help Show all options.
Environment variable equivalents:
SIMPLEBAYES_HOST
SIMPLEBAYES_PORT
SIMPLEBAYES_AUTH_TOKEN
SIMPLEBAYES_LANGUAGE
SIMPLEBAYES_REMOVE_STOP_WORDS (1, true, yes = enabled)
SIMPLEBAYES_VERBOSE (1, true, yes = enabled)
Verbose mode
When --verbose is set, the server logs each request and response to stderr, plus classifier insight: tokens extracted, category operations, scores, and summaries. Example:
$ simplebayes-server --port 8000 --verbose
When --auth-token is configured, all API endpoints except /healthz and /readyz require:
Authorization: Bearer <token>
The API uses HTTP Bearer authentication. When auth is enabled, OpenAPI docs at /docs and /redoc expose the Bearer scheme; use the "Authorize" button in Swagger UI to set the token for interactive testing.
Use as a Library in Your App
Import and create a classifier:
from simplebayes import SimpleBayes
classifier = SimpleBayes()
# Optional: SimpleBayes(alpha=0.01, language="english", remove_stop_words=True) to filter stop words
classifier.train("spam", "buy now limited offer click here")
classifier.train("ham", "team meeting schedule for tomorrow")
classification = classifier.classify_result("limited offer today")
print(f"category={classification.category} score={classification.score}")
scores = classifier.score("team schedule update")
print(scores)
classifier.untrain("spam", "buy now limited offer click here")
Persistence example:
from simplebayes import SimpleBayes
classifier = SimpleBayes()
classifier.train("spam", "buy now limited offer click here")
classifier.save_to_file("/tmp/simplebayes-model.json")
loaded = SimpleBayes()
loaded.load_from_file("/tmp/simplebayes-model.json")
print(loaded.classify_result("limited offer today"))
Custom options example:
# Laplace smoothing for better handling of unseen tokens
classifier = SimpleBayes(alpha=0.01)
# Spanish text with Spanish stemmer and stop words
classifier = SimpleBayes(language="spanish", remove_stop_words=True)
# Opt-in stop-word removal
classifier = SimpleBayes(remove_stop_words=True)
Notes for library usage:
- Classifier operations are thread-safe.
- Scores are relative values; compare scores within the same model.
- Category names accepted by
train/untrainmatch^[-_A-Za-z0-9]{1,64}$.
Classifier Options
| Parameter | Default | Description |
|---|---|---|
tokenizer |
built-in | Override with a callable (str) -> list[str]. |
alpha |
0.0 |
Laplace smoothing. Use 0.01 or 1.0 to avoid zero probabilities for tokens unseen in a category; improves handling of sparse vocabularies. |
language |
"english" |
Language code for both the Snowball stemmer and built-in stop words. Supported: arabic, armenian, basque, catalan, danish, dutch, english, esperanto, estonian, finnish, french, german, greek, hindi, hungarian, indonesian, irish, italian, lithuanian, nepali, norwegian, portuguese, romanian, russian, serbian, spanish, swedish, tamil, turkish, yiddish. |
remove_stop_words |
False |
Filter common stop words when True (the, is, and, etc.). Default False for backwards compatibility. |
Tokenization
Default tokenization (when no custom tokenizer is provided):
- Unicode NFKC normalization and lowercasing
- Split on non-word characters
- Snowball stemming (language from
languageparam) - Stop-word removal when
remove_stop_words=True
The language parameter drives both stemming and stop-word filtering. Built-in stopword lists are included for all supported languages: arabic, armenian, basque, catalan, danish, dutch, english, esperanto, estonian, finnish, french, german, greek, hindi, hungarian, indonesian, irish, italian, lithuanian, nepali, norwegian, portuguese, romanian, russian, serbian, spanish, swedish, tamil, turkish, yiddish. No download or file storage required.
Stream APIs are available:
save(stream)load(stream)
File API notes:
save_to_file("")andload_from_file("")use/tmp/simplebayes-model.json.- Provided file paths must be absolute.
Development Checks
$ ./.venv/bin/pytest tests/ --cov=simplebayes --cov-fail-under=100 -v
$ ./.venv/bin/flake8 simplebayes tests
$ ./.venv/bin/pylint simplebayes tests --fail-under=10
Using the HTTP API
API Notes
- Category names in
/train/{category}and/untrain/{category}must match^[-_A-Za-z0-9]{1,64}$. - Request body size is capped at 1 MiB on text endpoints.
- Error responses for auth/size/encoding are JSON:
{"error":"unauthorized"}{"error":"request body too large"}{"error":"invalid utf-8 payload"}
- The HTTP service stores classifier state in memory; process restarts clear training data.
Common Error Responses
| Status | When |
|---|---|
401 |
Missing/invalid bearer token when auth is enabled |
405 |
Wrong HTTP method |
400 |
Request body contains invalid UTF-8 |
413 |
Request body exceeds 1 MiB |
422 |
Invalid category route format |
Training the Classifier
Endpoint:
/train/{category}
Example: /train/spam
Accepts: POST
Body: raw text/plain
Example:
curl -s -X POST "http://localhost:8000/train/spam" \
-H "Content-Type: text/plain" \
--data "buy now limited offer click here"
Untraining the Classifier
Endpoint:
/untrain/{category}
Example: /untrain/spam
Accepts: POST
Body: raw text/plain
Getting Classifier Status
Endpoint:
/info
Accepts: GET
Example response:
{
"categories": {
"spam": {
"tokenTally": 6,
"probNotInCat": 0,
"probInCat": 1
}
}
}
Classifying Text
Endpoint:
/classify
Accepts: POST
Body: raw text/plain
Example response:
{
"category": "spam",
"score": 3.2142857142857144
}
If no category can be selected (for example, untrained model), category is returned as null.
Scoring Text
Endpoint:
/score
Accepts: POST
Body: raw text/plain
Example response:
{
"spam": 3.2142857142857144,
"ham": 0.7857142857142857
}
Flushing Training Data
Endpoint:
/flush
Accepts: POST
Body: raw text/plain (optional)
Example response:
{
"success": true,
"categories": {}
}
Health and Readiness
Liveness endpoint
/healthz
Accepts: GET
Readiness endpoint
/readyz
Accepts: GET
/healthz and /readyz are intentionally unauthenticated even when API auth is enabled.
Operational Notes
- The HTTP server is in-memory by default; deploys/restarts wipe trained state.
- Use
save_to_fileandload_from_filein library workflows to persist/reload model state. /readyzreturns200while accepting traffic and503when draining during shutdown.
License
MIT, see LICENSE.
Metadata
Release files for simplebayes 3.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| simplebayes-3.2.0.tar.gz | 70.8 kB | With provenance | Details for simplebayes-3.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| simplebayes-3.2.0-py3-none-any.whl | Python 3 | none | any | Details for simplebayes-3.2.0-py3-none-any.whl |
Total release size: 133.7 kB
Release files / simplebayes-3.2.0.tar.gz
| Download URL | simplebayes-3.2.0.tar.gz |
|---|---|
| Size | 70.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2d3e801409e88a1db76a070e1a28f9dbd1068102cb2d2e6967854abc082aa896
|
|
BLAKE2b-256 checksum How to use checksums |
a306b31cde00d558f859fc35fec75d7452ec2b4d9e021b2685988778650c10e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 24, 2026.
Transparency logRelease files / simplebayes-3.2.0-py3-none-any.whl
| Download URL | simplebayes-3.2.0-py3-none-any.whl |
|---|---|
| Size | 62.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5567001d351a752ce67f6bab4c30acf6cedf59763e44eb234dfb27dfba83dcbc
|
|
BLAKE2b-256 checksum How to use checksums |
9572e0509647a3106c2512a309e12f63ce17f9e02618c16587409fbc0337cbfc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 24, 2026.
Transparency logSecurity for simplebayes 3.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for simplebayes-3.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 24, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.