-
Notifications
You must be signed in to change notification settings - Fork 2
/
Apriori.java
575 lines (435 loc) · 18.6 KB
/
Apriori.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import java.util.*;
import java.io.*;
/**
* @author Purvesh
* Apriori Algorithm implementation
*
*/
public class Apriori {
public static void main(String[] args)throws IOException {
Scanner s = new Scanner(System.in);
System.out.println("Enter path of your data file: (It should be in C:\\path\\innerpath\\filename format)");
String path = s.nextLine();
System.out.println("Enter Minimum Support rate(IN range of 1-10)");
float min_supp = s.nextFloat();
if(min_supp <1 || min_supp > 10){
System.out.println("Invalid Support rate choice");
System.exit(0);
}
System.out.println("Enter Minimum confidence rate(IN range of 0.1-10)");
float min_conf = s.nextFloat();
if(min_conf<0.1 || min_conf >10){
System.out.println("Invalid confidence rate choice");
System.exit(0);
}
String e_path = "C:\\Data\\hashmapoutput88";
try {
HashMap <String,Integer> m1 = new LinkedHashMap<String,Integer>();
HashMap <String,Integer> m2 = new LinkedHashMap<String,Integer>();
HashMap <String,Integer> q = new LinkedHashMap<String,Integer>();
ArrayList<String> a1 = new ArrayList<String>();
ArrayList <String> a2 = new ArrayList<String>();
File fr = new File(path);
Scanner sc = new Scanner(fr);
int my_count=0;
while (sc.hasNext()) {
my_count++;
String words = sc.next();
String[] space = words.split(" ");
for (int i = 0; i < space.length; i++) {
a1.add(space[i]);
}
}
System.out.println(a1);
System.out.println("For Frequent Set 1: ");
// find_word_set_from_data_set() function will generate from Frequent item itemset from candidate item set
// Parameter of this function are k-1(candidate item set), data path,user specified support, and last is K(i.e k-frequent item itemset)
m1 = find_word_set_from_data_set(a1, path, min_supp,1);
//printing frequent itemset (here 1 frequnt item set)
Set q1 = m1.entrySet();
Iterator itq1 = q1.iterator();
while(itq1.hasNext()) {
Map.Entry mentry = (Map.Entry)itq1.next();
//System.out.print(mentry.getKey()+" -- "+mentry.getValue()+"\n");
String hash_key = (String) mentry.getKey();
int hash_value = (int) mentry.getValue();
hash_key.trim();
q.put(hash_key, hash_value);
}
System.out.println("For Frequent Set 2: ");
// candidate_itemset function will generate candidate item set, this function will gennerate upto 2 candidate itemset.
// it has 2 parameters, 1) previous frequent itemset, and 2)K
a2 = candidate_itemset(m1,2);
m2 = find_word_set_from_data_set(a2, path, min_supp,2);
Set q2 = m2.entrySet();
Iterator itq2 = q2.iterator();
while(itq2.hasNext()) {
Map.Entry mentry = (Map.Entry)itq2.next();
//System.out.print(mentry.getKey()+" -- "+mentry.getValue()+"\n");
String hash_key = (String) mentry.getKey();
int hash_value = (int) mentry.getValue();
hash_key.trim();
q.put(hash_key, hash_value);
}
Set set = m2.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
System.out.print(mentry.getKey()+" ==== "+mentry.getValue()+"\n");
}
System.out.println("\n\n");
//writetofile(file,m2,2);
// write_result_to_outputfile2 function will store output to external file.
write_result_to_outputfile2(m2,2,e_path);
HashMap <String,Integer> m = new LinkedHashMap<String,Integer>();
ArrayList<String> a = new ArrayList<String>();
int count=3;
//this loop will automatically generate K-frequent item set.
while(!m2.isEmpty()){
a = candidate_itemset_general(m2,(count-1));
m2.clear();
System.out.println("For Iterator set "+count+":");
m = find_word_set_from_data_set(a, path, min_supp,count);
Set q4 = m.entrySet();
Iterator itq4 = q4.iterator();
while(itq4.hasNext()) {
Map.Entry mentry = (Map.Entry)itq4.next();
//System.out.print(mentry.getKey()+" -- "+mentry.getValue()+"\n");
String hash_keyy = (String) mentry.getKey();
int hash_valuey = (int) mentry.getValue();
hash_keyy.trim();
hash_keyy.replaceAll("\\s+$", "");
//System.out.println("----="+hash_keyy+"----=");
q.put(hash_keyy, hash_valuey);
}
//writetofile(file,m,count);
write_result_to_outputfile2(m,count,e_path);
Set set2 = m.entrySet();
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator2.next();
System.out.print(mentry.getKey()+" -- "+mentry.getValue()+"\n");
String hash_key = (String) mentry.getKey();
int hash_value = (int) mentry.getValue();
m2.put(hash_key, hash_value);
}
System.out.println("\n\n");
m.clear();
count++;
}
HashMap <Integer,String> h88 = new LinkedHashMap<Integer,String>();
// for storing subset permutation in external file named P
File f1 =new File("C:\\Data\\p");
f1.delete();
if(!f1.exists()){
f1.createNewFile();
}
File f2 =new File("C:\\Data\\p2");
f2.delete();
if(!f2.exists()){
f2.createNewFile();
}
//In this file all frequent item set is stored.
File file88 = new File("C:\\Data\\hashmapoutput88");
FileReader ff= new FileReader(file88.getAbsolutePath());
BufferedReader bb = new BufferedReader(ff);
HashMap <String, String> hr = new LinkedHashMap<String, String>();
ArrayList<String> my = new ArrayList<String>();
//IN this loop subset of all frequent item set will be generated
for(int cnt=3; cnt<6; cnt++){
String line;
String compare = "iterator "+ cnt;
System.out.println(cnt+" -- "+compare);
while(!(line = bb.readLine()).equals(compare)){
//System.out.println("line - - "+line);
int length = line.split("\\s+").length;
if(length>=3){
HashMap <Integer,String> h = new LinkedHashMap<Integer,String>();
//System.out.println("compare ----------- "+compare);
//System.out.println("line ----------- "+line);
String left[] = line.split("=====");
String[] input = left[0].trim().split("\\s+");
//this for loop will convert word to int and and this integer is as a input to permutation function
for(int i=0;i<input.length;i++){
h.put(i, input[i]);
}
String permu_input="";
Set pur = h.entrySet();
Iterator it9 = pur.iterator();
while(it9.hasNext()) {
Map.Entry mentry = (Map.Entry)it9.next();
//System.out.println("keyyyyyy "+mentry.getKey()+" "+mentry.getValue()+"\n");
permu_input = permu_input+ mentry.getKey();
}
//This function will generate subset/permutation of giiven input.
permutation(permu_input,f1);
FileReader ff1= new FileReader(f1.getAbsolutePath());
BufferedReader bb1 = new BufferedReader(ff1);
String p_line;
while((p_line = bb1.readLine())!=null){
//System.out.println(p_line);
String[] d = p_line.split("");
String final_per="";
int f=0;
for(String se : d){
int j = Integer.parseInt(se);
String b = h.get(j);
my.add(b);
if(f==0){
final_per = b;
} else{
final_per = final_per+" "+b;
}
f++;
}
hr.put(final_per, ""); //this hasmap will store all permutation.
}
}
}
}
HashMap <String,String> hf = new LinkedHashMap<String,String>();
FileReader fw = new FileReader(f1.getName());
BufferedReader bw = new BufferedReader(fw);
String read;
System.out.println("Start : -----------------------------");
Set q24 = q.entrySet();
Iterator itq24 = q24.iterator();
while(itq24.hasNext()) {
Map.Entry mentry = (Map.Entry)itq24.next();
System.out.print(mentry.getKey()+" -- "+mentry.getValue()+"\n");
}
Set q8 = q.entrySet();
Iterator itq8 = q8.iterator();
while(itq8.hasNext()) {
Map.Entry mentry = (Map.Entry)itq8.next();
//System.out.print("----="+mentry.getKey()+"--=="+mentry.getValue()+"\n");
//String hash_key = (String) mentry.getKey();
//int hash_value = (int) mentry.getValue();
//q.put(hash_key, hash_value);
}
//System.exit(0);
Set pur2 = hr.entrySet();
Iterator it92 = pur2.iterator();
System.out.println("\n\n Generated Rules : \n\n");
//this is file where all rules will be stored.
File fout = new File("D:\\rules");
if(!fout.exists()){
fout.createNewFile();
}
FileWriter ffout= new FileWriter(fout.getAbsolutePath(),true);
BufferedWriter bbout = new BufferedWriter(ffout);
bbout.write("Summary:"+"\n");
bbout.write("Total rows in original set : "+my_count+"\n");
bbout.write("The selected Measures : Support="+min_supp+" ," + "Confidence="+ min_conf+"\n");
bbout.write("------------------------------------------"+"\n");
bbout.write("Discovered Rules:" + "\n");
int rule_cnt=1;
//this is looop where confidence will be count, pruning would be done and all rules are generated.
while(it92.hasNext()) {
Map.Entry mentry = (Map.Entry)it92.next();
String h_key = (String) mentry.getKey();
h_key.trim();
String[] tom2 = h_key.split(" ");
for (String hk : tom2){
if(q.containsKey(h_key)){
float first_t = q.get(h_key); // getting occurence of word
float second_t = q.get(hk);
//System.out.println(h_key+" = "+first_t);
//System.out.println(hk+" = "+second_t);
float confidence_ratio = first_t/second_t; //calculate confidence rate
if(confidence_ratio>=min_conf){ //pruning step
System.out.println("Rule "+rule_cnt+": Confident ratio for "+h_key+" and "+hk+" = "+ confidence_ratio);
// writing to external file
bbout.write("Rule "+rule_cnt+": Confident ratio for "+h_key+" and "+hk+" = "+ confidence_ratio+"\n");
rule_cnt++;
}
} else{
h_key = h_key+" ";
if(q.containsKey(h_key)){
float first_t = q.get(h_key);
float second_t = q.get(hk);
float confidence_ratio = first_t/second_t; //calculate confidence rate
if(confidence_ratio>=min_conf){
System.out.println("Rule "+rule_cnt+": Confident ratio for "+h_key+" and "+hk+" = "+ confidence_ratio);
bbout.write("Rule "+rule_cnt+": Confident ratio for "+h_key+" and "+hk+" = "+ confidence_ratio+"\n");
rule_cnt++;
}
} else{
continue;
}
}
}
}
bbout.write("Total rules discovered:"+ rule_cnt+"\n");
System.out.println("\n\n ");
System.out.println("------Finished----");
} catch (Exception e) {
System.out.println("Error");
}
}
//this will generate permutation/subset
public static void permutation(String str, File f1) throws IOException {
permutation("", str,f1);
}
private static void permutation(String prefix, String str, File f1) throws IOException {
int n = str.length();
if (n == 0){
//System.out.println("prefix "+prefix);
FileWriter fw = new FileWriter(f1.getAbsolutePath(),true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(prefix+"\n");
bw.close();
}
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n),f1);
}
}
// Write the final output to output file
private static void write_result_to_outputfile2(HashMap<String, Integer> m, int i,String output_path) {
try {
File file = new File(output_path);
if (!file.exists()) { // create a file if it doesn't exist
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("iterator "+ i + "\n");
Set set88 = m.entrySet();
Iterator iterator88 = set88.iterator();
while(iterator88.hasNext()) {
Map.Entry mentry88 = (Map.Entry)iterator88.next();
//System.out.print(mentry.getKey()+" -- "+mentry.getValue()+"\n");
String hash_key = (String) mentry88.getKey();
int hash_value = (int) mentry88.getValue();
bw.write(hash_key+" ===== "+hash_value+"\n");
}
bw.close();
System.out.println("Final Result written to output file.. Done!!");
} catch (IOException e) {
e.printStackTrace();
}
}
// to generate K candidate item set (3 to K)
private static ArrayList<String> candidate_itemset_general(HashMap<String, Integer> m, int itemsetNumber) {
ArrayList <String> a = new ArrayList<String>();
ArrayList<String> output = new ArrayList<String>();
Set set = m.entrySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()) {
Map.Entry mentry = (Map.Entry)iterator.next();
a.add((String) mentry.getKey());
}
int len = a.size();
for(int k=0; k<(len-1); k++){
for(int l=k+1; l<len; l++){
ArrayList <String> a1 = new ArrayList<String>();
ArrayList <String> intersection = new ArrayList<String>();
ArrayList <String> first_arraylist = new ArrayList<String>();
ArrayList <String> second_arraylist = new ArrayList<String>();
String[] first = a.get(k).split("\\s+");
String[] second = a.get(l).split("\\s+");
for(String g:first){
first_arraylist.add(g);
}
for(String w:second){
second_arraylist.add(w);
}
String flag = "";
for(int b=0; b<(itemsetNumber-1); b++){
if(first[b].equals(second[b])){
flag = "true";
} else {
flag = "false";
break;
}
}
int first_length = first_arraylist.size();
if(flag.equals("true")){
intersection = intersection(first,second);
// this loop will go itemset generation
if(intersection.size() == (first_length-1)){
a1.addAll(first_arraylist);
a1.removeAll(second_arraylist);
a1.addAll(second_arraylist);
String final_string="";
for(String h:a1){
final_string = final_string+h+" ";
}
output.add(final_string);
}
}
}
}
return output;
}
//this will get unique intersection
private static ArrayList<String> intersection(String[] first, String[] second) {
ArrayList <String> temp_arr = new ArrayList<String>();
for (String d1:first) {
for (String d2:second) {
if ((d1.equals(d2))) {
temp_arr.add(d1);
}
}
}
return temp_arr;
}
//this will generate 1 and 2 Candidate itemset.
private static ArrayList<String> candidate_itemset(HashMap<String, Integer> m, int itemsetNumber) {
ArrayList <String> a = new ArrayList<String>();
ArrayList <String> a1 = new ArrayList<String>();
for (String key : m.keySet()) {
String result = key.trim();
a.add(result);
}
int len = a.size();
for(int k=0; k<len; k++){
for(int l=k+1; l<len; l++){
a1.add(a.get(k)+ " "+a.get(l));
}
}
return a1;
}
// This will generate 1 to K frequent itemset.
private static HashMap<String, Integer> find_word_set_from_data_set(ArrayList<String> a, String path, float min_supp, int itemsetNumber) throws IOException {
HashMap<String, Integer> m = new HashMap<String, Integer>();
for(String word_set : a){
int cnt = 0;
String current_line;
BufferedReader br = new BufferedReader(new FileReader(path));
while ((current_line = br.readLine()) != null) {
String[] words = word_set.split("\\s+");
int k=0;
for(int j=0; j<itemsetNumber; j++){
if(current_line.contains(words[j])){
k++;
}
}
if(k == itemsetNumber){
cnt++;
if(cnt>=min_supp){
m.put(word_set, cnt);
}
}
}
}
return m;
}
// Write the final output to output file
private static void write_result_to_outputfile(String output_text, String output_path) {
try {
File file = new File(output_path);
if (!file.exists()) { // create a file if it doesn't exist
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(output_text);
bw.close();
System.out.println("Final Result written to output file.. Done!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}