-
Notifications
You must be signed in to change notification settings - Fork 15
/
apriori.min.js
1 lines (1 loc) · 4.03 KB
/
apriori.min.js
1
!function(a,b){"object"==typeof exports?module.exports=b():"function"==typeof define&&define.amd?define("apriori",[],b):a.Apriori=b()}(this,function(){"use strict";var a;return function(a){var b=function(){function a(a,b){this.frequentItemSets=a,this.associationRules=b}return a}();a.AnalysisResult=b;var c=function(){function a(a,b){this.itemSet=a,this.support=b}return a}();a.FrequentItemSet=c;var d=function(){function a(a,b,c){this.lhs=a,this.rhs=b,this.confidence=c}return a}();a.AssociationRule=d;var e=function(){function d(a,b,c){this.minSupport=a||.15,this.minConfidence=b||.6,this.debugMode=c||!1}return d.prototype.analyze=function(c){var d=this,e=(new Date).getTime(),g={},h={},i=d.toOneElementItemSets(c),j=d.findItemSetsMinSupportSatisfied(i,c,d.minSupport,g),k=j,l=1;d.debugMode&&console.log("Before finding item sets: "+d.getTime(e)+" ms");for(var m=function(a){return a.itemSet};0!==k.length;){h[l]=k;var n=f.toFixedSizeJoinedSets(k.map(m),l+1);k=d.findItemSetsMinSupportSatisfied(n,c,d.minSupport,g),l+=1}d.debugMode&&console.log("After finding item sets: "+d.getTime(e)+" ms");var o=function(a,b,c){var d=b[a.toString()];return d?d/c.length:0},p=[],q=function(a){var b=!1;return p.forEach(function(c){b||(b=c.toString()===a.toString())}),b};d.debugMode&&console.log("Before calculating association rules: "+d.getTime(e)+" ms");var r,s=[],t=function(b){var e=f.getDiffArray(r,b);if(e.length>0){var h=o(r,g,c),i=o(b,g,c),j=h/i;!isNaN(j)&&!q(b)&&j>=d.minConfidence&&(p.push(b),s.push(new a.AssociationRule(b,e,j)))}},u=function(a){r=a,f.toAllSubSets(r).forEach(t)};for(var v in h){var w=h[v].map(m);0===w.length||w[0].length<=1||w.forEach(u)}d.debugMode&&console.log("After calculating association rules: "+d.getTime(e)+" ms");var x=new b(h,s);return d.debugMode&&(console.log("AnalysisResult: "+JSON.stringify(x)),console.log("Apriori.Algorithm's total spent time: "+d.getTime(e)+" ms")),x},d.prototype.toOneElementItemSets=function(a){var b=[];return a.forEach(function(a){a.forEach(function(a){b.push(new Array(a))})}),f.toArraySet(b)},d.prototype.findItemSetsMinSupportSatisfied=function(a,b,d,e){var g=[],h={};a.forEach(function(a){b.forEach(function(b){f.isSubSetArrayOf(a,b)&&(e[a.toString()]||(e[a.toString()]=0),h[a.toString()]||(h[a.toString()]=0),e[a.toString()]+=1,h[a.toString()]+=1)})});var i=!1,j=function(a){i||(i=a.itemSet.toString()===l.toString())};for(var k in h){var l=k.split(",").sort(),m=h[l.toString()],n=m/b.length;n>=d&&(i=!1,g.forEach(j),i||g.push(new c(l,n)))}return g},d.prototype.showAnalysisResultFromFile=function(a){var b=this;require("fs").readFile(a,"utf8",function(a,c){if(a)throw a;var d=f.readCSVToArray(c,","),e=b.analyze(d);console.log(JSON.stringify(e.associationRules))})},d.prototype.getTime=function(a){return(new Date).getTime()-a},d}();a.Algorithm=e;var f=function(){function a(){}return a.toStringSet=function(a){var b=[];return a.forEach(function(a){-1===b.indexOf(a)&&b.push(a)}),b},a.toArraySet=function(a){var b={},c=[];return a.forEach(function(a){b.hasOwnProperty(a.toString())||(c.push(a),b[a.toString()]=!0)}),c},a.toAllSubSets=function(b){var c=function(b,d,e,f){if(0===b)e.length>0&&(f[f.length]=a.toStringSet(e));else for(var g=0;g<d.length;g++){var h=b-1,i=d.slice(g+1),j=e.concat([d[g]]);c(h,i,j,f)}},d=[];b.sort();for(var e=1;e<b.length;e++)c(e,b,[],d);return d.push(b),a.toArraySet(d)},a.toFixedSizeJoinedSets=function(b,c){var d=[];return b.forEach(function(e){b.forEach(function(b){if(a.getDiffArray(e,b).length>0){var f=[].concat(e).concat(b),g=a.toStringSet(f);g.length===c&&d.push(g)}})}),a.toArraySet(d)},a.isSubSetArrayOf=function(a,b){var c=!0;return a.forEach(function(a){c&&-1===b.indexOf(a)&&(c=!1)}),c},a.getDiffArray=function(a,b){var c=[];return a.forEach(function(a){-1===b.indexOf(a)&&c.push(a)}),c},a.readCSVToArray=function(a,b){b=b||",";for(var c,d=new RegExp("(\\"+b+'|\\r?\\n|\\r|^)(?:"([^"]*(?:""[^"]*)*)"|([^"\\'+b+"\\r\\n]*))","gi"),e=[[]];c=d.exec(a);){var f=c[1];f.length&&f!==b&&e.push([]);var g=c[2]?c[2].replace(new RegExp('""',"g"),'"'):c[3];g.length>0&&e[e.length-1].push(g)}return e},a}();a.ArrayUtils=f}(a||(a={})),a});