1 using System.Collections.Generic;
7 static class ForbiddenWordFilter
9 static readonly
string fileListPath = Path.Combine(
"Data",
"forbiddenwordlist.txt");
11 private static readonly HashSet<string> forbiddenWords;
13 static ForbiddenWordFilter()
17 forbiddenWords = File.ReadAllLines(fileListPath).Select(s => s.ToLowerInvariant()).ToHashSet();
19 catch (System.IO.IOException e)
21 DebugConsole.ThrowError($
"Failed to load the list of forbidden words from {fileListPath}.", e);
25 public static bool IsForbidden(
string text)
27 return IsForbidden(text, out _);
30 public static bool IsForbidden(
string text, out
string forbiddenWord)
32 forbiddenWord =
string.Empty;
33 if (forbiddenWords ==
null)
38 char[] delimiters =
new char[] {
' ',
'-',
'.',
'_',
':',
';',
'\'' };
40 HashSet<string> words =
new HashSet<string>();
41 foreach (
char delimiter
in delimiters)
43 foreach (
string word
in text.Split(delimiter))
45 words.Add(word.ToLowerInvariant());
49 text = text.ToLowerInvariant();
50 foreach (
string forbidden
in forbiddenWords)
52 if (forbidden.Contains(
' '))
54 if (words.Contains(forbidden.Trim()))
56 forbiddenWord = forbidden.Trim();
62 if (text.Contains(forbidden))
64 forbiddenWord = forbidden.Trim();