/// <summary> /// This method runs our bayes spam filter after the threshold was calibrated /// and prints the results /// </summary> /// <param name="folderPath"> Path to the email folder. </param> private void RunBayesSpamFilter(string folderPath) { if (Directory.Exists(folderPath)) { var isInSpamFolder = folderPath.Equals(spamTestPath); List <double> errors = new List <double>(); var filePaths = Directory.GetFiles(folderPath); var spamChecker = new SpamChecker(wordInfoDictionary); // The spam probabilities of each file are calculated and all the errors are added to a separate list foreach (var filePath in filePaths) { var spamProbability = spamChecker.GetSpamProbability(filePath); if (isInSpamFolder && spamProbability <= threshold) { errors.Add(spamProbability);// These should be spam, but aren't marked by our filter } else if (!isInSpamFolder && spamProbability >= threshold) { errors.Add(spamProbability);// These should be ham, but aren't marked by our filter } } PrintTestResult(folderPath, isInSpamFolder, errors, filePaths); } else { throw new DirectoryNotFoundException(); } }
/// <summary> /// Count all wrongly marked spam files. /// </summary> /// <param name="spamFilePaths"> Path to spam files. </param> /// <param name="spamChecker"> The checker to use. </param> /// <returns></returns> private int GetSpamMarkedAsHamCounter(string[] spamFilePaths, SpamChecker spamChecker) { var spamMarkedAsHam = 0; foreach (var spamFilePath in spamFilePaths) { var spamProbability = spamChecker.GetSpamProbability(spamFilePath); if (spamProbability < threshold) { spamMarkedAsHam++; } } return(spamMarkedAsHam); }