贝叶斯分类器的Java实现

贝叶斯分类器的Java实现


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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package com.ycj.publicOpinionAnalysisSystem.nlp.utility;

import com.hankcs.hanlp.classification.tokenizers.*;
import javax.xml.crypto.dom.DOMCryptoContext;
import java.io.Serializable;
import java.util.*;

/**
* @Author: yanchengjie
*/
public class BayesClassifier implements Serializable {

private static final long serialVersionUID = 8338342007460341994L;

// 语料库总词数
private Long corpusAllWordsNum = new Long(0);

// 语料库总文档数
private Long corpusAllDocNum = new Long(0);

// 语料库各分类文档数
private Map<String, Long> corpusClassDocNumMap = new HashMap<>();

// 语料库各分类词数
private Map<String, Long> corpusClassWordsNumMap = new HashMap<>();

// 语料库各分类先验概率
private Map<String, Double> corpusClassPriorProbabilityMap = new TreeMap<>();

// 各分类词频树
private Map<String, TreeMap<String, Integer>> corpusClassWordFrequency = new HashMap<>();

// 语料库包含特征词的文档总数, 一篇文档包含多次计作一次
private TreeMap<String, Integer> wordExistInAllDocNumTree;

// 特征映射
private Map<String, TreeMap<String, Double>> corpusClassWordFeature = new HashMap<>();

// IDF映射
private TreeMap<String, Double> corpusClassWordIDF = new TreeMap<>();


// 分词器
private ITokenizer tokenizer = new BigramTokenizer();

// 中文停用词表
private TreeSet<String> stopWordsTree;

// 平滑系数
private static Double ALPHA = 1.0;

// 语料库各分类文档集合,String[]表示分词后一篇文档(语料加载元数据)
private Map<String, Vector<String[]>> corpusClassDocSet;


public BayesClassifier(Map<String, Vector<String[]>> corpusClassDocSet, TreeSet<String> stopWordsTree) {
this.stopWordsTree = stopWordsTree;
this.corpusClassDocSet = corpusClassDocSet;
// 构建 语料库包含特征词的文档数Tree
this.wordExistInAllDocNumTree = createWordExistInAllDocNumTree(corpusClassDocSet);
if (corpusClassDocSet != null) {
for (Map.Entry<String, Vector<String[]>> entry : corpusClassDocSet.entrySet()) {
// 构建 各分类下文档数
this.corpusClassDocNumMap.put(entry.getKey(), Integer.valueOf(entry.getValue().size()).longValue());
// 构建 总文档数
this.corpusAllDocNum += Integer.valueOf(entry.getValue().size()).longValue();
// 构建 各分类的词频树
TreeMap<String, Integer> wordFrequencyTree = createWordFrequencyTree(entry.getValue());
this.corpusClassWordFrequency.put(entry.getKey(), wordFrequencyTree);
}
if (corpusClassWordFrequency != null) {
// 构建 语料库各分类词数
for (Map.Entry<String, TreeMap<String, Integer>> entry : corpusClassWordFrequency.entrySet()) {
String className = entry.getKey();
TreeMap<String, Integer> corpusTree = entry.getValue();
Long corpusNum = new Long(0);
Iterator<Map.Entry<String, Integer>> iterator = corpusTree.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> wordNum = iterator.next();
// 去停用词
if (stopWordsTree.contains(wordNum.getKey())) {
iterator.remove();
continue;
}
corpusNum += wordNum.getValue().longValue();
}
corpusClassWordsNumMap.put(className, corpusNum);
}
}
// 构建 语料库总词数
for (Map.Entry<String, Long> entry : corpusClassWordsNumMap.entrySet()) {
corpusAllWordsNum += entry.getValue();
}
// 构建 先验概率
/*for (Map.Entry<String, Long> entry : corpusClassDocNumMap.entrySet()) {
corpusClassPriorProbabilityMap.put(entry.getKey(), entry.getValue().doubleValue() / corpusAllDocNum);
}*/
// 构建 先验概率
for (Map.Entry<String, Long> entry : corpusClassWordsNumMap.entrySet()) {
corpusClassPriorProbabilityMap.put(entry.getKey(), Math.log(entry.getValue().doubleValue() / corpusAllWordsNum));
}
// 构建 特征映射 条件概率
for (Map.Entry<String, TreeMap<String, Integer>> entry : corpusClassWordFrequency.entrySet()) {
String className = entry.getKey();
TreeMap<String, Integer> corpusTree = entry.getValue();
TreeMap<String, Double> featureTree = new TreeMap<>();
for (Map.Entry<String, Integer> wordNum : corpusTree.entrySet()) {
Double featureValue = (wordNum.getValue().doubleValue() + ALPHA) / (Integer.valueOf(corpusTree.size()).doubleValue()
+ corpusClassWordFrequency.size() * ALPHA);
/*Double featureValue = (wordNum.getValue().doubleValue() + ALPHA) / (corpusClassWordsNumMap.get(className).doubleValue()
+ corpusClassWordsNumMap.size() * ALPHA);*/
// Double featureValue = (wordNum.getValue().doubleValue() + ALPHA) / (corpusClassDocNumMap.get(className).doubleValue() + corpusClassDocNumMap.size() * ALPHA);
// 取对数
featureValue = Math.log(featureValue);
featureTree.put(wordNum.getKey(), featureValue);
}
corpusClassWordFeature.put(className, featureTree);
}
// 构建 IDF映射
for (Map.Entry<String, Integer> entry : wordExistInAllDocNumTree.entrySet()) {
Double idf = Math.log((ALPHA + corpusAllDocNum.doubleValue()) / (ALPHA + wordExistInAllDocNumTree.get(entry.getKey()).doubleValue()));
idf = Math.log(idf);
corpusClassWordIDF.put(entry.getKey(), idf);
}
}
}

