java keyword filtering algorithm

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

* Java keyword filtering
* HashMap achieve DFA algorithm
* Keywords replaced *
* @author Methocel
* /

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

({ "Rawtypes" , "an unchecked" })
public class { / ** directly prohibited * / Private the HashMap keysMap = new new the HashMap (); static Final String tihuantxt = "************* ********************************************* " ; Private int matchType = 1 ; // 1: 2 to match the minimum length: The maximum length matching





public void addKeywords(List<String> keywords) {
for (int i = 0; i < keywords.size(); i++) {
String key = keywords.get(i).trim();
HashMap nowhash = null;
nowhash = keysMap;
for (int j = 0; j < key.length(); j++) {
char word = key.charAt(j);
Object wordMap = nowhash.get(word);
if (wordMap != null) {
nowhash = (HashMap) wordMap;
} else {
HashMap<String, String> newWordHash = new HashMap<String, String>();
newWordHash.put("isEnd", "0");
nowhash.put(word, newWordHash);
nowhash = newWordHash;
}
if (j == key.length() - 1) {
nowhash.put("isEnd", "1");
}
}
}
}


* Keyword reset
* / public void clearKeywords () { keysMap.clear (); }





* Check the string starts from a position of whether a keyword compliance begin, if there is a value matching keyword, keyword matching the length of the return value, otherwise, returns zero
* flag 1: 2 to match the minimum length: The maximum length matching
* / Private int checkKeyWords (String TXT, int the begin, int In Flag) { the HashMap nowhash = null ; nowhash = keysMap; int maxMatchRes = 0 ; int RES = 0 ; int L = txt.length (); char Word = 0 ; for ( int I = the begin ; I <L; I ++) { Word = txt.charAt (I); Object wordMap = nowhash.get (Word);










if (wordMap != null) {
res++;
nowhash = (HashMap) wordMap;
if (((String) nowhash.get("isEnd")).equals("1")) {
if (flag == 1) {
wordMap = null;
nowhash = null;
txt = null;
return res;
} else {
maxMatchRes = res;
}
}
} else {
txt = null;
nowhash = null;
return maxMatchRes;
}
}
txt = null;
nowhash = null;
return maxMatchRes;
}


* 返回txt中关键字的列表
*/
public String getTxtKeyWords(String txt) {
Set set = new HashSet();
int l = txt.length();
String newtxt = new String();
for (int i = 0; i < l;) {
int len = checkKeyWords(txt, i, matchType);
if (len > 0) {
newtxt += txt.substring(0, i);
newtxt += tihuantxt.substring(0, len);
newtxt += txt.substring(i + len, txt.length());
txt = newtxt;
newtxt = "";
i += len;
} else {
i++;
}
}
return txt;
}


Txt * is determined whether there is only a keyword
* / public Boolean isContentKeyWords (String txt) { for ( int I = 0 ; I <txt.length (); I ++) { int len = checkKeyWords (txt, I, . 1 ); IF (len> 0 ) { return to true ; } } TXT = null ; return to false ; }











public int getMatchType() {
return matchType;
}

public void setMatchType(int matchType) {
this.matchType = matchType;
}
}

Original: Big Box  java keyword filtering algorithm


Guess you like

Origin www.cnblogs.com/chinatrump/p/11596627.html