-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessQuery.flex.java
More file actions
401 lines (384 loc) · 10.5 KB
/
ProcessQuery.flex.java
File metadata and controls
401 lines (384 loc) · 10.5 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import java.util.*;
import java.io.*;
import java.sql.Timestamp;
class ProcessQuery{
public static PrintWriter writer = null;
public static int count =0;
public static void main(String[] args) throws IOException{
//int count = 0;
if(args.length != 1){
System.out.println("Please enter two arguments \"Input directory name\" and \"Output directory name\" ");
System.exit(-1);
}
File outputFolder = new File("processed_query");
if(!outputFolder.exists()){
if(outputFolder.mkdir()){}
else{System.out.println("Failed to create Output directory. ");}
}
File inputFolder = new File(args[0]);
File[] fileNames = inputFolder.listFiles();
//Arrays.sort(fileNames);
for(File file: fileNames){
if(file.isFile()){
File outputFile = null;
try(BufferedReader br = new BufferedReader(new FileReader(file))){
MyLexer yy = new MyLexer(br);
try{
outputFile = new File("processed_query" +File.separator + file.getName());
count++;
writer = new PrintWriter(outputFile);
while (yy.yylex() !=null){
}
}
catch(IOException e){
System.out.println("Cannot create output file "+file.getName()+".txt");
}catch(Exception e){
/*System.out.println("don't know what happend with file: " + file.getName() );
e.printStackTrace();
System.out.println();*/
}
finally {
if(writer != null){
writer.flush();
writer.close();
}
}
}
}
}
}
public static void downcase(String str){
writer.println(str.toLowerCase());
}
}
class MyLexer {
private final int YY_BUFFER_SIZE = 512;
private final int YY_F = -1;
private final int YY_NO_STATE = -1;
private final int YY_NOT_ACCEPT = 0;
private final int YY_START = 1;
private final int YY_END = 2;
private final int YY_NO_ANCHOR = 4;
private final int YY_BOL = 128;
private final int YY_EOF = 129;
private java.io.BufferedReader yy_reader;
private int yy_buffer_index;
private int yy_buffer_read;
private int yy_buffer_start;
private int yy_buffer_end;
private char yy_buffer[];
private boolean yy_at_bol;
private int yy_lexical_state;
MyLexer (java.io.Reader reader) {
this ();
if (null == reader) {
throw (new Error("Error: Bad input stream initializer."));
}
yy_reader = new java.io.BufferedReader(reader);
}
MyLexer (java.io.InputStream instream) {
this ();
if (null == instream) {
throw (new Error("Error: Bad input stream initializer."));
}
yy_reader = new java.io.BufferedReader(new java.io.InputStreamReader(instream));
}
private MyLexer () {
yy_buffer = new char[YY_BUFFER_SIZE];
yy_buffer_read = 0;
yy_buffer_index = 0;
yy_buffer_start = 0;
yy_buffer_end = 0;
yy_at_bol = true;
yy_lexical_state = YYINITIAL;
}
private boolean yy_eof_done = false;
private final int YYINITIAL = 0;
private final int yy_state_dtrans[] = {
0
};
private void yybegin (int state) {
yy_lexical_state = state;
}
private int yy_advance ()
throws java.io.IOException {
int next_read;
int i;
int j;
if (yy_buffer_index < yy_buffer_read) {
return yy_buffer[yy_buffer_index++];
}
if (0 != yy_buffer_start) {
i = yy_buffer_start;
j = 0;
while (i < yy_buffer_read) {
yy_buffer[j] = yy_buffer[i];
++i;
++j;
}
yy_buffer_end = yy_buffer_end - yy_buffer_start;
yy_buffer_start = 0;
yy_buffer_read = j;
yy_buffer_index = j;
next_read = yy_reader.read(yy_buffer,
yy_buffer_read,
yy_buffer.length - yy_buffer_read);
if (-1 == next_read) {
return YY_EOF;
}
yy_buffer_read = yy_buffer_read + next_read;
}
while (yy_buffer_index >= yy_buffer_read) {
if (yy_buffer_index >= yy_buffer.length) {
yy_buffer = yy_double(yy_buffer);
}
next_read = yy_reader.read(yy_buffer,
yy_buffer_read,
yy_buffer.length - yy_buffer_read);
if (-1 == next_read) {
return YY_EOF;
}
yy_buffer_read = yy_buffer_read + next_read;
}
return yy_buffer[yy_buffer_index++];
}
private void yy_move_end () {
if (yy_buffer_end > yy_buffer_start &&
'\n' == yy_buffer[yy_buffer_end-1])
yy_buffer_end--;
if (yy_buffer_end > yy_buffer_start &&
'\r' == yy_buffer[yy_buffer_end-1])
yy_buffer_end--;
}
private boolean yy_last_was_cr=false;
private void yy_mark_start () {
yy_buffer_start = yy_buffer_index;
}
private void yy_mark_end () {
yy_buffer_end = yy_buffer_index;
}
private void yy_to_mark () {
yy_buffer_index = yy_buffer_end;
yy_at_bol = (yy_buffer_end > yy_buffer_start) &&
('\r' == yy_buffer[yy_buffer_end-1] ||
'\n' == yy_buffer[yy_buffer_end-1] ||
2028/*LS*/ == yy_buffer[yy_buffer_end-1] ||
2029/*PS*/ == yy_buffer[yy_buffer_end-1]);
}
private java.lang.String yytext () {
return (new java.lang.String(yy_buffer,
yy_buffer_start,
yy_buffer_end - yy_buffer_start));
}
private int yylength () {
return yy_buffer_end - yy_buffer_start;
}
private char[] yy_double (char buf[]) {
int i;
char newbuf[];
newbuf = new char[2*buf.length];
for (i = 0; i < buf.length; ++i) {
newbuf[i] = buf[i];
}
return newbuf;
}
private final int YY_E_INTERNAL = 0;
private final int YY_E_MATCH = 1;
private java.lang.String yy_error_string[] = {
"Error: Internal error.\n",
"Error: Unmatched input.\n"
};
private void yy_error (int code,boolean fatal) {
java.lang.System.out.print(yy_error_string[code]);
java.lang.System.out.flush();
if (fatal) {
throw new Error("Fatal Error.\n");
}
}
private int[][] unpackFromString(int size1, int size2, String st) {
int colonIndex = -1;
String lengthString;
int sequenceLength = 0;
int sequenceInteger = 0;
int commaIndex;
String workString;
int res[][] = new int[size1][size2];
for (int i= 0; i < size1; i++) {
for (int j= 0; j < size2; j++) {
if (sequenceLength != 0) {
res[i][j] = sequenceInteger;
sequenceLength--;
continue;
}
commaIndex = st.indexOf(',');
workString = (commaIndex==-1) ? st :
st.substring(0, commaIndex);
st = st.substring(commaIndex+1);
colonIndex = workString.indexOf(':');
if (colonIndex == -1) {
res[i][j]=Integer.parseInt(workString);
continue;
}
lengthString =
workString.substring(colonIndex+1);
sequenceLength=Integer.parseInt(lengthString);
workString=workString.substring(0,colonIndex);
sequenceInteger=Integer.parseInt(workString);
res[i][j] = sequenceInteger;
sequenceLength--;
}
}
return res;
}
private int yy_acpt[] = {
/* 0 */ YY_NOT_ACCEPT,
/* 1 */ YY_NO_ANCHOR,
/* 2 */ YY_NO_ANCHOR,
/* 3 */ YY_NO_ANCHOR,
/* 4 */ YY_NO_ANCHOR,
/* 5 */ YY_NO_ANCHOR,
/* 6 */ YY_NO_ANCHOR,
/* 7 */ YY_NO_ANCHOR,
/* 8 */ YY_NOT_ACCEPT,
/* 9 */ YY_NO_ANCHOR,
/* 10 */ YY_NO_ANCHOR,
/* 11 */ YY_NOT_ACCEPT,
/* 12 */ YY_NO_ANCHOR,
/* 13 */ YY_NOT_ACCEPT,
/* 14 */ YY_NOT_ACCEPT,
/* 15 */ YY_NOT_ACCEPT,
/* 16 */ YY_NOT_ACCEPT,
/* 17 */ YY_NOT_ACCEPT,
/* 18 */ YY_NO_ANCHOR,
/* 19 */ YY_NOT_ACCEPT,
/* 20 */ YY_NOT_ACCEPT,
/* 21 */ YY_NO_ANCHOR,
/* 22 */ YY_NOT_ACCEPT,
/* 23 */ YY_NO_ANCHOR
};
private int yy_cmap[] = unpackFromString(1,130,
"11:9,18,2,11:2,2,11:19,12,11,12:4,11:3,12,11:3,17,9,14:10,8,11,1,11,3,11,15" +
",16:26,11:3,12,13,11,16:7,4,16:7,6,16:2,7,5,16:2,10,16:3,11,18,11,12,11,0:2")[0];
private int yy_rmap[] = unpackFromString(1,24,
"0,1,2,1,3,1,4,5,2,1,6,7,8,9,4,10,11,12,13,14,15,16,17,18")[0];
private int yy_nxt[][] = unpackFromString(19,19,
"1,2,3,9,4,23:3,9:2,23,9:2,23,9:2,23,9,3,-1:20,8:2,5,8:15,-1:4,23,21,23:2,-1" +
":2,23,-1:2,23:2,11,23,-1:6,6:4,-1:2,6,-1:5,6,-1:6,7:4,-1,7:2,-1,7:6,-1:5,23" +
":3,12,15,-1,23,-1:2,23:2,11,23,-1:6,13:4,-1:2,13,-1:5,13,-1:6,23:4,15,-1,23" +
",-1:2,23:2,11,23,-1:6,13:4,-1:2,13,-1:5,13,14,-1:10,19,-1:19,22,-1:9,7,-1,7" +
":16,-1:4,23:2,10,23,-1:2,23,-1:2,23:2,11,23,-1:11,16,-1:19,17,-1:12,23,18,2" +
"3:2,-1:2,23,-1:2,23:2,11,23,-1:12,20,-1:12,23:4,-1:2,23,-1:2,23:2,11,23,-1:" +
"2");
public String yylex ()
throws java.io.IOException {
int yy_lookahead;
int yy_anchor = YY_NO_ANCHOR;
int yy_state = yy_state_dtrans[yy_lexical_state];
int yy_next_state = YY_NO_STATE;
int yy_last_accept_state = YY_NO_STATE;
boolean yy_initial = true;
int yy_this_accept;
yy_mark_start();
yy_this_accept = yy_acpt[yy_state];
if (YY_NOT_ACCEPT != yy_this_accept) {
yy_last_accept_state = yy_state;
yy_mark_end();
}
while (true) {
if (yy_initial && yy_at_bol) yy_lookahead = YY_BOL;
else yy_lookahead = yy_advance();
yy_next_state = YY_F;
yy_next_state = yy_nxt[yy_rmap[yy_state]][yy_cmap[yy_lookahead]];
if (YY_EOF == yy_lookahead && true == yy_initial) {
return null;
}
if (YY_F != yy_next_state) {
yy_state = yy_next_state;
yy_initial = false;
yy_this_accept = yy_acpt[yy_state];
if (YY_NOT_ACCEPT != yy_this_accept) {
yy_last_accept_state = yy_state;
yy_mark_end();
}
}
else {
if (YY_NO_STATE == yy_last_accept_state) {
throw (new Error("Lexical Error: Unmatched Input."));
}
else {
yy_anchor = yy_acpt[yy_last_accept_state];
if (0 != (YY_END & yy_anchor)) {
yy_move_end();
}
yy_to_mark();
switch (yy_last_accept_state) {
case 1:
case -2:
break;
case 2:
{}
case -3:
break;
case 3:
{}
case -4:
break;
case 4:
{ProcessQuery.downcase(new String(yytext()));}
case -5:
break;
case 5:
{}
case -6:
break;
case 6:
{ProcessQuery.downcase(new String(yytext()));}
case -7:
break;
case 7:
{ProcessQuery.downcase(new String(yytext()));}
case -8:
break;
case 9:
{}
case -9:
break;
case 10:
{ProcessQuery.downcase(new String(yytext()));}
case -10:
break;
case 12:
{ProcessQuery.downcase(new String(yytext()));}
case -11:
break;
case 18:
{ProcessQuery.downcase(new String(yytext()));}
case -12:
break;
case 21:
{ProcessQuery.downcase(new String(yytext()));}
case -13:
break;
case 23:
{ProcessQuery.downcase(new String(yytext()));}
case -14:
break;
default:
yy_error(YY_E_INTERNAL,false);
case -1:
}
yy_initial = true;
yy_state = yy_state_dtrans[yy_lexical_state];
yy_next_state = YY_NO_STATE;
yy_last_accept_state = YY_NO_STATE;
yy_mark_start();
yy_this_accept = yy_acpt[yy_state];
if (YY_NOT_ACCEPT != yy_this_accept) {
yy_last_accept_state = yy_state;
yy_mark_end();
}
}
}
}
}
}