@@ -11,8 +11,9 @@ class FPGrowth
1111 protected int $ support = 3 ;
1212 protected float $ confidence = 0.7 ;
1313 private int $ maxLength = 0 ;
14-
14+ private $ itemsetSeparator ;
1515 private $ patterns ;
16+
1617 private $ rules ;
1718
1819 /**
@@ -81,11 +82,12 @@ public function getRules()
8182 * @param int $support 1, 2, 3 ...
8283 * @param float $confidence 0 ... 1
8384 */
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" )
8586 {
8687 $ this ->setSupport ($ support );
8788 $ this ->setConfidence ($ confidence );
8889 $ this ->setMaxLength ($ maxLength );
90+ $ this ->itemsetSeparator = $ itemsetSeparator ;
8991 }
9092
9193 /**
@@ -104,7 +106,7 @@ public function run(array $transactions)
104106 */
105107 protected function findFrequentPatterns (array $ transactions ): array
106108 {
107- $ tree = new FPTree ($ transactions , $ this ->support , null , 0 , $ this ->maxLength );
109+ $ tree = new FPTree ($ transactions , $ this ->support , null , 0 , $ this ->maxLength , $ this -> itemsetSeparator );
108110 return $ tree ->minePatterns ($ this ->support );
109111 }
110112
@@ -116,16 +118,16 @@ protected function generateAssociationRules(array $patterns): array
116118 {
117119 $ rules = [];
118120 foreach (array_keys ($ patterns ) as $ pattern ) {
119- $ itemSet = explode (' , ' , $ pattern );
121+ $ itemSet = explode ($ this -> itemsetSeparator , $ pattern );
120122 $ upperSupport = $ patterns [$ pattern ];
121123 for ($ i = 1 ; $ i < count ($ itemSet ); $ i ++) {
122124 $ combinations = new Combinations ($ itemSet , $ i );
123125 foreach ($ combinations ->generator () as $ antecedent ) {
124126 sort ($ antecedent );
125- $ antecedentStr = implode (' , ' , $ antecedent );
127+ $ antecedentStr = implode ($ this -> itemsetSeparator , $ antecedent );
126128 $ consequent = array_diff ($ itemSet , $ antecedent );
127129 sort ($ consequent );
128- $ consequentStr = implode (' , ' , $ consequent );
130+ $ consequentStr = implode ($ this -> itemsetSeparator , $ consequent );
129131 if (isset ($ patterns [$ antecedentStr ])) {
130132 $ lowerSupport = $ patterns [$ antecedentStr ];
131133 $ confidence = floatval ($ upperSupport ) / $ lowerSupport ;
0 commit comments