BayesFilterTrainFilter Method |
Namespace: MailBee.AntiSpam
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | message is a null reference (Nothing in Visual Basic). |
Prior to starting using the Bayesian filter to detect spam messages you should learn it with a number of spam and non-spam messages. The filter becomes effective if the database contains at least hundreds of different spam and non-spam messages.
This method updates the database only in memory. To store the database to disk, use SaveDatabase(String, String) method.
This sample trains Bayesian database with spam and non-spam messages and saves it to disk.
It's assumed the spam and non-spam samples are .EML files located in C:\AntiSpam\Spam and C:\AntiSpam\NonSpam folders respectively. The database itself (spam.dat and nonspam.dat) will be saved in C:\AntiSpam folder.
// To use the code below, import these namespaces at the top of your code. using System.IO; using MailBee.Mime; using MailBee.AntiSpam; class Sample { static void Main(string[] args) { BayesFilter filter = new BayesFilter(); MailMessage msg = new MailMessage(); filter.LoadDatabase(@"C:\AntiSpam\spam.dat", @"C:\AntiSpam\nonspam.dat"); // Train Bayesian filter for spam messages. string[] files = Directory.GetFiles(@"C:\AntiSpam\Spam", "*.eml"); foreach (string file in files) { msg.LoadMessage(file); filter.TrainFilter(msg, true); // Mark as spam. } // Train Bayesian filter for non-spam messages. files = Directory.GetFiles(@"C:\AntiSpam\NonSpam", "*.eml"); foreach (string file in files) { msg.LoadMessage(file); filter.TrainFilter(msg, false); // Mark as non-spam. } // Save Bayesian database to disk. filter.SaveDatabase(@"C:\AntiSpam\spam.dat", @"C:\AntiSpam\nonspam.dat"); } }