MailBee.NET Objects 4.0

BayesFilter.TrainFilter Method 

Learns from the specified message as from spam or non-spam source.

public void TrainFilter(
   MailMessage message,
   bool isSpam
);

Parameters

message
A reference to the MailMessage object representing the message to train the Bayesian filter.
isSpam
true if the message is a spam; false if it's a legitimate message.

Remarks

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.

Exceptions

Exception TypeCondition
MailBeeInvalidArgumentException message is a null reference (Nothing in Visual Basic).

Example

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

See Also

BayesFilter Class | MailBee.AntiSpam Namespace