-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs.java
365 lines (335 loc) · 8.11 KB
/
rs.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
package seminar;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Scanner;
public class rs {
public static void main(String[] args) throws IOException {
File code = new File("logfile.txt");
ArrayList<String>[] sessions = new ArrayList[7];
for (int i = 0; i < 7; i++) {
sessions[i] = new ArrayList<String>();
}
ArrayList<String> list = new ArrayList<String>();
HashMap<String, Integer> ip_address = new HashMap<>();
HashMap<String, Integer> page_freq = new HashMap<>();
HashMap<Integer, Integer> no_of_pages = new HashMap<>();
for (int i = 1; i <= 7; i++) {
no_of_pages.put(i, 0);
}
Scanner sc = new Scanner(code);
int i = 1;
while (sc.hasNextLine()) {
String line = sc.nextLine();
String var[] = line.split(",");
if (!page_freq.containsKey(var[3])) {
page_freq.put(var[3], 1);
} else {
int val = page_freq.get(var[3]);
val = val + 1;
page_freq.replace(var[3], val);
}
if (!ip_address.containsKey(var[0])) {
ip_address.put(var[0], i);
i++;
int index = ip_address.get(var[0]);
int val = no_of_pages.get(index);
val++;
no_of_pages.replace(index, val);
index = index - 1;
sessions[index].add(var[3]);
} else {
int index = ip_address.get(var[0]);
int val = no_of_pages.get(index);
val++;
no_of_pages.replace(index, val);
index = index - 1;
sessions[index].add(var[3]);
}
}
int n = ip_address.size();
for (int x = 0; x < n; x++) {
int j ;
System.out.print("Session"+(x+1)+": ");
for (j = 0; j < sessions[x].size()-1; j++) {
System.out.print(sessions[x].get(j) + " -> ");
}
System.out.print(sessions[x].get(j));
System.out.println();
}
// FREQUENCY CALCULATION
System.out.println();
System.out.println();
System.out.println("FREQUENCY OF EACH PAGE:");
System.out.println(page_freq);
for (String name : page_freq.keySet()) {
int f = page_freq.get(name);
if (f < 3) {
list.add(name);
}
}
System.out.println();
System.out.println();
System.out.println("DELETED PAGES");
System.out.println(list);
// REMOVING THE PAGES WHOSE FREQ < 3
for (int x = 0; x < n; x++) {
for(int j=0;j < list.size();j++) {
String val = list.get(j);
if(sessions[x].contains(val)) {
sessions[x].remove(val);
}
}
}
HashMap<String, Integer> list_of_new_pages = new HashMap<String, Integer>();
// PRINTING AFTER DELETION
int count = 0;
System.out.println();
System.out.println();
System.out.println("NEW SESSIONS AFTER DELETING PAGES");
for (int x = 0; x < n; x++) {
int j;
for (j = 0; j < sessions[x].size()-1; j++) {
String a = sessions[x].get(j);
if (!list_of_new_pages.containsKey(a)) {
list_of_new_pages.put(a, count);
count = count + 1;
}
System.out.print(sessions[x].get(j) + " -> ");
}
String a = sessions[x].get(j);
if (!list_of_new_pages.containsKey(a)) {
list_of_new_pages.put(a, count);
count = count + 1;
}
System.out.print(sessions[x].get(j));
System.out.println();
}
int size = list_of_new_pages.size();
int count2 = 0;
String page_array[] = new String[size];
for(String op : list_of_new_pages.keySet()) {
list_of_new_pages.replace(op, count2);
page_array[count2]=op;
count2++;
}
System.out.println();
int arr[][] = new int[size][size];
for (int z = 0; z < size; z++) {
for (int y = 0; y < size; y++) {
arr[z][y] = 0;
}
}
for (int x = 0; x < n; x++) {
for (int j = 0; j < sessions[x].size()-1; j++) {
String a = sessions[x].get(j);
String b = sessions[x].get(j + 1);
arr[list_of_new_pages.get(a)][ list_of_new_pages.get(b)]++;
}
System.out.println();
}
System.out.println("Matrix Created Using 2nd order Markov Model: ");
System.out.println();
System.out.println();
System.out.print("\t");
for (int in = 0; in < size; in++) {
System.out.print(page_array[in]+"\t");
}
System.out.println();
for (int z = 0; z < size; z++) {
System.out.print(page_array[z]+"\t");
for (int y = 0; y < size; y++) {
System.out.print(arr[z][y]+"\t");
}
System.out.println();
}
int max=0;
int pos1=-1,pos2=-1;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
int val=arr[x][y];
if(val>max)
{
max=val;
pos1=x;
pos2=y;
}
}
}
String src=page_array[pos1];
String dest=page_array[pos2];
System.out.println();
System.out.println();
System.out.println("Using Markov 2nd order, most frequently accessed pages are:");
System.out.print(src+"->");
System.out.println(dest);
int sd_cnt[]=new int[n];
for(int x=0;x<n;x++)
{
int cntr=0;
if(sessions[x].contains(src) && sessions[x].contains(dest))
{
for(int y=0;y<sessions[x].size();y++)
{
String val=sessions[x].get(y);
if(val.equals(src))
{
if(y+1<sessions[x].size())
{
val=sessions[x].get(y+1);
if(val.equals(dest))
{
cntr++;
sd_cnt[x]=cntr;
}
}
}
}
}
}
int sum=0;
for(int x=0;x<n;x++)
{
sum=sum+sd_cnt[x];
}
ArrayList<String>[] seq = new ArrayList[sum];
for (int x = 0; x < sum; x++) {
seq[x] = new ArrayList<String>();
}
int cnt=0;
for(int x=0;x<n;x++)
{
if(sd_cnt[x]>0)
{
for(int y=0;y<sessions[x].size();y++)
{
if(sd_cnt[x]>0)
{
String val=sessions[x].get(y);
if(val.equals(src))
{
if(y+2<sessions[x].size())
{
val=sessions[x].get(y+1);
if(val.equals(dest))
{
sd_cnt[x]--;
y=y+2;
val=sessions[x].get(y);
seq[cnt].add(val);
cnt++;
}
else
{
val=sessions[x].get(y);
seq[cnt].add(val);
}
}
}
else
{
seq[cnt].add(val);
}
}
}
}
}
for(int x=0;x<cnt;x++)
{
int y;
for(y=0;y<seq[x].size()-1;y++)
{
System.out.print(seq[x].get(y)+"\t");
}
System.out.print(src+""+dest+"\t"+seq[x].get(y));
System.out.println();
}
HashMap<String, Integer> pre = new HashMap<>();
HashMap<String, Integer> post = new HashMap<>();
for(int x=0;x<cnt;x++)
{ int y;
for(y=0;y<seq[x].size()-1;y++)
{
String val=seq[x].get(y);
if(!pre.containsKey(val))
{
pre.put(val, 1);
}
else
{
int c=pre.get(val);
c++;
pre.replace(val, c);
}
}
String val=seq[x].get(y);
if(!post.containsKey(val))
{
post.put(val, 1);
}
else
{
int c=post.get(val);
c++;
post.replace(val, c);
}
}
int s1=pre.size();
int s2=post.size();
int calc[][]=new int [s2][s1];
int counter1=0,counter=0;
for(String op : post.keySet()) {
counter=0;
for(String oq: pre.keySet())
{
int cf=0;
for(int x=0;x<cnt;x++)
{
int index=seq[x].size()-1;
String val=seq[x].get(index);
if(seq[x].contains(op) && val.equals(op) && seq[x].contains(oq) && !op.equals(oq))
{
cf++;
}
}
calc[counter1][counter]=cf;
counter++;
}
counter1++;
}
int copy[]=new int[s1];
int counters=0;
for(String op : pre.keySet())
{
int val=pre.get(op);
copy[counters]=val;
counters++;
}
int perc[][]=new int[s2][s1];
for(int x=0;x<s2;x++)
{
for(int y=0;y<s1;y++)
{
perc[x][y]=(calc[x][y]*100)/copy[y];
}
}
System.out.println();
System.out.println();
counter1=0;
for(String op : post.keySet()) {
System.out.println("Confidence of accessing page "+op+" using subsequence association rules:");
System.out.println();
counter=0;
for(String oq: pre.keySet())
{
System.out.println(oq+"->"+op+"\t"+oq+""+op+"/"+oq+"\t"+calc[counter1][counter]+"/"+copy[counter]+"\t"+perc[counter1][counter]+"%");
counter++;
}
counter1++;
System.out.println();
}
System.out.println("If the user accesses page G before pages E and F then there is a 100% confidence that the user will access page H next. Whereas, if the user visits page H before visiting pages E and F, then there is a 100% confidence that the user will access page I next.");
sc.close();
}
}