Learns from the specified message as from spam or non-spam source.
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 method.
| Exception Type | Condition |
|---|---|
| MailBeeInvalidArgumentException | message is a null reference (Nothing in Visual Basic). |
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.
[C#] // 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"); } }
[Visual Basic] ' To use the code below, import these namespaces at the top of your code. Imports System.IO Imports MailBee.Mime Imports MailBee.AntiSpam Module Sample Sub Main(ByVal args() As String) Dim filter As BayesFilter = New BayesFilter() Dim msg As MailMessage = New MailMessage() filter.LoadDatabase("C:\AntiSpam\spam.dat", "C:\AntiSpam\nonspam.dat") ' Train Bayesian filter for spam messages. Dim files() As String = Directory.GetFiles("C:\AntiSpam\Spam", "*.eml") For Each file As String In files msg.LoadMessage(file) filter.TrainFilter(msg, True) ' Mark as spam. Next ' Train Bayesian filter for non-spam messages. files = Directory.GetFiles("C:\AntiSpam\NonSpam", "*.eml") For Each file As String In files msg.LoadMessage(file) filter.TrainFilter(msg, False) ' Mark as non-spam. Next ' Save Bayesian database to disk. filter.SaveDatabase("C:\AntiSpam\spam.dat", "C:\AntiSpam\nonspam.dat") End Sub End Module
BayesFilter Class | MailBee.AntiSpam Namespace