/// <summary> /// Filters a tweet both on illegal characters and bad words /// </summary> /// <param name="tweet">Tweet that needs to be filtered</param> /// <returns>Filtered tweet or null if illegal tweet</returns> public Tweet FilterTweet(Tweet tweet) { if (tweet == null) return null; else if (FilterWords(tweet)) return FilterChars(tweet); return null; }
/// <summary> /// Filters a tweet on illegal characters /// </summary> /// <param name="tweet">Tweet that needs to be filtered on illegal characters/param> /// <returns>Filtered tweet</returns> private Tweet FilterChars(Tweet tweet) { StringBuilder newText = new StringBuilder(); bool appended = false; foreach (char w in tweet.Text.ToCharArray()) { // Check if we can translate the character foreach (DictionaryEntry pair in translateCharacters) { if (w.ToLower() == (char)pair.Key) { newText.Append(pair.Value); appended = true; break; } } // Check if we were able to map the character to another character, else check if illegal if (!appended) { foreach (char c in illegalCharacters) { if (w.ToLower() == c) { appended = true; newText.Append(" "); break; } } } if (!appended) newText.Append(w); appended = false; } tweet.Text = newText.ToString().ToUpper(); return tweet; }
/// <summary> /// Filters a tweet on bad words /// </summary> /// <param name="tweet">Tweet that needs to be filtered on bad words</param> /// <returns>Filtered tweet or null if illegal tweet</returns> private bool FilterWords(Tweet tweet) { foreach (string woord in slechteWoorden) { foreach (string tweetWoord in tweet.Text.Split(' ')) { if (tweetWoord.ToLower() == woord.ToLower()) { return false; } } } return true; }