-
Notifications
You must be signed in to change notification settings - Fork 0
/
aprori.js
127 lines (112 loc) · 3.08 KB
/
aprori.js
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
//{tra:[],sup:number,con:number}
var _c = require('js-combinatorics');
class item{
constructor(name){
this.name = name;
this.freq = 0;
};
get occ(){
return this.freq;
}
incFreq(){
this.freq++;
}
}
module.exports = function(tObj){
if(!tObj.tra || !tObj.sup || !tObj.con) return new Error("invalid input");
else{
var obj = tObj;
var sup = parseInt(obj.sup);
var con = parseInt(obj.con);
var nTra = (obj.tra.length);
var itemSet = [];
var track = [];
var mapData = [];
var c = 0;
//calc supnu
var nSup = (sup/100)*nTra;
//item set
obj.tra.map((item) => {
item.items.map((o) => {
if(track.indexOf(o) + 1){
itemSet.forEach((i) => {
if(i.name == o) i.freq++;
})
}else{
track.push(o);
itemSet.push({name:o,freq:1});
}
})
})
itemSet.forEach((i) => {
if(i.freq < nSup){
itemSet.splice(itemSet.indexOf(i));
track.splice(track.indexOf(i.name));
}
})
// console.log("\n\nitemSet\n");
// console.log(itemSet);
// console.log(track)
mapData.push(
{
c:c++,
items:itemSet
}
)
let upc = 2;
var replicaItemSet = itemSet;
while((replicaItemSet.length - 1) != 0){
var tempItemSet = getComb(track, upc++);
var newItemSet = [];
tempItemSet.forEach((s) => {
let data = {
name:s,
freq:0
};
obj.tra.map((item) => {
if(
data.name.every((e) => {return item.items.indexOf(e) >= 0})
) data.freq++;
});
if(data.freq < nSup) ;
else newItemSet.push(data);
});
//console.log(newItemSet);
mapData.push({
c:c++,
items:newItemSet
})
replicaItemSet = newItemSet;
}
// console.log("\n\nmapdata:\n");
// console.log(mapData);
var confidence = [];
replicaItemSet[0].name.forEach((e) => {
itemSet.forEach((o) => {
if(o.name === e){
let cCon = (replicaItemSet.freq/o.freq)*100;
if(cCon < con) ;
else confidence.push(o.name);
}
})
})
//ret json
return {
sup:sup,
con:con,
nTra:nTra,
nSup:nSup,
stack:mapData,
confidenceRule:{
set:replicaItemSet[0].name,
cItems:confidence
}
};
}
}
function getComb(arr, cmb){
let x = [];
cmb = _c.combination(arr, cmb);
while(a = cmb.next()) x.push(a);
return x;
}