@@ -11,8 +11,9 @@ class FPGrowth
11
11
protected int $ support = 3 ;
12
12
protected float $ confidence = 0.7 ;
13
13
private int $ maxLength = 0 ;
14
-
14
+ private $ itemsetSeparator ;
15
15
private $ patterns ;
16
+
16
17
private $ rules ;
17
18
18
19
/**
@@ -81,11 +82,12 @@ public function getRules()
81
82
* @param int $support 1, 2, 3 ...
82
83
* @param float $confidence 0 ... 1
83
84
*/
84
- public function __construct (int $ support , float $ confidence , int $ maxLength = 0 )
85
+ public function __construct (int $ support , float $ confidence , int $ maxLength = 0 , string $ itemsetSeparator = "\0" )
85
86
{
86
87
$ this ->setSupport ($ support );
87
88
$ this ->setConfidence ($ confidence );
88
89
$ this ->setMaxLength ($ maxLength );
90
+ $ this ->itemsetSeparator = $ itemsetSeparator ;
89
91
}
90
92
91
93
/**
@@ -104,7 +106,7 @@ public function run(array $transactions)
104
106
*/
105
107
protected function findFrequentPatterns (array $ transactions ): array
106
108
{
107
- $ tree = new FPTree ($ transactions , $ this ->support , null , 0 , $ this ->maxLength );
109
+ $ tree = new FPTree ($ transactions , $ this ->support , null , 0 , $ this ->maxLength , $ this -> itemsetSeparator );
108
110
return $ tree ->minePatterns ($ this ->support );
109
111
}
110
112
@@ -116,16 +118,16 @@ protected function generateAssociationRules(array $patterns): array
116
118
{
117
119
$ rules = [];
118
120
foreach (array_keys ($ patterns ) as $ pattern ) {
119
- $ itemSet = explode (' , ' , $ pattern );
121
+ $ itemSet = explode ($ this -> itemsetSeparator , $ pattern );
120
122
$ upperSupport = $ patterns [$ pattern ];
121
123
for ($ i = 1 ; $ i < count ($ itemSet ); $ i ++) {
122
124
$ combinations = new Combinations ($ itemSet , $ i );
123
125
foreach ($ combinations ->generator () as $ antecedent ) {
124
126
sort ($ antecedent );
125
- $ antecedentStr = implode (' , ' , $ antecedent );
127
+ $ antecedentStr = implode ($ this -> itemsetSeparator , $ antecedent );
126
128
$ consequent = array_diff ($ itemSet , $ antecedent );
127
129
sort ($ consequent );
128
- $ consequentStr = implode (' , ' , $ consequent );
130
+ $ consequentStr = implode ($ this -> itemsetSeparator , $ consequent );
129
131
if (isset ($ patterns [$ antecedentStr ])) {
130
132
$ lowerSupport = $ patterns [$ antecedentStr ];
131
133
$ confidence = floatval ($ upperSupport ) / $ lowerSupport ;
0 commit comments