国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java使用DFA算法實現過濾多家公司自定義敏感字功能詳解

Java使用DFA算法實現過濾多家公司自定義敏感字功能詳解

2020-12-22 15:32jack-0023 Java教程

這篇文章主要介紹了Java使用DFA算法實現過濾多家公司自定義敏感字功能,結合實例形式分析了DFA算法的實現原理及過濾敏感字的相關操作技巧,需要的朋友可以參考下

本文實例講述了java使用dfa算法實現過濾多家公司自定義敏感字功能。分享給大家供大家參考,具體如下:

背景

因為最近有通訊有個需求,說需要讓多家客戶公司可以自定義敏感詞過濾掉他們自定義的規則,選擇了dfa算法來做,不過和以前傳統了dfa寫法不太一樣了

模式圖

Java使用DFA算法實現過濾多家公司自定義敏感字功能詳解

直接上代碼

?
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
public class keywordfilter {
//  private static reentrantreadwritelock lock = new reentrantreadwritelock();
  public static map<string, hashmap> currentmap = new concurrenthashmap<string, hashmap>();
  public static map nowhash = null;
  public static object wordmap;// map子節點
  // 不建立對象
  private keywordfilter() {
  }
  private static string getkey(int companyid) {
    return "companyid" + companyid;
  }
  /*
   * <p>說明:清掃內容</p>
   *
   * @author:姚旭民
   *
   * @data:2017-8-22 上午10:13:11
   */
  public static void clear() {
    try {
      currentmap.clear();
    } catch (exception e) {
      e.printstacktrace();
    } finally {
    }
  }
  /*
   * <p>說明:各個渠道的過濾字符</p>
   *
   * @author:姚旭民
   *
   * @data:2017-8-20 下午2:55:06
   */
  public static void savekeywords(int companyid, list<string> keywords) {
    try {
      map tempallmap = currentmap;
      string key = getkey(companyid);
      int l = keywords.size();
      int il;
      map tempmap;
      for (int i = 0; i < l; i++) {
        string key2 = keywords.get(i).trim();// 去掉空白
        nowhash = currentmap;
        il = key2.length();
        for (int j = 0; j < il; j++) {
          char word = key2.charat(j);
          tempmap = (map) nowhash.get(word);
          wordmap = nowhash.get(word);
          if (wordmap != null) {// 檢查數據
            if (!tempmap.containskey(key)) {
              nowhash.put(key, 0);
            }
            nowhash = (hashmap) wordmap;
          } else {
            hashmap<string, string> newwordhash = new hashmap<string, string>();
            newwordhash.put(key, "0");
            nowhash.put(word, newwordhash);
            nowhash = newwordhash;
          }
          if (j == il - 1) {
            nowhash.put(key, "1");
          }
        }
      }
    } catch (exception e) {
      e.printstacktrace();
    } finally {
      nowhash = null;
      wordmap = null;
    }
  }
  /*
   * <p>說明:替換掉對應的渠道規定掉敏感字</p>
   *
   * @author:姚旭民
   *
   * @data:2017-8-20 上午11:41:47
   */
  public static list<string> repword(int companyid, string txt) {
    map tempmap = currentmap;
    list<string> result = new arraylist<string>();
    string key = getkey(companyid);
    nowhash = currentmap;
    int l = txt.length();
    char word;
    string keywordstr = "";
    string keystatu;
    stringbuilder keyword = new stringbuilder();// 敏感字
    for (int i = 0; i < l; i++) {
      word = txt.charat(i);
      wordmap = nowhash.get(word);
      if (wordmap != null) {// 找到類似敏感字的字體,開始查詢
        keyword.append(word);
        object te = nowhash = (hashmap) wordmap;
        // 遍歷到這一步,就符合完整的關鍵字模板
        if (nowhash.get(key) != null
            && nowhash.get(key).tostring().equals("1")) {// 確定是敏感字,開始替換
          if (i < l - 1 && nowhash.get(txt.charat(i + 1)) != null) {// 優先過濾長敏感詞,去掉就檳城了優先過濾段敏感詞
            continue;
          }
          txt = txt.replaceall(keyword.tostring(), "*");
          nowhash = currentmap;
          keywordstr += keyword.tostring() + ",";
          i = i - keyword.length() + 1;
          l = txt.length();// 重新獲取字符長度
          keyword.delete(0, keyword.length());// 清空數據
        }
      } else {// 這個字不是敏感字,直接排除
        nowhash = currentmap;
        keyword.delete(0, keyword.length());// 清空數據
        continue;
      }
    }
    // 清除內存指向
    nowhash = null;
    wordmap = null;
    result.add(txt);
    result.add(keywordstr.length() - 1 > 0 ? keywordstr.substring(0,
        keywordstr.length() - 1) : keywordstr);
    return result;
  }
  /*
   * <p>說明:檢查是否存在敏感字</p>
   *
   * @author:姚旭民
   *
   * @data:2017-8-20 下午3:00:06 專門設計成私有的,如果沒有理由,別改動他
   */
  private static int checkkeywords(string txt, int companyid, int begin) {
    int result = 0;
    string key = getkey(companyid);
    try {
      nowhash = currentmap;
      int l = txt.length();
      char word = 0;
      for (int i = begin; i < l; i++) {
        word = txt.charat(i);
        wordmap = nowhash.get(word);
        if (wordmap != null) {
          result++;
          nowhash = (hashmap) wordmap;
          if (((string) nowhash.get(key)).equals("1")) {
            nowhash = null;
            wordmap = null;
            return result;
          }
        } else {
          result = 0;
          break;
        }
      }
    } catch (exception e) {
      e.printstacktrace();
    } finally {
      nowhash = null;
      wordmap = null;
      return result;
    }
  }
  /*
   * <p>說明:返回檢查的文本中包含的敏感字</p>
   *
   * @author:姚旭民
   *
   * @data:2017-8-20 下午3:32:53
   */
  public static string gettxtkeywords(string txt, int companyid) {
    string result = null;
    stringbuilder temp = new stringbuilder();
    string key;
    int l = txt.length();
    for (int i = 0; i < l;) {
      int len = checkkeywords(txt, companyid, i);
      if (len > 0) {
        key = (txt.substring(i, i + len));// 挑選出來的關鍵字
        temp.append(key + ",");
        txt = txt.replaceall(key, "");// 挑選出來的關鍵字替換成空白,加快挑選速度
        l = txt.length();
      } else {
        i++;
      }
    }
    if (temp.length() > 0) {
      result = temp.substring(0, temp.length() - 1);
    }
    return result;
  }
  /*
   * <p>說明:判斷文中是否包含渠道規定的敏感字</p>
   *
   * @author:姚旭民
   *
   * @data:2017-8-20 下午3:33:19
   */
  public boolean iskeywords(string txt, int companyid) {
    for (int i = 0; i < txt.length(); i++) {
      int len = checkkeywords(txt, companyid, i);
      if (len > 0) {
        return true;
      }
    }
    return false;
  }
  public static void main(string[] arg) {
    list<string> keywords = new arraylist<string>();
    keywords.add("傻×");
    keywords.add("漢奸");
    keywords.add("草");
    keywords.add("草泥馬");
    keywordfilter.savekeywords(1, keywords);
    string txt = "是傻×漢奸傻a傻b傻c傻d漢奸傻×草泥馬";
    list<string> list = repword(1, txt);
    system.out.println("文中包含的敏感字為:" + list.get(1));
    system.out.println("原文:" + txt);
    system.out.println("敏感字過濾后:" + list.get(0));
  }
}