/*public BayesClassifier(Map<String, TreeMap<String, Integer>> corpusClassWordFrequency, Map<String, Long> corpusEachClassDocNum, TreeSet<String> stopWordsTree) {
this.corpusClassWordFrequency = corpusClassWordFrequency;
this.corpusEachClassDocNum = corpusEachClassDocNum;
this.stopWordsTree = stopWordsTree;
if (corpusEachClassDocNum != null) {
corpusEachClassDocNum.entrySet();
for (Map.Entry<String, Long> entry : corpusEachClassDocNum.entrySet()) {
corpusAllDocNum += entry.getValue();
}
}
if (corpusClassWordFrequency != null) {
// 计算语料库各分类总词数
for (Map.Entry<String, TreeMap<String, Integer>> entry : corpusClassWordFrequency.entrySet()) {
String className = entry.getKey();
TreeMap<String, Integer> corpusTree = entry.getValue();
Long corpusNum = new Long(0);

Iterator<Map.Entry<String, Integer>> iterator=corpusTree.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> wordNum= iterator.next();
// 去停用词
if (stopWordsTree.contains(wordNum.getKey())){
iterator.remove();
continue;
}
corpusNum += wordNum.getValue().longValue();
// 合并词数 构建全词频树
if (!corpusWordFrequency.containsKey(wordNum.getKey())){
corpusWordFrequency.put(wordNum.getKey(),wordNum.getValue());
}else {
corpusWordFrequency.replace(wordNum.getKey(),wordNum.getValue()+corpusWordFrequency.get(wordNum.getKey()));
}
}
corpusEachClassWordsNumMap.put(className, corpusNum);
}
// 计算语料库总词数
for (Map.Entry<String, Long> entry : corpusEachClassWordsNumMap.entrySet()) {
corpusAllWordsNum += entry.getValue();
}
// 计算先验概率
for (Map.Entry<String, Long> entry : corpusEachClassWordsNumMap.entrySet()) {
corpusClassPriorProbabilityMap.put(entry.getKey(), entry.getValue().doubleValue() / corpusAllWordsNum);
}
// 计算TF-IDF
for (Map.Entry<String, TreeMap<String, Integer>> entry : corpusClassWordFrequency.entrySet()) {
String className = entry.getKey();
TreeMap<String, Integer> corpusTree = entry.getValue();
TreeMap<String, Double> tfIDFTree = new TreeMap<>();
for (Map.Entry<String, Integer> wordNum : corpusTree.entrySet()) {
Double alpha=1.0;
Double tf = (wordNum.getValue().doubleValue()+alpha) / (corpusEachClassWordsNumMap.get(className).doubleValue()+corpusEachClassDocNum.size()*alpha);
//Double idf = Math.log((1 + corpusAllDocNum) / corpusWordFrequency.get(wordNum.getKey()).doubleValue());
//Double tf_idf = tf * idf;
//Double tf_idf = tf;
tfIDFTree.put(wordNum.getKey(), tf);
}
corpusClassWordTFIDF.put(className, tfIDFTree);
}
}
}*/

private static TreeMap<String, Integer> createWordFrequencyTree(Vector<String[]> docSet) {
if (docSet == null) throw new NullPointerException("docSet为空");
TreeMap<String, Integer> wordFrequencyTree = new TreeMap<>();
Iterator<String[]> iterator = docSet.iterator();
while (iterator.hasNext()) {
String[] strings = iterator.next();
for (int i = 0; i < strings.length; i++) {
if (wordFrequencyTree.containsKey(strings[i])) {
wordFrequencyTree.replace(strings[i], wordFrequencyTree.get(strings[i]) + 1);
} else {
wordFrequencyTree.put(strings[i], 1);
}
}
}
return wordFrequencyTree;
}

