-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatbot.java
More file actions
executable file
·294 lines (278 loc) · 7.92 KB
/
Copy pathChatbot.java
File metadata and controls
executable file
·294 lines (278 loc) · 7.92 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
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
import java.util.*;
import java.io.*;
public class Chatbot {
private static String filename = "WARC201709_wid.txt";
private static Scanner sc;
private static ArrayList<Integer> corpus = new ArrayList<Integer>();
private static ArrayList<Integer> readCorpus() {
try {
File f = new File(filename);
sc = new Scanner(f);
while (sc.hasNext()) {
if (sc.hasNextInt()) {
int i = sc.nextInt();
corpus.add(i);
} else {
sc.next();
}
}
} catch (FileNotFoundException ex) {
System.out.println("File Not Found.");
}
return corpus;
}
static public void main(String[] args) {
ArrayList<Integer> corpus = readCorpus();
int flag = Integer.valueOf(args[0]);
if (flag == 100) {
int w = Integer.valueOf(args[1]);
double[] p = returnUni();
int count = (int) (p[w] * corpus.size());
System.out.println(count);
System.out.println(String.format("%.7f", p[w]));
} else if (flag == 200) {
int n1 = Integer.valueOf(args[1]);
int n2 = Integer.valueOf(args[2]);
double prob = (n1 * 1.0) / (n2 * 1.0);
double[] ans = checkIntervals(1, prob, 0, 0, 0);
System.out.println((int) ans[0]);
System.out.println(String.format("%.7f", ans[1]));
System.out.println(String.format("%.7f", ans[2]));
} else if (flag == 300) {
int h = Integer.valueOf(args[1]);
int w = Integer.valueOf(args[2]);
List<Double> list = returnBi(h);
double size = list.get(list.size() - 1);
list.remove(list.size() - 1);
System.out.println((int) (size * list.get(w)));
System.out.println((int) size);
System.out.println(String.format("%.7f", list.get(w)));
} else if (flag == 400) {
int n1 = Integer.valueOf(args[1]);
int n2 = Integer.valueOf(args[2]);
int h = Integer.valueOf(args[3]);
double prob = (n1 * 1.0) / (n2 * 1.0);
double[] ans = checkIntervals(2, prob, h, 0, 0);
System.out.println((int) ans[0]);
System.out.println(String.format("%.7f", ans[1]));
System.out.println(String.format("%.7f", ans[2]));
} else if (flag == 500) {
int h1 = Integer.valueOf(args[1]);
int h2 = Integer.valueOf(args[2]);
int w = Integer.valueOf(args[3]);
List<Double> list = returnTri(h1, h2);
double size = list.get(list.size() - 1);
list.remove(list.size() - 1);
System.out.println((int) (size * list.get(w)));
System.out.println((int) size);
if (size == 0.0)
System.out.println("undefined");
else
System.out.println(String.format("%.7f", list.get(w)));
} else if (flag == 600) {
int n1 = Integer.valueOf(args[1]);
int n2 = Integer.valueOf(args[2]);
int h1 = Integer.valueOf(args[3]);
int h2 = Integer.valueOf(args[4]);
double prob = (n1 * 1.0) / (n2 * 1.0);
double[] ans = checkIntervals(3, prob, 0, h1, h2);
if (ans[2] == 0.0)
System.out.println("undefined");
else {
System.out.println((int) ans[0]);
System.out.println(String.format("%.7f", ans[1]));
System.out.println(String.format("%.7f", ans[2]));
}
}
else if (flag == 700) {
int seed = Integer.valueOf(args[1]);
int t = Integer.valueOf(args[2]);
int h1 = 0, h2 = 0;
Random rng = new Random();
if (seed != -1)
rng.setSeed(seed);
if (t == 0) {
double r = rng.nextDouble();
double[] ans = checkIntervals(1, r, 0, 0, 0);
h1 = (int) ans[0];
System.out.println(h1);
if (h1 == 9 || h1 == 10 || h1 == 12) {
return;
}
r = rng.nextDouble();
ans = checkIntervals(2, r, h1, 0, 0);
h2 = (int) ans[0];
System.out.println(h2);
} else if (t == 1) {
h1 = Integer.valueOf(args[3]);
// TODO Generate second word using r
double r = rng.nextDouble();
double[] ans = checkIntervals(2, r, h1, 0, 0);
h2 = (int) ans[0];
System.out.println(h2);
} else if (t == 2) {
h1 = Integer.valueOf(args[3]);
h2 = Integer.valueOf(args[4]);
}
while (h2 != 9 && h2 != 10 && h2 != 12) {
double r = rng.nextDouble();
int w = 0;
double[] ans = checkIntervals(3, r, 0, h1, h2);
w = (int) ans[0];
System.out.println(w);
h1 = h2;
h2 = w;
}
}
}
private static double[] returnUni() {
double[] p = new double[4700];
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < corpus.size(); j++) {
if (corpus.get(j) == i)
p[i]++;
}
p[i] = p[i] / (double) corpus.size();
}
return p;
}
private static List<Double> returnBi(int h) {
List<Double> list = new ArrayList<Double>();
ArrayList<Integer> words_after_h = new ArrayList<Integer>();
for (int j = 1; j < corpus.size(); j++) {
if (corpus.get(j - 1) == h)
words_after_h.add(corpus.get(j));
}
for (int i = 0; i < 4700; i++) {
int count = 0;
for (int k = 0; k < corpus.size() - 1; k++) {
if (corpus.get(k) == h && corpus.get(k + 1) == i)
count++;
}
list.add(count / (double) words_after_h.size());
}
list.add(words_after_h.size() * 1.0);
return list;
}
private static List<Double> returnTri(int h1, int h2) {
List<Double> list = new ArrayList<Double>();
ArrayList<Integer> words_after_h = new ArrayList<Integer>();
for (int j = 2; j < corpus.size(); j++) {
if (corpus.get(j - 1) == h2 && corpus.get(j - 2) == h1)
words_after_h.add(corpus.get(j));
}
for (int i = 0; i < 4700; i++) {
int count = 0;
for (int k = 0; k < corpus.size() - 2; k++) {
if (corpus.get(k) == h1 && corpus.get(k + 1) == h2 && corpus.get(k + 2) == i)
count++;
}
list.add(count / (double) words_after_h.size());
}
list.add(words_after_h.size() * 1.0);
return list;
}
public static double[] checkIntervals(int option, double prob, int h, int h1, int h2) {
double[] answer = new double[3];
if (option == 1) {
double[] p = returnUni();
if (prob >= 0.0 && prob <= p[0]) {
answer[0] = 0;
answer[1] = 0.0;
answer[2] = p[0];
} else {
for (int i = 1; i < p.length; i++) {
// left is the summation of 0 to i - 1
Double left = 0.0;
Double right = 0.0;
for (int j = 0; j <= i - 1; j++) {
left = left + p[j];
right = right + p[j];
if (j == i - 1) {
right = right + p[j + 1];
}
}
if (prob > left && prob <= right) {
answer[0] = i;
answer[1] = left;
answer[2] = right;
break;
}
}
}
} else if (option == 2) {
List<Double> bigram = returnBi(h);
bigram.remove(bigram.size() - 1);
int start = 0;
for (int i = 0; i < bigram.size(); i++) {
if (bigram.get(i) != 0) {
start = i;
break;
}
}
if (prob >= 0.0 && prob <= bigram.get(start)) {
answer[0] = start;
answer[1] = 0.0;
answer[2] = bigram.get(start);
} else {
for (int i = start + 1; i < bigram.size(); i++) {
// left is the summation of 0 to i - 1
Double left = 0.0;
Double right = 0.0;
if (bigram.get(i) != 0) {
for (int j = 0; j <= i - 1; j++) {
left = left + bigram.get(j);
right = right + bigram.get(j);
if (j == i - 1) {
right = right + bigram.get(j + 1);
}
}
if (prob > left && prob <= right) {
answer[0] = i;
answer[1] = left;
answer[2] = right;
break;
}
}
}
}
} else {
List<Double> trigram = returnTri(h1, h2);
trigram.remove(trigram.size() - 1);
int start = 0;
for (int i = 0; i < trigram.size(); i++) {
if (trigram.get(i) != 0) {
start = i;
break;
}
}
if (prob >= 0.0 && prob <= trigram.get(start)) {
answer[0] = start;
answer[1] = 0.0;
answer[2] = trigram.get(start);
} else {
for (int i = start + 1; i < trigram.size(); i++) {
// left is the summation of 0 to i - 1
Double left = 0.0;
Double right = 0.0;
if (trigram.get(i) != 0) {
for (int j = 0; j <= i - 1; j++) {
left = left + trigram.get(j);
right = right + trigram.get(j);
if (j == i - 1) {
right = right + trigram.get(j + 1);
}
}
if (prob > left && prob <= right) {
answer[0] = i;
answer[1] = left;
answer[2] = right;
break;
}
}
}
}
}
return answer;
}
}