Skip to content

Commit 92169c5

Browse files
authored
Merge pull request #8 from integer-net/separator
Change separator for itemsets
2 parents 4027837 + bf7a505 commit 92169c5

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/FPGrowth.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -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;

src/FPTree.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class FPTree
1818

1919
private int $maxLength = 0;
2020

21-
private int $depth = 0;
21+
private string $itemsetSeparator;
22+
23+
private int $depth = 0;
2224

2325
/**
2426
* Initialize the tree.
@@ -27,12 +29,13 @@ class FPTree
2729
* @param $rootValue
2830
* @param int $rootCount
2931
*/
30-
public function __construct(array $transactions, int $threshold, $rootValue, int $rootCount, $maxLength = 0)
32+
public function __construct(array $transactions, int $threshold, $rootValue, int $rootCount, $maxLength = 0, string $itemsetSeparator = "\0")
3133
{
3234
$this->frequent = $this->findFrequentItems($transactions, $threshold);
3335
$this->headers = $this->buildHeaderTable();
3436
$this->root = $this->buildFPTree($transactions, $rootValue, $rootCount, $this->frequent);
3537
$this->maxLength = $maxLength;
38+
$this->itemsetSeparator = $itemsetSeparator;
3639
}
3740

3841
/**
@@ -195,10 +198,10 @@ protected function zipPatterns(array $patterns): array
195198
// We are in a conditional tree.
196199
$newPatterns = [];
197200
foreach (array_keys($patterns) as $strKey) {
198-
$key = explode(',', $strKey);
201+
$key = explode($this->itemsetSeparator, $strKey);
199202
$key[] = $this->root->value;
200203
sort($key);
201-
$newPatterns[implode(',', $key)] = $patterns[$strKey];
204+
$newPatterns[implode($this->itemsetSeparator, $key)] = $patterns[$strKey];
202205
}
203206

204207
return $newPatterns;
@@ -236,7 +239,7 @@ protected function generatePatternList(): array
236239
$min = $this->frequent[$x];
237240
}
238241
}
239-
$patterns[implode(',', $pattern)] = $min;
242+
$patterns[implode($this->itemsetSeparator, $pattern)] = $min;
240243
}
241244
}
242245

0 commit comments

Comments
 (0)