-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatbot.java
More file actions
295 lines (279 loc) · 10.2 KB
/
Copy pathChatbot.java
File metadata and controls
295 lines (279 loc) · 10.2 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
295
import java.util.*;
import java.io.*;
public class Chatbot{
private static String filename = "./WARC201709_wid.txt";
private static int vSize = 4699;
private static ArrayList<Integer> readCorpus(){
ArrayList<Integer> corpus = new ArrayList<Integer>();
try{
File f = new File(filename);
Scanner 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;
}
public static int[][] conditionProb(int h, ArrayList<Integer> corpus){
int[][] countMatrix = new int[4700][2];
for (int i = 0; i < countMatrix.length; i++) {
countMatrix[i][0] = i;
countMatrix[i][1] = 0;
}
for (int u = 0; u <= vSize; u++) {
for (int i = 0; i < corpus.size() - 1; i++) {
if(h == corpus.get(i) && u == corpus.get(i+1)){
countMatrix[u][1]++;
}
}
}
return countMatrix;
}
public static int[][] conditionProb(int h1, int h2, ArrayList<Integer> corpus){
int[][] countMatrix = new int[4700][2];
for (int i = 0; i < countMatrix.length; i++) {
countMatrix[i][0] = i;
countMatrix[i][1] = 0;
}
for (int u = 0; u <= vSize; u++) {
for (int i = 0; i < corpus.size() - 1; i++) {
if(h1 == corpus.get(i) && h2 == corpus.get(i + 1) && u == corpus.get(i+2)){
countMatrix[u][1]++;
}
}
}
return countMatrix;
}
public static void unigram(List<double[]> segList, ArrayList<Integer> corpus){
double sumProb = 0;
for (int i = 0; i <= vSize; i++) {
int countI = 0;
for (Integer indice: corpus
) {
if(i == indice){
countI++;
}
}
double[] segment = {i, sumProb, sumProb + countI/(double)corpus.size()};
if(segment[1] != segment[2])
{
segList.add(segment);
sumProb += countI/(double)corpus.size();
}
}
}
public static int findInterval(List<double[]> segList, double r){
int w = 0;
for (int i = 0; i < segList.size(); i++) {
if(r == 0){
w = (int)segList.get(0)[0];
}
else{
if(r > segList.get(i)[1] && r <= segList.get(i)[2]){
w = (int)segList.get(i)[0];
}
}
}
return w;
}
public static List<double[]> addToSeglist(List<double[]> segList, int[][] countMatrix, int count){
double sumProb = 0;
for (int i = 0; i < vSize; i++) {
double left = sumProb;
sumProb = sumProb + (double)countMatrix[i][1] / (double)count;
if(left != sumProb) {
segList.add(new double[]{i, left, sumProb});
}
}
return segList;
}
public static void printSegList(List<double[]> segList, double r){
if(r == 0){
System.out.println((int)segList.get(0)[0]);
System.out.println(String.format("%.7f", segList.get(0)[1]));
System.out.println(String.format("%.7f", segList.get(0)[2]));
}
else {
for (double[] seg : segList
) {
if (r > seg[1] && r <= seg[2]) {
System.out.println((int) seg[0]);
System.out.println(String.format("%.7f", seg[1]));
System.out.println(String.format("%.7f", seg[2]));
}
}
}
}
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]);
int count = 0;
//TODO count occurence of w
for (Integer indice:corpus
) {
if(indice == w){
count++;
}
}
System.out.println(count);
System.out.println(String.format("%.7f",count/(double)corpus.size()));
}
else if(flag == 200){
//TODO generate
int n1 = Integer.valueOf(args[1]);
int n2 = Integer.valueOf(args[2]);
double r = (double)n1 / (double)n2;
List<double[]> segList = new ArrayList<>();
unigram(segList,corpus);
printSegList(segList,r);
}
else if(flag == 300){
int h = Integer.valueOf(args[1]);
int w = Integer.valueOf(args[2]);
//TODO
int[][] countMatrix = conditionProb(h,corpus);
//output
int count = 0;
for (int i = 0; i < countMatrix.length; i++) {
count += countMatrix[i][1];
}
System.out.println(countMatrix[w][1]);
System.out.println(count);
System.out.println(String.format("%.7f", (double)countMatrix[w][1]/(double) count));
}
else if(flag == 400){
int n1 = Integer.valueOf(args[1]);
int n2 = Integer.valueOf(args[2]);
int h = Integer.valueOf(args[3]);
//TODO
double r = (double) n1 / (double) n2;
ArrayList<double[]> segList = new ArrayList<>();
int[][] countMatrix = conditionProb(h,corpus);
int count = 0;
for (int i = 0; i < countMatrix.length; i++) {
count += countMatrix[i][1];
}
addToSeglist(segList,countMatrix,count);
printSegList(segList,r);
}
else if(flag == 500){
int h1 = Integer.valueOf(args[1]);
int h2 = Integer.valueOf(args[2]);
int w = Integer.valueOf(args[3]);
int count = 0;
//TODO
int[][] countMatrix = conditionProb(h1,h2,corpus);
for (int i = 0; i < countMatrix.length; i++) {
count += countMatrix[i][1];
}
System.out.println(countMatrix[w][1]);
System.out.println(count);
if(count == 0)
System.out.println("undefined");
else
System.out.println(String.format("%.7f", countMatrix[w][1]/(double)count));
}
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]);
//TODO
int count = 0;
double r = (double) n1 / (double) n2;
int[][] countMatrix = conditionProb(h1,h2,corpus);
for (int i = 0; i < countMatrix.length; i++) {
count += countMatrix[i][1];
}
if(count == 0){
System.out.println("undefined");
}
else {
ArrayList<double[]> segList = new ArrayList<>();
addToSeglist(segList,countMatrix,count);
printSegList(segList, r);
}
}
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){
// TODO Generate first word using r
double r = rng.nextDouble();
if(h1 == 9 || h1 == 10 || h1 == 12){
return;
}
ArrayList<double[]> segList = new ArrayList<>();
unigram(segList, corpus);
h1 = findInterval(segList,r);
System.out.println(h1);
// TODO Generate second word using r
r = rng.nextDouble();
int[][] countMatrix = conditionProb(h1, corpus);
segList = new ArrayList<>();
int count = 0;
for (int i = 0; i < countMatrix.length; i++) {
count += countMatrix[i][1];
}
addToSeglist(segList,countMatrix,count);
h2 = findInterval(segList,r);
System.out.println(h2);
}
else if(t == 1){
h1 = Integer.valueOf(args[3]);
// TODO Generate second word using r
double r = rng.nextDouble();
int[][] countMatrix = conditionProb(h1, corpus);
List<double[]>segList = new ArrayList<>();
int count = 0;
for (int i = 0; i < countMatrix.length; i++) {
count += countMatrix[i][1];
}
addToSeglist(segList,countMatrix,count);
h2 = findInterval(segList,r);
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;
// TODO Generate new word using h1,h2
int[][] countMatrix = conditionProb(h1,h2,corpus);
List<double[]> segList = new ArrayList<>();
int count = 0;
for (int i = 0; i < countMatrix.length; i++) {
count += countMatrix[i][1];
}
if(count == 0){
countMatrix = conditionProb(h2,corpus);
for (int i = 0; i < countMatrix.length; i++) {
count += countMatrix[i][1];
}
}
addToSeglist(segList, countMatrix, count);
w = findInterval(segList,r);
System.out.println(w);
h1 = h2;
h2 = w;
}
}
return;
}
}