-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNaiveBayesTest.java
362 lines (321 loc) · 10.1 KB
/
NaiveBayesTest.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// NaiveBaseTest.java
// 2011.07.26 Hiroki Tanioka
import java.io.*;
import java.util.*;
/**
* Test Class for NaiveBayes Classifier
*/
public class NaiveBayesTest {
static private int DEF_TERM_NUM = 3000;
static private int DEF_LABEL_NUM = 2;
static public boolean logging = false;
static NaiveBayes bayse = new NaiveBayes();
static Map<String, Integer> cateMap = new HashMap<String, Integer>();
static List<String> cateList = new ArrayList<String>();
static Map<String, Integer> termMap = new HashMap<String, Integer>();
static List<String> termList = new ArrayList<String>();
public static void main(String args[]) {
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader r = new BufferedReader(isr);
while (true) {
System.out.println("");
System.out.print("> ");
String s = r.readLine();
if (s == null || "".equals(s) || ".".equals(s)) {
break;
}
String[] commands = {"--help", "/?", "train ", "choice ", "output", };
int index = 0;
String line = "";
for (index = 0; index < commands.length; index++) {
String command = commands[index];
int pos = s.indexOf(command);
if (pos >= 0) {
line = s.substring(pos + command.length());
break;
}
}
switch (index) {
case 2:
{
//System.out.println(line);
if (train(line) == false) {
break;
}
}
break;
case 3:
{
//System.out.println(line);
if (choice(line) == false) {
break;
}
}
break;
case 4:
{
if (output(line) == false) {
break;
}
}
break;
case 0:
case 1:
default:
{
System.out.print(
"Usage: java NaiveBayesTest\n" +
" (to execute this Test tool) + \n" +
"\n" +
"where online commands include:\n" +
" train is to train keywords for a category\n" +
" <the head term can be interpreted as category.>\n" +
" <the following terms can be interpreted as keywords.>\n" +
" e.g.) > train enjoy leisure summer_vacation new_year_vacation\n" +
" trained!\n" +
"\n" +
" choice is to choose a category for the given keywords\n" +
" <all terms can be interpreted as keywords.>\n" +
" e.g.) > choice leisure summer_vacation new_year_vacation\n" +
" category = enjoy (0)\n" +
"\n" +
" output is to show keyword probabilities for each category.\n" +
" <the following terms can be interpreted as categories.>\n" +
" <If following term is none, all categories are shown.>\n" +
" e.g.) > output enjoy\n" +
" category, enjoy,\n" +
" total, 3,\n" +
" leisure, 73.1(1.0),\n" +
" new_year_vacation, 73.1(1.0),\n" +
" summer_vacation, 73.1(1.0),\n" +
"");
}
break;
}
}
r.close();
return;
}
catch (IOException e) {
e.printStackTrace();
return;
}
}
private static boolean output(String line) {
if (cateMap.size() <= 0 || cateList.size() <= 0 ||
termMap.size() <= 0 || termList.size() <= 0) {
System.out.println(cateMap.size() + ", " +
cateList.size() + ", " +
termMap.size() + ", " +
termList.size());
return false;
}
Map<String, Integer> termFreq = new HashMap<String, Integer>();
String[] cates = line.split(" ");
if (cates == null) {
System.out.println("Failed to split.");
return false;
}
Set<String> category_set = new TreeSet<String>();
for (int i = 0; i < cates.length; i++) {
String term = cates[i].trim();
if (term == null || "".equals(term)) {
continue;
}
category_set.add(term);
}
//System.out.println("category_set = " + category_set.size());
List<Integer> categories = new ArrayList<Integer>();
List<float[]> probs = new ArrayList<float[]>();
List<float[]> terms = new ArrayList<float[]>();
bayse.output(categories, probs, terms);
// categories
System.out.print("category, ");
for (int i = 0; i < categories.size(); i++) {
String category = cateList.get(i);
if (category_set.size() > 0 && !category_set.contains(category)) {
continue;
}
System.out.print(category + ", ");
}
System.out.println("");
System.out.print("total, ");
for (int i = 0; i < categories.size(); i++) {
String category = cateList.get(i);
if (category_set.size() > 0 && !category_set.contains(category)) {
continue;
}
int cate_freq = categories.get(i).intValue();
System.out.print(cate_freq + ", ");
}
System.out.println("");
// term's probabilities
for (int i = 0; i < terms.size(); i++) {
String term = termList.get(i);
System.out.print(term + ", ");
float[] term_probs = probs.get(i);
float[] term_freqs = terms.get(i);
for (int j = 0; j < term_probs.length; j++) {
String category = cateList.get(j);
if (category_set.size() > 0 &&
!category_set.contains(category)) {
continue;
}
System.out.print(term_probs[j] +
"(" + term_freqs[j] + ")" + ", ");
}
System.out.println("");
}
return true;
}
private static boolean choice(String line) {
if (cateMap.size() <= 0 || cateList.size() <= 0 ||
termMap.size() <= 0 || termList.size() <= 0) {
return false;
}
Map<String, Integer> termFreq = new HashMap<String, Integer>();
String[] terms = line.split(" ");
if (terms.length <= 0) {
System.err.println("Failed to split.");
return false;
}
for (int i = 0; i < terms.length; i++) {
String term = terms[i].trim();
if (term == null || "".equals(term)) {
continue;
}
Integer _freq = termFreq.get(term);
if (_freq == null) {
_freq = new Integer(0);
termFreq.put(term, _freq);
}
int freq = _freq.intValue();
termFreq.put(term, new Integer(freq + 1));
}
if (logging) {
Set<String> termSet = termFreq.keySet();
for (Iterator<String> i = termSet.iterator(); i.hasNext();) {
String key = i.next();
Integer freq = termFreq.get(key);
System.out.println(key + ": " + freq);
}
}
int[] term_id = new int[termFreq.size()];
float[] term_fq = new float[termFreq.size()];
int index = 0;
Set<String> termSet = termFreq.keySet();
for (Iterator<String> i = termSet.iterator(); i.hasNext();) {
String key = i.next();
Integer freq = termFreq.get(key);
Integer _id = termMap.get(key);
if (_id == null) {
// int id = termList.size();
_id = new Integer(-1);
//System.out.println(key + " " + _id.toString());
// termMap.put(key, _id);
// termList.add(key);
}
//System.out.println(key + " " + termMap.get(key));
term_id[index] = _id.intValue();
term_fq[index] = freq.intValue();
index++;
}
int result_id = bayse.choice(term_id, term_fq);
String category = cateList.get(result_id);
System.out.println("category = " + category + " (" + result_id + ")");
return true;
}
private static boolean train(String line) {
final int repeat = 10;
return train(line, repeat);
}
private static boolean train(String line, int repeat) {
if (cateMap.size() <= 0 || cateList.size() <= 0 ||
termMap.size() <= 0 || termList.size() <= 0) {
repeat = 0;
}
Map<String, Integer> termFreq = new HashMap<String, Integer>();
String[] terms = line.split(" ");
if (terms.length <= 0) {
System.out.println("Failed to split.");
return false;
}
String category = terms[0].trim();
Integer _cate_id = cateMap.get(category);
if (_cate_id == null) {
int cate_id = cateList.size();
_cate_id = new Integer(cate_id);
cateMap.put(category, cate_id);
cateList.add(category);
}
for (int i = 1; i < terms.length; i++) {
String term = terms[i].trim();
if (term == null || "".equals(term)) {
continue;
}
Integer _freq = termFreq.get(term);
if (_freq == null) {
_freq = new Integer(0);
termFreq.put(term, _freq);
}
int freq = _freq.intValue();
termFreq.put(term, new Integer(freq + 1));
}
if (logging) {
System.out.println("category = " + _cate_id.intValue());
Set<String> termSet = termFreq.keySet();
for (Iterator<String> i = termSet.iterator(); i.hasNext();) {
String key = i.next();
Integer freq = termFreq.get(key);
System.out.println(key + ": " + freq);
}
}
int[] term_id = new int[termFreq.size()];
float[] term_fq = new float[termFreq.size()];
int index = 0;
Set<String> termSet = termFreq.keySet();
for (Iterator<String> i = termSet.iterator(); i.hasNext();) {
String key = i.next();
Integer freq = termFreq.get(key);
Integer _id = termMap.get(key);
if (_id == null) {
int id = termList.size();
_id = new Integer(id);
//System.out.println(key + " " + _id.toString());
termMap.put(key, _id);
termList.add(key);
}
//System.out.println(key + " " + termMap.get(key));
term_id[index] = _id.intValue();
term_fq[index] = freq.intValue();
index++;
}
//System.out.println("cate_id = " + _cate_id);
boolean ret = bayse.train(_cate_id.intValue(), term_id, term_fq);
if (ret == false) {
System.out.println("Failed to train.");
}
if (repeat > 0) {
// conditional train
for (int i = 1; i < repeat; i++) {
int result_id = bayse.choice(term_id, term_fq);
if (result_id == _cate_id.intValue()) {
break;
}
ret = bayse.train(_cate_id.intValue(), term_id, term_fq);
if (ret == false) {
System.out.println("Failed to train.");
}
}
}
int result_id = bayse.choice(term_id, term_fq);
if (result_id == _cate_id.intValue()) {
System.out.println("trained!");
}
else {
System.out.println("insufficiency...");
}
return true;
}
}