public static void Run() { // ExStart:PrintHeaderUsingMailMessageSaveOptions string hamFolder = RunExamples.GetDataDir_Email() + "/hamFolder"; string spamFolder = RunExamples.GetDataDir_Email() + "/Spam"; string testFolder = RunExamples.GetDataDir_Email(); string dataBaseFile = RunExamples.GetDataDir_Email() + "SpamFilterDatabase.txt"; TeachAndCreateDatabase(hamFolder, spamFolder, dataBaseFile); string[] testFiles = Directory.GetFiles(testFolder, "*.eml"); SpamAnalyzer analyzer = new SpamAnalyzer(dataBaseFile); foreach (string file in testFiles) { MailMessage msg = MailMessage.Load(file); Console.WriteLine(msg.Subject); double probability = analyzer.Test(msg); PrintResult(probability); } // ExEnd:PrintHeaderUsingMailMessageSaveOptions }
private static void TeachAndCreateDatabase(string hamFolder, string spamFolder, string dataBaseFile) { string[] hamFiles = Directory.GetFiles(hamFolder, "*.eml"); string[] spamFiles = Directory.GetFiles(spamFolder, "*.eml"); SpamAnalyzer analyzer = new SpamAnalyzer(); for (int i = 0; i < hamFiles.Length; i++) { MailMessage hamMailMessage; try { hamMailMessage = MailMessage.Load(hamFiles[i]); } catch (Exception) { continue; } Console.WriteLine(i); analyzer.TrainFilter(hamMailMessage, false); } for (int i = 0; i < spamFiles.Length; i++) { MailMessage spamMailMessage; try { spamMailMessage = MailMessage.Load(hamFiles[i]); } catch (Exception) { continue; } Console.WriteLine(i); analyzer.TrainFilter(spamMailMessage, true); } analyzer.SaveDatabase(dataBaseFile); }