派生自 projectDept/qhighschool

EricsHu
2023-11-26 bf13676673e9f0f090a68bd8cfbaefbf0d696d32
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.qxueyou.scc.Sensitive;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
 
 
//屏蔽敏感词初始化
 
 
@SuppressWarnings({"rawtypes", "unchecked"})
public class SensitiveWordInit {
    // 字符编码
    private String ENCODING = "UTF-8";
 
    // 初始化敏感字库
    public Map initKeyWord() {
// 读取敏感词库 ,存入Set中
        Set<String> wordSet = readSensitiveWordFile();
// 将敏感词库加入到HashMap中//确定有穷自动机DFA
        return addSensitiveWordToHashMap(wordSet);
    }
 
 
    /**
     * 读取敏感词库,将敏感词放入HashSet中,构建一个DFA算法模型:<br>
     * 中 = {
     * isEnd = 0
     * 国 = {
     * isEnd = 1
     * 人 = {isEnd = 0
     * 民 = {isEnd = 1}
     * }
     * 男  = {
     * isEnd = 0
     * 人 = {
     * isEnd = 1
     * }
     * }
     * }
     * }
     * 五 = {
     * isEnd = 0
     * 星 = {
     * isEnd = 0
     * 红 = {
     * isEnd = 0
     * 旗 = {
     * isEnd = 1
     * }
     * }
     * }
     * }
     */
 
 
    // 读取敏感词库 ,存入HashMap中
    private Set<String> readSensitiveWordFile() {
 
 
        Set<String> wordSet = null;
// app为项目地址
        /*
         * String app = System.getProperty("user.dir"); System.out.println(app);
         * URL resource = Thread.currentThread().getContextClassLoader()
         * .getResource("/"); String path = resource.getPath().substring(1);
         * System.out.println(path); File file = new File(path +
         * "censorwords.txt");
         */
 
 
//敏感词库
        File file = new File(
                "src/main/resources/SensitiveWordList.txt");
        try {
// 读取文件输入流
            InputStreamReader read = new InputStreamReader(new FileInputStream(
                    file), ENCODING);
 
// 文件是否是文件 和 是否存在
            if (file.isFile() && file.exists()) {
 
                wordSet = new HashSet<String>();
// StringBuffer sb = new StringBuffer();
// BufferedReader是包装类,先把字符读到缓存里,到缓存满了,再读入内存,提高了读的效率。
                BufferedReader br = new BufferedReader(read);
                String txt = null;
 
 
// 读取文件,将文件内容放入到set中
                while ((txt = br.readLine()) != null) {
                    wordSet.add(txt);
                }
 
 
                br.close();
 
 
                /*
                 * String str = sb.toString(); String[] ss = str.split(","); for
                 * (String s : ss) { wordSet.add(s); }
                 */
            }
 
 
// 关闭文件流
            read.close();
 
 
        } catch (Exception e) {
            e.printStackTrace();
        }
 
 
        return wordSet;
    }
 
 
    // 将HashSet中的敏感词,存入HashMap中
    private Map addSensitiveWordToHashMap(Set<String> wordSet) {
 
 
// 初始化敏感词容器,减少扩容操作
        Map wordMap = new HashMap(wordSet.size());
 
 
        for (String word : wordSet) {
            Map nowMap = wordMap;
            for (int i = 0; i < word.length(); i++) {
// 转换成char型
                char keyChar = word.charAt(i);
// 获取
                Object tempMap = nowMap.get(keyChar);
// 如果存在该key,直接赋值
                if (tempMap != null) {
                    nowMap = (Map) tempMap;
                }
// 不存在则,则构建一个map,同时将isEnd设置为0,因为他不是最后一个
                else {
// 设置标志位
                    Map<String, String> newMap = new HashMap<String, String>();
                    newMap.put("isEnd", "0");
// 添加到集合
                    nowMap.put(keyChar, newMap);
                    nowMap = newMap;
                }
// 最后一个
                if (i == word.length() - 1) {
                    nowMap.put("isEnd", "1");
                }
            }
        }
        return wordMap;
    }
 
 
}