# simplebayes package

## Submodules

- simplebayes.categories module
  - `BayesCategories`
    - `BayesCategories.add_category()`
    - `BayesCategories.delete_category()`
    - `BayesCategories.get_categories()`
    - `BayesCategories.get_category()`
- simplebayes.category module
  - `BayesCategory`
    - `BayesCategory.get_tally()`
    - `BayesCategory.get_token_count()`
    - `BayesCategory.train_token()`
    - `BayesCategory.untrain_token()`

## Module contents

class simplebayes.SimpleBayes(tokenizer: Callable[[str], List[str]] | None = None, alpha: float = 0.0, language: str = 'english', remove_stop_words: bool = False)

  Bases: `object`

  A memory-based, optional-persistence naïve bayesian text classifier.

  calculate_bayesian_probability(cat: str, token_score: float, token_tally: float) → float

    Calculates the bayesian probability for a given token/category

    Parameters:

      - cat (str) – The category we’re scoring for this token

      - token_score (float) – The tally of this token for this category

      - token_tally (float) – The tally total for this token from all categories

    Returns:

      bayesian probability

    Return type:

      float

  calculate_category_probability() → None

    Caches the individual probabilities for each category

  classify(text: str) → str | None

    Chooses the highest scoring category for a sample of text

    Parameters:

      text (str) – sample text to classify

    Returns:

      the “winning” category

    Return type:

      str

  classify_result(text: str) → ClassificationResult

    Returns structured classification output including score.

  classmethod count_token_occurrences(words: List[str]) → Dict[str, int]

    Creates a key/value set of word/count for a given sample of text

    Parameters:

      words (list) – full list of all tokens, non-unique

    Returns:

      key/value pairs of words and their counts in the list

    Return type:

      dict

  flush() → None

    Deletes all tokens & categories

  get_summaries() → Dict[str, CategorySummary]

    Returns per-category summary details.

  load(source) → None

    Loads classifier state from a text stream.

  load_from_file(absolute_path: str = '') → None

    Loads classifier state from a persisted model file.

  classmethod normalize_category(category: str | None) → str

    Validates and normalizes category input.

  save(destination) → None

    Saves classifier state to a text stream.

  save_to_file(absolute_path: str = '') → None

    Saves classifier state to file using atomic replacement.

  score(text: str) → Dict[str, float]

    Scores a sample of text

    Parameters:

      text (str) – sample text to score

    Returns:

      dict of scores per category

    Return type:

      dict

  tally(category: str) → int

    Gets the tally for a requested category

    Parameters:

      category (str) – The category we want a tally for

    Returns:

      tally for a given category

    Return type:

      int

  classmethod tokenize_text(text: str) → List[str]

    Default tokenize method; can be overridden

    Parameters:

      text (str) – the text we want to tokenize

    Returns:

      list of tokenized text

    Return type:

      list

  train(category: str, text: str) → None

    Trains a category with a sample of text

    Parameters:

      - category (str) – the name of the category we want to train

      - text (str) – the text we want to train the category with

  untrain(category: str, text: str) → None

    Untrains a category with a sample of text

    Parameters:

      - category (str) – the name of the category we want to train

      - text (str) – the text we want to untrain the category with