import os

from reverend.thomas import Bayes

def get_db(private_path, username):
  path = os.path.join(os.path.join(private_path, username), 'spam.bayes')
  guesser = Bayes()

  # load the spam DB
  try:
      guesser.load(path)
  except IOError:
      print "Creating a new spam filter database"
      guesser.save(path)

  return guesser, path

def train_spam(text, private_path, username):
  guesser, path = get_db(private_path, username)
  guesser.train('spam', text)
  guesser.save(path)

def train_ham(text, private_path, username):
  guesser, path = get_db(private_path, username)
  guesser.train('ham', text)
  guesser.save(path)

# try to guess the spam / ham ratio of a text
def guess(text, private_path, username):
  guesser, path = get_db(private_path, username)
  guesser.load(path)

  spam = 0
  ham = 0
  value = guesser.guess(text)
  for o in value:
    if o[0] == 'ham': ham = o[1]
    if o[0] == 'spam': spam = o[1]
  return spam > ham

@@ -2,16 +2,31 @@
 mport os
+%0Aimport os.path
 %0A%0Afrom r
@@ -213,26 +213,24 @@
 %0A  try:%0A    
-  
 guesser.load
@@ -254,18 +254,16 @@
 OError:%0A
-  
     prin
@@ -302,18 +302,143 @@
 tabase%22%0A
+%0A
   
+  parent_directory = os.path.dirname(path)%0A    if not os.path.isdir(parent_directory):%0A      os.makedirs(parent_directory)%0A%0A
     gues

create missing directories for spam file