Bad words
List of examples:
- Creating a bad word
- Updating a bad word
- Updating multiple bad words
- Checking text for a bad word
- Checking text for all bad words defined in the system
- Deleting a bad word
Creating a bad word
// Creates a new bad word object
BadWordInfo newWord = new BadWordInfo();
// Sets the bad word properties
newWord.WordExpression = "testbadword";
newWord.WordAction = BadWordActionEnum.ReportAbuse;
newWord.WordIsGlobal = true;
newWord.WordIsRegularExpression = false;
// Saves the bad word to the database
BadWordInfoProvider.SetBadWordInfo(newWord);
Updating a bad word
// Prepares a condition for loading a specific bad word
var where = new WhereCondition("WordExpression", QueryOperator.Equals, "testbadword");
// Gets the bad word based on the condition
var words = BadWordInfoProvider.GetBadWords().Where(where);
BadWordInfo word = words.FirstOrDefault();
if (word != null)
{
// Updates the bad word properties
word.WordAction = BadWordActionEnum.Replace;
word.WordReplacement = "good";
// Saves the changes to the database
BadWordInfoProvider.SetBadWordInfo(word);
}
Updating multiple bad words
// Prepares a condition for loading all bard words containing the word 'bad'
var where = new WhereCondition("WordExpression", QueryOperator.Like, "%bad%");
// Gets the bad words based on the condition
var words = BadWordInfoProvider.GetBadWords().Where(where);
// Loops through individual bad words
foreach (BadWordInfo word in words)
{
// Updates the properties
word.WordAction = BadWordActionEnum.Replace;
word.WordReplacement = "good";
// Saves the updated bad word
BadWordInfoProvider.SetBadWordInfo(word);
}
Checking text for a bad word
// Prepares a condition for loading a specific bad word
var where = new WhereCondition("WordExpression", QueryOperator.Equals, "testbadword");
// Gets the bad word based on the condition
var words = BadWordInfoProvider.GetBadWords().Where(where);
if (words.Count > 0)
{
// Gets the object representing the bad word from the collection of results
BadWordInfo badWord = words.FirstOrDefault();
// Creates a string to be checked for the presence of the bad word
string text = "This is a string containing the sample testbadword";
// Creates a hashtable that will contain found bad words
Hashtable foundWords = new Hashtable();
// Modifies the string according to the found bad words and returns the action which should be performed
BadWordActionEnum action = BadWordInfoProvider.CheckBadWord(badWord, null, null, ref text, foundWords, 0);
if (foundWords.Count != 0)
{
switch (action)
{
case BadWordActionEnum.Deny:
// Perform additional actions here
break;
case BadWordActionEnum.RequestModeration:
// Perform additional actions here
break;
case BadWordActionEnum.Remove:
// Perform additional actions here
break;
case BadWordActionEnum.Replace:
// Perform additional actions here
break;
case BadWordActionEnum.ReportAbuse:
// Perform additional actions here
break;
case BadWordActionEnum.None:
// Perform additional actions here
break;
}
}
else
{
// The bad word is not present in the checked string
}
}
Checking text for all bad words defined in the system
// Creates a string to be checked for the presence of bad words
string text = "This is a string containing the sample testbadword.";
// Creates a hashtable that will contain found bad words
Hashtable foundWords = new Hashtable();
// Modifies the string according to the found bad words and returns the action which should be performed
BadWordActionEnum action = BadWordInfoProvider.CheckAllBadWords(null, null, ref text, foundWords);
if (foundWords.Count != 0)
{
switch (action)
{
case BadWordActionEnum.Deny:
// Perform additional actions here
break;
case BadWordActionEnum.RequestModeration:
// Perform additional actions here
break;
case BadWordActionEnum.Remove:
// Perform additional actions here
break;
case BadWordActionEnum.Replace:
// Perform additional actions here
break;
case BadWordActionEnum.ReportAbuse:
// Perform additional actions here
break;
case BadWordActionEnum.None:
// Perform additional actions here
break;
}
}
else
{
// The bad words are not present in the checked string
}
Deleting a bad word
// Prepares a condition for loading a specific bad word
var where = new WhereCondition("WordExpression", QueryOperator.Equals, "testbadword");
// Gets the bad word based on the condition
var words = BadWordInfoProvider.GetBadWords().Where(where);
BadWordInfo word = words.FirstOrDefault();
if (word != null)
{
// Deletes the bad word
BadWordInfoProvider.DeleteBadWordInfo(word);
}