1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
# 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:
```python
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:
```python
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:
```python
# 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`/`untrain` match `^[-_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):
1. Unicode NFKC normalization and lowercasing
2. Split on non-word characters
3. Snowball stemming (language from `language` param)
4. 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("")` and `load_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:
```bash
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:
```json
{
"categories": {
"spam": {
"tokenTally": 6,
"probNotInCat": 0,
"probInCat": 1
}
}
}
```
### Classifying Text
##### Endpoint:
```
/classify
Accepts: POST
Body: raw text/plain
```
Example response:
```json
{
"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:
```json
{
"spam": 3.2142857142857144,
"ham": 0.7857142857142857
}
```
### Flushing Training Data
##### Endpoint:
```
/flush
Accepts: POST
Body: raw text/plain (optional)
```
Example response:
```json
{
"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_file` and `load_from_file` in library workflows to persist/reload model state.
- `/readyz` returns `200` while accepting traffic and `503` when draining during shutdown.
## License
MIT, see `LICENSE`.
|