forked from sudheesh001/OS-CS302
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgo.java
160 lines (128 loc) · 5.63 KB
/
Algo.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Set;
import datastructures.INode;
public abstract class Algo {
String filePathFormat;
int fileStartIndex;
int fileEndIndex;
int numOfItems;
int numOfBaskets;
INode root;
protected int k;
protected long basketIndex;
protected boolean foundSomething;
protected int minK;
protected int maxK;
Set<String> alphabet;
/***
* One line of comma separated values = one basket of items. Line starting with "#" are treated as comments and thus
* ignored. All the baskets are placed in several files according to the path from start to end. Comparison is case
* insensitive!
*/
public Algo(String _filePathFormat, int _fileStartIndex, int _fileEndIndex, Set<String> _alphabet) {
filePathFormat = _filePathFormat;
fileStartIndex = _fileStartIndex;
fileEndIndex = _fileEndIndex;
alphabet = _alphabet;
}
public Algo(String _filePathFormat, int _fileStartIndex, int _fileEndIndex, String _alphabetFilePath)
throws Exception {
this(_filePathFormat, _fileStartIndex, _fileEndIndex, Common.loadAlphabetSetFromFile(_alphabetFilePath));
}
public Algo(String _filePathFormat, int _fileStartIndex, int _fileEndIndex, double support) throws Exception {
this(_filePathFormat, _fileStartIndex, _fileEndIndex, Common.loadAlphabetSetFromInput(_filePathFormat,
_fileStartIndex, _fileEndIndex, support));
}
public Algo(String _filePathFormat, int _fileStartIndex, int _fileEndIndex) throws Exception {
this(_filePathFormat, _fileStartIndex, _fileEndIndex, 0.0);
}
/***
* Return the most frequent itemsets which are more frequent than the support and has more items (of type T) than
* minimumK.
*
* @param support
* - min frequency to consider items set frequent.
* @param minimumK
* - min size of itemset to consider it interesting.
* @return Map - for each K we keep the frequent K-itemsets.
* @throws Exception
*/
public INode findFrequentItemSet(double support, int minK, int maxK) throws Exception {
BufferedReader br;
String line;
basketIndex = 0;
iterNum = 0;
k = 1;
this.maxK = maxK;
this.minK = minK;
init();
while (true) {
startOfSingleIteration();
for (int i = fileStartIndex; i <= fileEndIndex; i++) {
br = new BufferedReader(new FileReader(String.format(filePathFormat, i)));
while ((line = br.readLine()) != null) {
if (line.startsWith("#") || line.equals("")) {
// Ignore comments
continue;
}
basketIndex++;
if (!processSingleLine(line, support)) {
return root;
}
}
br.close();
}
if (!endOfSingleIteration(support, basketIndex, foundSomething)) {
break;
}
}
return root;
}
protected abstract void init();
protected abstract void startOfSingleIteration();
protected abstract boolean processSingleLine(String line, double support);
protected abstract boolean endOfSingleIteration(double support, long basketIndex, boolean foundSomething);
public int getNumOfItems() {
return numOfItems;
}
public int getNumOfBaskets() {
return numOfBaskets;
}
// STATS:
private static class StatsHolder {
int count;
}
private static HashMap<Integer, StatsHolder> levelToApperneces = new HashMap<Integer, StatsHolder>();
private static int statsMaxK = 0;
public static int iterNum = 0;
public static synchronized void incStats(int k) {
if (levelToApperneces.containsKey(k)) {
levelToApperneces.get(k).count++;
} else {
if (k > statsMaxK)
statsMaxK = k;
StatsHolder tmp = new StatsHolder();
tmp.count = 1;
levelToApperneces.put(k, tmp);
}
}
public static String getStats() {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= statsMaxK; i++) {
sb.append(String.format(" level - %d@items - %d\n", i, levelToApperneces.get(i).count));
}
return sb.toString();
}
public Set<String> getAlphabet() {
return alphabet;
}
public void setAlphabet(Set<String> alphabet) {
this.alphabet = alphabet;
}
@Override
public String toString() {
return this.getClass().getSimpleName();
}
}