C# 添加敏感词

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
   public class CheckStreamReader
{
    //使用的数据:
    private static HashSet<string> hash = new HashSet<string>();
    private byte[] fastCheck = new byte[char.MaxValue];
    private BitArray charCheck = new BitArray(char.MaxValue);
    private int maxWordLength = 0;
    private int minWordLength = int.MaxValue;
    private static string[] badwords = { };
 
 
    public CheckStreamReader()
    {
        if (hash == null || hash.Count <= 0)
        {
            //添加敏感词
            string path = HttpContext.Current.Server.MapPath("~/config") + "/StreamReader.txt";
            StreamReader sr = new StreamReader(path, Encoding.GetEncoding("utf-8"));
            string strText = sr.ReadToEnd();
            badwords = strText.Split('|');
            InitializationText();
        }
    }
 
 
    //初始化数据的代码:将敏感词加入的hash表中
    private void InitializationText()
    {
        foreach (string word in badwords)
        {
            maxWordLength = Math.Max(maxWordLength, word.Length);
            minWordLength = Math.Min(minWordLength, word.Length);
 
            for (int i = 0; i < 7 && i < word.Length; i++)
            {
                fastCheck[word[i]] |= (byte)(1 << i);
            }
 
            for (int i = 7; i < word.Length; i++)
            {
                fastCheck[word[i]] |= 0x80;
            }
 
            if (word.Length == 1)
            {
                charCheck[word[0]] = true;
            }
            else
            {
                hash.Add(word);
            }
        }
    }
 
    //判断是否包含脏字的代码:
    public bool HasBadWord(string text)
    {
        if (hash == null || hash.Count<=0)
        {
            string path = HttpContext.Current.Server.MapPath("~/" + ConfigurationManager.AppSettings["ConfigPath"]) + "/StreamReader.txt";
            StreamReader sr = new StreamReader(path, Encoding.GetEncoding("utf-8"));
            string strText = sr.ReadToEnd();
            badwords = strText.Split('|');
            InitializationText();
        }
 
        int index = 0;
 
        while (index < text.Length)
        {
            if ((fastCheck[text[index]] & 1) == 0)
            {
                while (index < text.Length - 1 && (fastCheck[text[++index]] & 1) == 0) ;
            }
 
            if (minWordLength == 1 && charCheck[text[index]])
            {
                return true;
            }
 
            for (int j = 1; j <= Math.Min(maxWordLength, text.Length - index - 1); j++)
            {
                if ((fastCheck[text[index + j]] & (1 << Math.Min(j, 7))) == 0)
                {
                    break;
                }
 
                if (j + 1 >= minWordLength)
                {
                    string sub = text.Substring(index, j + 1);
 
                    if (hash.Contains(sub))
                    {
                        return true;
                    }
                }
            }
 
            index++;
        }
 
        return false;
    }
}

  

posted @   逊老头  阅读(616)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示