Unity中使用的一套敏感詞過濾方式


當項目中的敏感詞數量不是很多的時候,直接用數組來遍歷過濾其實也可以,但是具體的數量有多大,這個肯定不好說,因此,對.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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM