### Installation Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Install the Dotnet Bad Word Detector library using the .NET CLI. ```APIDOC ## Installation ```bash dotnet add package DotnetBadWordDetector ``` ``` -------------------------------- ### Install DotnetBadWordDetector Package Source: https://github.com/felipeluz/dotnet-bad-word-detector-and-filter/blob/main/README.md Use the dotnet CLI to add the package to your project. ```bash dotnet add package DotnetBadWordDetector ``` -------------------------------- ### Get Profanity Probability Source: https://github.com/felipeluz/dotnet-bad-word-detector-and-filter/blob/main/README.md Obtain the probability score that a word or phrase is offensive using `GetProfanityProbability` or `GetPhraseProfanityProbability`. ```csharp float probWord = detector.GetProfanityProbability("example"); float probPhrase = detector.GetPhraseProfanityProbability("this is an example"); ``` -------------------------------- ### Get Profanity Probability for Words Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Calculates the probability (0-1) that a word is profane. Useful for custom filtering based on confidence thresholds. Ensure the ProfanityDetector is initialized. ```csharp using DotnetBadWordDetector; var detector = new ProfanityDetector(); // Get probability scores for words float prob1 = detector.GetProfanityProbability("hello"); Console.WriteLine($"Probability: {prob1:P2}"); // e.g., "Probability: 2.50%" float prob2 = detector.GetProfanityProbability("suspiciousword"); Console.WriteLine($"Probability: {prob2:P2}"); // e.g., "Probability: 87.30%" // Implement custom threshold-based filtering string word = "example"; float probability = detector.GetProfanityProbability(word); if (probability > 0.9f) { Console.WriteLine("High confidence: definitely profane"); } else if (probability > 0.5f) { Console.WriteLine("Medium confidence: possibly profane, flag for review"); } else { Console.WriteLine("Low confidence: likely clean"); } ``` -------------------------------- ### Get Phrase Profanity Probability Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Determines the probability that a phrase contains profane words by analyzing each word. Ideal for scoring entire sentences for content moderation. The detector must be initialized. ```csharp using DotnetBadWordDetector; var detector = new ProfanityDetector(); // Get phrase-level probability scores float phraseProb = detector.GetPhraseProfanityProbability("this is a test sentence"); Console.WriteLine($"Phrase profanity probability: {phraseProb:P2}"); // Compare multiple user comments string[] comments = { "Great product, highly recommend!", "This is terrible garbage", "Normal feedback here" }; foreach (var comment in comments) { float score = detector.GetPhraseProfanityProbability(comment); Console.WriteLine($"Comment: \"{comment}\" - Score: {score:P2}"); } // Output example: // Comment: "Great product, highly recommend!" - Score: 3.20% // Comment: "This is terrible garbage" - Score: 45.80% // Comment: "Normal feedback here" - Score: 2.10% ``` -------------------------------- ### Initialize ProfanityDetector (Default and Multilingual) Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Create a new instance of the ProfanityDetector. The default constructor loads the English locale. Setting `allLocales: true` loads all supported language models, but may increase false positives in mixed-language texts. Keep the detector instance in memory for optimal performance. ```csharp using DotnetBadWordDetector; // Create detector with English only (default) - recommended for best performance var detector = new ProfanityDetector(); // Create detector with all supported locales (English, Spanish, Portuguese) // Note: Multiple locales may cause false-positives in multilingual texts var multilingualDetector = new ProfanityDetector(allLocales: true); // Keep the detector instance in memory for better performance // It uses very little memory (less than 100 KB) ``` -------------------------------- ### Create ProfanityDetector Instance Source: https://github.com/felipeluz/dotnet-bad-word-detector-and-filter/blob/main/README.md Instantiate the ProfanityDetector. By default, it loads English language models. Set `allLocales` to true to include Spanish and Portuguese. ```csharp using DotnetBadWordDetector; // English only (default) var detector = new ProfanityDetector(); // Or load all supported languages: English, Spanish, and Portuguese var detectorAll = new ProfanityDetector(allLocales: true); ``` -------------------------------- ### Initialize ProfanityDetector (Specific Locales) Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Instantiate the ProfanityDetector with specific language locales for targeted multilingual support. Available locales include ENGLISH, SPANISH, PORTUGUESE, and RUSSIAN_CYRILLIC. ```csharp using DotnetBadWordDetector; using DotnetBadWordDetector.Model; // Create detector for Spanish and Portuguese only var latinDetector = new ProfanityDetector(Locales.SPANISH, Locales.PORTUGUESE); // Create detector for English and Russian var bilingualDetector = new ProfanityDetector(Locales.ENGLISH, Locales.RUSSIAN_CYRILLIC); // Create detector for a single non-English locale var spanishOnlyDetector = new ProfanityDetector(Locales.SPANISH); ``` -------------------------------- ### ProfanityDetector Constructor (Default) Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Initialize the ProfanityDetector with default settings (English locale) or load all supported language models for multilingual detection. ```APIDOC ## ProfanityDetector Constructor (Default) Creates a new profanity detector instance with English locale by default, or optionally loads all supported language models (English, Spanish, Portuguese) for multilingual detection. ```csharp using DotnetBadWordDetector; // Create detector with English only (default) - recommended for best performance var detector = new ProfanityDetector(); // Create detector with all supported locales (English, Spanish, Portuguese) // Note: Multiple locales may cause false-positives in multilingual texts var multilingualDetector = new ProfanityDetector(allLocales: true); // Keep the detector instance in memory for better performance // It uses very little memory (less than 100 KB) ``` ``` -------------------------------- ### ProfanityDetector Constructor (Specific Locales) Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Initialize the ProfanityDetector with specific language locales for targeted multilingual support. ```APIDOC ## ProfanityDetector Constructor (Specific Locales) Creates a profanity detector with specific language locales for targeted multilingual support. Available locales are ENGLISH, SPANISH, PORTUGUESE, and RUSSIAN_CYRILLIC. ```csharp using DotnetBadWordDetector; using DotnetBadWordDetector.Model; // Create detector for Spanish and Portuguese only var latinDetector = new ProfanityDetector(Locales.SPANISH, Locales.PORTUGUESE); // Create detector for English and Russian var bilingualDetector = new ProfanityDetector(Locales.ENGLISH, Locales.RUSSIAN_CYRILLIC); // Create detector for a single non-English locale var spanishOnlyDetector = new ProfanityDetector(Locales.SPANISH); ``` ``` -------------------------------- ### Configure Profanity Detector with Locales Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Initializes the ProfanityDetector with specific language models using the Locales enum. Supports single or multiple language models for multi-region applications. Defaults to English if no locale is specified. ```csharp using DotnetBadWordDetector; using DotnetBadWordDetector.Model; // Available locales // Locales.ENGLISH - English language model // Locales.SPANISH - Spanish language model // Locales.PORTUGUESE - Portuguese language model // Locales.RUSSIAN_CYRILLIC - Russian (Cyrillic script) model // Example: Create detector based on user's language preference string userLanguage = "es"; // Spanish ProfanityDetector detector = userLanguage switch { "en" => new ProfanityDetector(Locales.ENGLISH), "es" => new ProfanityDetector(Locales.SPANISH), "pt" => new ProfanityDetector(Locales.PORTUGUESE), "ru" => new ProfanityDetector(Locales.RUSSIAN_CYRILLIC), _ => new ProfanityDetector() // Default to English }; // Multi-region application supporting multiple languages var globalDetector = new ProfanityDetector( Locales.ENGLISH, Locales.SPANISH, Locales.PORTUGUESE ); ``` -------------------------------- ### Complete Content Moderation Service Integration Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Integrates the ProfanityDetector into a service to moderate user-generated content. It handles different levels of profanity with custom thresholds and provides a structured moderation result. Initialize the detector once for optimal performance. ```csharp using DotnetBadWordDetector; using DotnetBadWordDetector.Model; public class ContentModerationService { private readonly ProfanityDetector _detector; private const float HighConfidenceThreshold = 0.85f; private const float ReviewThreshold = 0.50f; public ContentModerationService(bool multilingual = false) { // Initialize once and reuse - detector uses minimal memory (<100KB) _detector = multilingual ? new ProfanityDetector(allLocales: true) : new ProfanityDetector(); } public ModerationResult ModerateContent(string content) { // Quick check for any profanity if (!_detector.IsPhraseProfane(content)) { return new ModerationResult { Status = ModerationStatus.Approved, CleanContent = content, Confidence = 1.0f - _detector.GetPhraseProfanityProbability(content) }; } // Get probability for confidence scoring float probability = _detector.GetPhraseProfanityProbability(content); if (probability >= HighConfidenceThreshold) { return new ModerationResult { Status = ModerationStatus.Rejected, CleanContent = _detector.MaskProfanity(content, '*'), Confidence = probability, Reason = "High-confidence profanity detected" }; } else if (probability >= ReviewThreshold) { return new ModerationResult { Status = ModerationStatus.NeedsReview, CleanContent = _detector.MaskProfanity(content, '*'), Confidence = probability, Reason = "Possible profanity - requires human review" }; } return new ModerationResult { Status = ModerationStatus.Approved, CleanContent = content, Confidence = 1.0f - probability }; } } public enum ModerationStatus { Approved, Rejected, NeedsReview } public class ModerationResult { public ModerationStatus Status { get; set; } public string CleanContent { get; set; } public float Confidence { get; set; } public string Reason { get; set; } } // Usage var service = new ContentModerationService(multilingual: true); var result = service.ModerateContent("User submitted comment here"); Console.WriteLine($"Status: {result.Status}"); Console.WriteLine($"Clean Content: {result.CleanContent}"); Console.WriteLine($"Confidence: {result.Confidence:P2}"); ``` -------------------------------- ### GetPhraseProfanityProbability Method Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Analyzes a phrase by checking each word individually and returning the highest profanity probability found. ```APIDOC ## GetPhraseProfanityProbability ### Description Gets the probability that a phrase contains profane words by analyzing each word individually and returning the highest probability found. ### Parameters - **phrase** (string) - Required - The phrase or sentence to analyze. ### Response - **probability** (float) - The highest profanity probability found within the phrase. ``` -------------------------------- ### Locales Enum Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Defines supported language locales for profanity detection models. ```APIDOC ## Locales ### Description Defines the available language locales for profanity detection. Each locale corresponds to a separate ML model. ### Supported Locales - **Locales.ENGLISH** - **Locales.SPANISH** - **Locales.PORTUGUESE** - **Locales.RUSSIAN_CYRILLIC** ``` -------------------------------- ### MaskProfanity Method Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Replaces offensive words in a string with a specified masking character. ```APIDOC ## MaskProfanity ### Description Masks offensive words in a phrase by replacing them with a specified character. ### Parameters - **original** (string) - Required - The text to be masked. - **maskChar** (char) - Optional - The character used for masking (defaults to '*'). ### Response - **maskedText** (string) - The resulting string with profanity masked. ``` -------------------------------- ### GetProfanityProbability Method Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Calculates the probability (0 to 1) that a specific word is profane, returning the highest score across loaded language models. ```APIDOC ## GetProfanityProbability ### Description Gets the probability (0 to 1) that a given word or small sentence is profane. Returns the highest probability across all loaded language models. ### Parameters - **word** (string) - Required - The word or small sentence to analyze. ### Response - **probability** (float) - The calculated probability score between 0 and 1. ``` -------------------------------- ### IsProfane Method Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Predicts whether a given word or small sentence is profane. The method cleans the input before analysis and returns true if any loaded language model classifies the input as profane. ```APIDOC ## IsProfane Method Predicts whether a given word or small sentence is profane. The method cleans the input by removing special characters (keeping alphanumeric, spaces, and @ symbols) before analysis. Returns true if any of the loaded language models classifies the word as profane. ```csharp using DotnetBadWordDetector; var detector = new ProfanityDetector(); // Check individual words bool isProfane1 = detector.IsProfane("hello"); // Returns: false bool isProfane2 = detector.IsProfane("badword"); // Returns: true (if offensive) // Also handles creative substitutions due to ML model bool isProfane3 = detector.IsProfane("h3ll0"); // ML model can detect obfuscated words // Use in content moderation flow string userInput = "example"; if (detector.IsProfane(userInput)) { Console.WriteLine("Content rejected: offensive language detected"); } else { Console.WriteLine("Content approved"); } ``` ``` -------------------------------- ### Check if a Word is Offensive Source: https://github.com/felipeluz/dotnet-bad-word-detector-and-filter/blob/main/README.md Use the `IsProfane` method to determine if a single word is classified as offensive. ```csharp if (detector.IsProfane("example")) { // Word is classified as offensive } ``` -------------------------------- ### Mask Profanity in Text Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Replaces offensive words in a string with a specified masking character, defaulting to '*'. Useful for sanitizing user-generated content. The detector needs to be instantiated. ```csharp using DotnetBadWordDetector; var detector = new ProfanityDetector(); // Mask profanity with default asterisks string original = "This contains badword in the middle"; string masked = detector.MaskProfanity(original); // Returns: "This contains ******* in the middle" (if "badword" is 7 chars) // Use custom masking character string maskedWithHash = detector.MaskProfanity(original, '#'); // Returns: "This contains ####### in the middle" // Real-world chat application example string[] chatMessages = { "Hello everyone!", "This badword ruined my day", "See you later!" }; Console.WriteLine("--- Moderated Chat ---"); foreach (var message in chatMessages) { string cleanMessage = detector.MaskProfanity(message, '*'); Console.WriteLine(cleanMessage); } ``` -------------------------------- ### Check if a Word or Sentence is Profane Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Use the `IsProfane` method to predict if a given word or short sentence contains offensive language. The method preprocesses input by removing special characters. It returns true if any loaded model flags the input as profane, and can detect obfuscated words due to its ML model. ```csharp using DotnetBadWordDetector; var detector = new ProfanityDetector(); // Check individual words bool isProfane1 = detector.IsProfane("hello"); // Returns: false bool isProfane2 = detector.IsProfane("badword"); // Returns: true (if offensive) // Also handles creative substitutions due to ML model bool isProfane3 = detector.IsProfane("h3ll0"); // ML model can detect obfuscated words // Use in content moderation flow string userInput = "example"; if (detector.IsProfane(userInput)) { Console.WriteLine("Content rejected: offensive language detected"); } else { Console.WriteLine("Content approved"); } ``` -------------------------------- ### Check if a Phrase Contains Offensive Words Source: https://github.com/felipeluz/dotnet-bad-word-detector-and-filter/blob/main/README.md Use the `IsPhraseProfane` method to check if any part of a given text phrase is offensive. ```csharp if (detector.IsPhraseProfane("this is an example")) { // Phrase contains at least one offensive word } ``` -------------------------------- ### IsPhraseProfane Method Source: https://context7.com/felipeluz/dotnet-bad-word-detector-and-filter/llms.txt Checks if a phrase contains any profane words by splitting the phrase into individual words and checking each one. Returns true if at least one word in the phrase is classified as profane. ```APIDOC ## IsPhraseProfane Method Checks if a phrase contains any profane words by splitting the phrase into individual words and checking each one. Returns true if at least one word in the phrase is classified as profane. ```csharp using DotnetBadWordDetector; var detector = new ProfanityDetector(); // Check entire phrases for offensive content bool containsProfanity = detector.IsPhraseProfane("this is a clean sentence"); // Returns: false bool hasBadWord = detector.IsPhraseProfane("this sentence contains badword here"); // Returns: true (if "badword" is offensive) // Content moderation example for user-generated content string comment = "Great article, thanks for sharing!"; if (detector.IsPhraseProfane(comment)) { Console.WriteLine("Comment blocked due to inappropriate content"); } else { Console.WriteLine("Comment posted successfully"); } ``` ``` -------------------------------- ### Mask Offensive Words in a Phrase Source: https://github.com/felipeluz/dotnet-bad-word-detector-and-filter/blob/main/README.md Replace detected offensive words within a phrase using the `MaskProfanity` method. Specify a replacement character, such as an asterisk. ```csharp string cleanText = detector.MaskProfanity("this is an example", '*'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.