A static dialogue profanity filter. Holds one whole-word regex covering Russian, English and Spanish swear words, and provides Normalize, HasStrongLanguage and Format, which either masks matches with *** or replaces them with an ellipsis in the clean variant.
using System;
using System.Text.RegularExpressions;
namespace Socer;
public enum DialogueCensorship { Clean=0,Masked=1 }
public static class DialogueFilter
{
// Whole words keep ordinary football words such as «страхуй» intact.
static readonly Regex Strong=new(@"\b(?:бля(?:дь)?|бляд\p{L}*|[её]б\p{L}*|за[её]б\p{L}*|объ[её]б\p{L}*|нахуй|о?ху[йеяё]\p{L}*|хер\p{L}*|пизд\p{L}*|сука|суки|суч\p{L}*|мудак\p{L}*|долбо[её]б\p{L}*|fuck\p{L}*|shit\p{L}*|bullshit|asshole\p{L}*|bitch\p{L}*|joder|jodid\p{L}*|coño|mierda|cabrón)\b",RegexOptions.IgnoreCase|RegexOptions.CultureInvariant);
public static DialogueCensorship Normalize(int value)=>value is >=0 and <=1?(DialogueCensorship)value:DialogueCensorship.Masked;
public static bool HasStrongLanguage(string text)=>Strong.IsMatch(text??"");
public static string Format(string raw,string clean,DialogueCensorship mode)
{
raw??="";
return mode switch
{
DialogueCensorship.Clean=>Strong.Replace(string.IsNullOrEmpty(clean)?raw:clean,"…"),
_=>Strong.Replace(raw,"***")
};
}
}