-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.java
More file actions
234 lines (211 loc) · 6.58 KB
/
Copy pathrun.java
File metadata and controls
234 lines (211 loc) · 6.58 KB
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
import static java.lang.System.*;
import java.util.List;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.seg.*;
import com.hankcs.hanlp.seg.NShort.*;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.evaluation.NominalPrediction;
import weka.classifiers.rules.DecisionTable;
import weka.classifiers.rules.PART;
import weka.classifiers.trees.DecisionStump;
import weka.classifiers.trees.J48;
import weka.core.FastVector;
import weka.core.Instances;
public class run {
public static int wordsLength = 54;
public static String[] words = {"之", "其", "或", "亦", "方", "于", "即", "皆", "因", "仍", "故", "尚", "呢"
, "了", "的", "着", "一", "不", "乃", "呀", "吗", "咧", "啊", "把", "让", "向"
, "往", "是", "在", "越", "更", "比", "很", "偏", "别", "好", "可", "便", "就"
, "但", "儿", "又", "也", "都", "要", "这", "那", "你", "我", "他", "来", "去"
, "道", "说"};
public static void CalNumof(String fileAddr, PrintWriter pw, int tag) {
BufferedReader datafile = null;
try {
datafile = new BufferedReader(new FileReader(fileAddr));
} catch (FileNotFoundException ex) {
err.println("File not found: " + fileAddr);
}
int []wordsNumber;
wordsNumber = new int[54];
try {
List<Term> termList = null;
String str = null;
do {
str = datafile.readLine();
termList = HanLP.segment(str);
boolean printFlag = false;
for (int i = 0; i < termList.size(); ++i)
for (int j = 0; j < wordsLength; ++j)
if (termList.get(i).word.equals(words[j]))
wordsNumber[j]++;
} while(str != null);
} catch (Exception ex) {}
for (int i = 0; i < wordsLength; ++i)
pw.write(wordsNumber[i] + ", ");
pw.write(tag + "\r\n");
}
public static Evaluation classify(Classifier model, Instances trainSet, Instances testSet) throws Exception {
Evaluation evaluation = new Evaluation(trainSet);
model.buildClassifier(trainSet);
evaluation.evaluateModel(model, testSet);
return evaluation;
}
public static double calculateAccuracy(FastVector predictions) {
double correct = 0;
for (int i = 0; i < predictions.size(); ++i) {
NominalPrediction np = (NominalPrediction) predictions.elementAt(i);
if (np.predicted() == np.actual())
correct++;
}
return 100 * correct / predictions.size();
}
public static void main(String[] args) {
File file = new File("trainArff.txt");
PrintWriter pw = null;
// 创建文件对应FileOutputStream
try {
pw = new PrintWriter(new FileOutputStream(file));
} catch(IOException ex) {
ex.printStackTrace();
}
pw.write("@relation i_wanna_eat_chicken\r\n");
pw.write("\r\n");
for (int i = 0; i < wordsLength; ++i) {
pw.write("@attribute " + words[i] + " numeric\r\n");
}
pw.write("@attribute tag {0, 1, 2}\r\n");
pw.write("\r\n");
pw.write("@data\r\n");
String fileAddr = args[0];
BufferedReader datafile = null;
try {
datafile = new BufferedReader(new FileReader(fileAddr));
} catch(FileNotFoundException ex) {
err.println("File not found: " + fileAddr);
}
String str = null;
try {
while(true) {
str = datafile.readLine();
if(str == null)
break;
int tag = Integer.valueOf(str.substring(0, 1)).intValue();
//tag = tag == 1 ? 0 : tag;
CalNumof(str.substring(2), pw, tag);
}
} catch (Exception ex){};
// 关闭文件
pw.close();
file = new File("testArff.txt");
pw = null;
// 创建文件对应FileOutputStream
try {
pw = new PrintWriter(new FileOutputStream(file));
} catch(IOException ex) {
ex.printStackTrace();
}
pw.write("@relation i_wanna_eat_chicken\r\n");
pw.write("\r\n");
for (int i = 0; i < wordsLength; ++i) {
pw.write("@attribute " + words[i] + " numeric\r\n");
}
pw.write("@attribute tag {0, 1, 2}\r\n");
pw.write("\r\n");
pw.write("@data\r\n");
fileAddr = args[1];
datafile = null;
try {
datafile = new BufferedReader(new FileReader(fileAddr));
} catch(FileNotFoundException ex) {
err.println("File not found: " + fileAddr);
}
str = null;
try {
while(true) {
str = datafile.readLine();
if(str == null)
break;
int tag = Integer.valueOf(str.substring(0, 1)).intValue();
CalNumof(str.substring(2), pw, tag);
}
} catch (Exception ex){};
// 关闭文件
pw.close();
datafile = null;
String filename = "trainArff.txt";
FileReader fr = null;
try {
fr = new FileReader(filename);
datafile = new BufferedReader(fr);
} catch(FileNotFoundException ex) {
err.println("File not found: " + filename);
}
Instances trainSet = null;
try {
trainSet = new Instances(datafile);
datafile.close();
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
trainSet.setClassIndex(trainSet.numAttributes() - 1);
filename = "testArff.txt";
try {
fr = new FileReader(filename);
datafile = new BufferedReader(fr);
} catch(FileNotFoundException ex) {
err.println("File not found: " + filename);
}
Instances testSet = null;
try {
testSet = new Instances(datafile);
datafile.close();
fr.close();
} catch(IOException ex) {
ex.printStackTrace();
}
testSet.setClassIndex(testSet.numAttributes() - 1);
Classifier models = new J48();
FastVector predictions = new FastVector();
try {
Evaluation validation = classify(models, trainSet, testSet);
predictions.appendElements(validation.predictions());
double accuracy = calculateAccuracy(predictions);
out.println("Accuracy of" + models.getClass().getSimpleName() + ":"
+ String.format("%.2f%%", accuracy)
+ "\n-------------------=======-------");
} catch (Exception ex) {}
// 删除中间文件
file = new File("trainArff.txt");
try {
file.delete();
} catch (Exception ex) {
ex.printStackTrace();
}
file = new File("testArff.txt");
try {
file.delete();
} catch (Exception ex) {
ex.printStackTrace();
}
// 输出结果文件
file = new File("HW3_1600012786.txt");
pw = null;
try {
pw = new PrintWriter(new FileOutputStream(file));
} catch(IOException ex) {
ex.printStackTrace();
}
for (int i = 0; i < predictions.size(); ++i) {
NominalPrediction np = (NominalPrediction) predictions.elementAt(i);
pw.write((int)np.predicted() + "\r\n");
}
pw.close();
}
}