public string FilterOutNegativeWords(string text, bool skipFiltering, IBannedWordsRepo bannedWordsRepo) { var bannedWordsCollection = bannedWordsRepo.GetBannedWordsList(); int count = 0; if (!String.IsNullOrEmpty(text)) { bannedWordsCollection.ForEach((w) => { if (text.Contains(w)) { if (!skipFiltering) text = text.Replace(w, MaskWord(w)); count += 1; } }); } Console.WriteLine("Negative words found: {0}", count); Console.WriteLine("Negative words filtered: {0}", skipFiltering ? 0 : count); Console.WriteLine("Text after filtering: {0}", text); return text; }
public int CountBannedWords(string text, IBannedWordsRepo bannedWordsRepo) { if (String.IsNullOrEmpty(text)) return 0; var textToLookup = RemoveSpecialCharactersFromText(text) .Split(null) .GroupBy(s => s).Select(s => new { word = s.Key.Trim(), occurances = s.Count() }) .ToDictionary(g => g.word, g => g.occurances); Console.WriteLine("--------------------"); Console.WriteLine("Banned words found: "); int count = 0; var bannedWordsCollection = bannedWordsRepo.GetBannedWordsList(); bannedWordsCollection.ForEach((w) => { if (textToLookup.ContainsKey(w.Trim())) { count += textToLookup[w]; Console.Write(w + " "); } }); Console.WriteLine(); return count; }