private static TreeMap<String, Integer> createWordExistInAllDocNumTree(Map<String, Vector<String[]>> corpusClassDocSet) {
if (corpusClassDocSet == null) throw new NullPointerException("corpusClassDocSet为空");
TreeMap<String, Integer> wordExistInAllDocNumTree = new TreeMap<>();
for (Map.Entry<String, Vector<String[]>> entry : corpusClassDocSet.entrySet()) {
Vector<String[]> docSet = entry.getValue();
Iterator<String[]> iterator = docSet.iterator();
while (iterator.hasNext()) {
String[] strings = iterator.next();
List<String> stringDifferent = new ArrayList<>(strings.length);
for (int i = 0; i < strings.length; i++) {
if (!stringDifferent.contains(strings[i])) stringDifferent.add(strings[i]);
}
for (int i = 0; i < stringDifferent.size(); i++) {
if (wordExistInAllDocNumTree.containsKey(stringDifferent.get(i))) {
wordExistInAllDocNumTree.replace(stringDifferent.get(i), wordExistInAllDocNumTree.get(strings[i]) + 1);
} else {
wordExistInAllDocNumTree.put(stringDifferent.get(i), 1);
}
}
}
}
return wordExistInAllDocNumTree;
}


public Double featureExtraction(String classification, String word) {
Double featureValue = corpusClassWordFeature.get(classification).get(word);
if (featureValue == null) {
featureValue = ALPHA / (corpusClassWordsNumMap.get(classification).doubleValue() + corpusClassWordsNumMap.size() * ALPHA);
featureValue = Math.log(featureValue);
corpusClassWordFeature.get(classification).put(word, featureValue);
}
return featureValue;
}

public Double idfExtraction(String word) {
Double idfValue = corpusClassWordIDF.get(word);
if (idfValue == null) {
idfValue = Math.log((ALPHA + corpusAllDocNum.doubleValue()) / ALPHA);
idfValue = Math.log(idfValue);
corpusClassWordIDF.put(word, idfValue);
}
return idfValue;
}

public Map<Double, String> calculateProbability(String[] strings) {
Map<String, Integer> tempForTF = new HashMap<>();
for (int i = 0; i < strings.length; i++) {
if (tempForTF.containsKey(strings[i])) {
tempForTF.replace(strings[i], tempForTF.get(strings[i]) + 1);
} else {
tempForTF.put(strings[i], 1);
}
}
Map<Double, String> probabilityMap = new HashMap<>();
for (Map.Entry<String, Double> entry : corpusClassPriorProbabilityMap.entrySet()) {
Double probability = corpusClassPriorProbabilityMap.get(entry.getKey());
Double feature;
Double idf;
for (int i = 0; i < strings.length; i++) {
try {
if (stopWordsTree.contains(strings[i])) continue;
feature = featureExtraction(entry.getKey(), strings[i]);
idf = idfExtraction(strings[i]);
probability = probability
+ feature + idf
+ Math.log(tempForTF.get(strings[i]).doubleValue() / Integer.valueOf(strings.length).doubleValue());
//if (probability == 0.0 || probability < Double.MIN_VALUE) probability = Double.MIN_VALUE;
} catch (NullPointerException e) {
e.printStackTrace();
continue;
}
}
probabilityMap.put(probability, entry.getKey());
}
return probabilityMap;
}

public String classify(String text) {
String[] strings = tokenizer.segment(text);
Map<Double, String> probabilityMap = calculateProbability(strings);
Double maxProbability = -Double.MAX_VALUE;
for (Map.Entry<Double, String> entry : probabilityMap.entrySet()) {
Double probability = entry.getKey() + Double.MIN_VALUE;
System.out.println("分类:"+entry.getValue()+" 概率:"+probability);
if (probability > maxProbability) {
maxProbability = probability;
}
}
//System.out.print("概率:" + (maxProbability - Double.MIN_VALUE) + ",");
String result = probabilityMap.get(maxProbability - Double.MIN_VALUE);
if (result == null) {
result = "";
}
return result;
}

public String classify(String[] strings) {
Map<Double, String> probabilityMap = calculateProbability(strings);
Double maxProbability = -Double.MAX_VALUE;
for (Map.Entry<Double, String> entry : probabilityMap.entrySet()) {
Double probability = entry.getKey() + Double.MIN_VALUE;
System.out.println("分类: 【"+entry.getValue()+"】 概率:"+probability);
if (probability > maxProbability) {
maxProbability = probability;
}
}
//System.out.println("概率:" + (maxProbability - Double.MIN_VALUE) + ",");
String result = probabilityMap.get(maxProbability - Double.MIN_VALUE);
if (result == null) {
result = "";
}
return result;
}

}