希望本文所述對大家java程序設計有所幫助。

原文鏈接:https://my.oschina.net/grkj/blog/1522696

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲专区 变态 另类 | 男人的天堂视频网站 | 在线精品一区 | 在线国产一区二区 | 欧美性猛交xxxx黑人猛交 | 男女啪啪免费网站 | 欧美影| 亚洲精品视频在线观看免费视频 | 日韩高清电影 | 欧美一级一区 | 人人鲁人人莫一区二区三区 | 成人三区 | 精品国产乱码久久久久久久软件 | 免费看a | 久久66 | 国产精品国产成人国产三级 | av集中淫 | 国产精品国产 | 国产精品久久久久久一区二区三区 | 国产精品成人一区二区 | 国产嫩草91 | 日本在线视频观看 | 亚洲激情综合在线 | 日本午夜精品 | 免费的av网站 | 亚洲视频一区在线播放 | www.国产| 91资源在线视频 | 午夜小电影 | 亚洲精品国产综合区久久久久久久 | 久久久精品国产 | 日韩欧美二区 | 色综合久久久久 | a欧美| 亚洲精选久久 | 欧美freesex交免费视频 | 爱爱综合网| 日本电影中文字幕 | 中文字幕亚洲视频 | 欧美一区永久视频免费观看 | 中文成人在线 |