simplebayes package

Submodules

Module contents

The MIT License (MIT)

Copyright (c) 2015 Ryan Vennell

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class simplebayes.SimpleBayes(tokenizer=None, cache_path='/tmp/')[source]

Bases: object

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

cache_file = '_simplebayes.pickle'
cache_persist()[source]

Saves the current trained data to the cache. This is initiated by the program using this module

cache_train()[source]

Loads the data for this classifier from a cache file

Returns:

whether or not we were successful

Return type:

bool

calculate_bayesian_probability(cat, token_score, token_tally)[source]

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()[source]

Caches the individual probabilities for each category

classify(text)[source]

Chooses the highest scoring category for a sample of text

Parameters:

text (str) – sample text to classify

Returns:

the “winning” category

Return type:

str

classmethod count_token_occurrences(words)[source]

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()[source]

Deletes all tokens & categories

get_cache_location()[source]

Gets the location of the cache file

Returns:

the location of the cache file

Return type:

string

score(text)[source]

Scores a sample of text

Parameters:

text (str) – sample text to score

Returns:

dict of scores per category

Return type:

dict

tally(category)[source]

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)[source]

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, text)[source]

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, text)[source]

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