当项目中的敏感词数量不是很多的时候,直接用数组来遍历过滤其实也可以,但是具体的数量有多大,这个肯定不好说,因此,对.txt中的敏感词合理组织后再进行过滤就显得非常有必要了。
如上图,左边是txt中配置的敏感词,右边是需要转换为的数据结构(C#中的Dictory来存储即可)
下面那句话是一个示例,即需要过滤敏感词的句子。
具体的实现代码:
1 using System; 2 using System.IO; 3 using System.Collections; 4 using UnityEngine; 5 using Common; 6 using System.Collections.Generic; 7 using System.Text; 8 9 public class FilterSensitiveWords 10 { 11 static string[] sensitiveWordsArray = null; 12 static string fileName = "sensitivewords.u"; 13 static string ReplaceValue = "*"; 14 static Dictionary<char, IList<string>> keyDict; 15 16 public static void Initialize() 17 { 18 string path = "txt/sensitivewords.u"; 19 string name = "sensitivewords"; 20 LoadHelp.LoadAssetBundle(path, name, ar => 21 { 22 string content = ar.Bundle.LoadAsset<TextAsset>(name).text; 23 if (!(content.Equals("") || content.Equals(null))) 24 { 25 sensitiveWordsArray = content.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); 26 27 keyDict = new Dictionary<char, IList<string>>(); 28 foreach (string s in sensitiveWordsArray) 29 { 30 if (string.IsNullOrEmpty(s)) 31 continue; 32 if (keyDict.ContainsKey(s[0])) 33 keyDict[s[0]].Add(s.Trim(new char[] { '\r' })); 34 else 35 keyDict.Add(s[0], new List<string> { s.Trim(new char[] { '\r' }) }); 36 } 37 } 38 }); 39 } 40 41 //判断一个字符串是否包含敏感词,包括含的话将其替换为* 42 public static bool IsContainSensitiveWords(ref string text, out string SensitiveWords) 43 { 44 bool isFind = false; 45 SensitiveWords = ""; 46 if (null == sensitiveWordsArray || string.IsNullOrEmpty(text)) 47 return isFind; 48 49 int len = text.Length; 50 StringBuilder sb = new StringBuilder(len); 51 bool isOK = true; 52 for (int i = 0; i < len; i++) 53 { 54 if (keyDict.ContainsKey(text[i])) 55 { 56 foreach (string s in keyDict[text[i]]) 57 { 58 isOK = true; 59 int j = i; 60 foreach (char c in s) 61 { 62 if (j >= len || c != text[j++]) 63 { 64 isOK = false; 65 break; 66 } 67 } 68 if (isOK) 69 { 70 SensitiveWords = s; 71 isFind = true; 72 i += s.Length - 1; 73 sb.Append('*', s.Length); 74 break; 75 } 76 77 } 78 if (!isOK) 79 sb.Append(text[i]); 80 } 81 else 82 sb.Append(text[i]); 83 } 84 if (isFind) 85 text = sb.ToString(); 86 87 return isFind; 88 } 89 }