#!/usr/bin/env python
"""
Lol documentations.
"""

__author__	= "Nick Moffitt <nick@zork.net>"
__version__	= 0.1
__url__		= "http://zork.net/pub/bayesfilter.py"
__description__	= "Use Reverend Thomas to perform bayesian filtering of comments."

def verify_installation(request):
	config = request.getConfiguration()
	if not config.has_key("bayes_dir"):
		print "The \"bayes_dir\" property must be set in your config.py file."
		print "It is the location where the bayes database file \"comments.bay\" will reside."
		return 0
	return 1

from reverend.thomas import Bayes

def cb_comment_reject(args):
	r = args["request"]
	comment = args["comment"]

	config = r.getConfiguration()
	dbfile = os.path.join(config.get("bayes_dir", "/tmp/"), "comments.bay")

	filter = Bayes()
	try:
		filter.load(dbfile)
	except IOError:
		pass
	
	text = comment['description'].lower()
	guess = filter.guess(text)
	type, certainty = guess[0] #the first one is the most certain, usually.
	if len(res) > 1:
	    (type2, certainty2) = guess[1]
	    if type == 'spam':
		certainty = certainty / certainty2
	    else:
		certainty = certainty2 / certainty
	    certainty = certainty/(certainty+1)
	if certainty > 0.90:
		filter.train(type, text)
		filter.save(dbfile)
	if type == 'spam' and certainty > 0.75:
		return 1
	return 0

