Skip to content

Commit 4027837

Browse files
authored
Merge pull request #7 from integer-net/max-len
Add configuration to limit length of rules
2 parents 6d9b3d0 + 3aaa88c commit 4027837

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/FPGrowth.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class FPGrowth
1010
{
1111
protected int $support = 3;
1212
protected float $confidence = 0.7;
13+
private int $maxLength = 0;
1314

1415
private $patterns;
1516
private $rules;
@@ -50,6 +51,15 @@ public function setConfidence(float $confidence): self
5051
return $this;
5152
}
5253

54+
public function getMaxLength(): int
55+
{
56+
return $this->maxLength;
57+
}
58+
59+
public function setMaxLength(int $maxLength): void
60+
{
61+
$this->maxLength = $maxLength;
62+
}
5363
/**
5464
* @return mixed
5565
*/
@@ -71,10 +81,11 @@ public function getRules()
7181
* @param int $support 1, 2, 3 ...
7282
* @param float $confidence 0 ... 1
7383
*/
74-
public function __construct(int $support, float $confidence)
84+
public function __construct(int $support, float $confidence, int $maxLength = 0)
7585
{
7686
$this->setSupport($support);
7787
$this->setConfidence($confidence);
88+
$this->setMaxLength($maxLength);
7889
}
7990

8091
/**
@@ -93,7 +104,7 @@ public function run(array $transactions)
93104
*/
94105
protected function findFrequentPatterns(array $transactions): array
95106
{
96-
$tree = new FPTree($transactions, $this->support, null, 0);
107+
$tree = new FPTree($transactions, $this->support, null, 0, $this->maxLength);
97108
return $tree->minePatterns($this->support);
98109
}
99110

src/FPTree.php

+22-3
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,23 @@ class FPTree
1616

1717
private FPNode $root;
1818

19+
private int $maxLength = 0;
20+
21+
private int $depth = 0;
22+
1923
/**
2024
* Initialize the tree.
2125
* @param array $transactions
2226
* @param int $threshold
2327
* @param $rootValue
2428
* @param int $rootCount
2529
*/
26-
public function __construct(array $transactions, int $threshold, $rootValue, int $rootCount)
30+
public function __construct(array $transactions, int $threshold, $rootValue, int $rootCount, $maxLength = 0)
2731
{
2832
$this->frequent = $this->findFrequentItems($transactions, $threshold);
2933
$this->headers = $this->buildHeaderTable();
3034
$this->root = $this->buildFPTree($transactions, $rootValue, $rootCount, $this->frequent);
35+
$this->maxLength = $maxLength;
3136
}
3237

3338
/**
@@ -168,6 +173,8 @@ public function minePatterns(int $threshold): array
168173
{
169174
if ($this->treeHasSinglePath($this->root)) {
170175
return $this->generatePatternList();
176+
} elseif ($this->maxLength && $this->maxLength <= $this->getDepth()) {
177+
return [];
171178
}
172179

173180
return $this->zipPatterns($this->mineSubTrees($threshold));
@@ -211,7 +218,13 @@ protected function generatePatternList(): array
211218
$patterns[$this->root->value] = $this->root->count;
212219
}
213220

214-
for ($i = 1; $i <= count($items); $i++) {
221+
// limit length of combinations to remaining length
222+
$count = count($items);
223+
if ($this->maxLength) {
224+
$count = min($count, $this->maxLength - $this->getDepth());
225+
}
226+
227+
for ($i = 1; $i <= $count; $i++) {
215228
$combinations = new Combinations($items,$i);
216229
foreach ($combinations->generator() as $subset) {
217230
$pattern = $this->root->value !== null ? array_merge($subset, [$this->root->value]) : $subset;
@@ -270,7 +283,8 @@ protected function mineSubTrees(int $threshold): array
270283
}
271284

272285
// Now we have the input for a subtree, so construct it and grab the patterns.
273-
$subtree = new FPTree($conditionalTreeInput, $threshold, $item, $this->frequent[$item]);
286+
$subtree = new FPTree($conditionalTreeInput, $threshold, $item, $this->frequent[$item], $this->maxLength);
287+
$subtree->depth = $this->depth + 1;
274288
$subtreePatterns = $subtree->minePatterns($threshold);
275289

276290
// Insert subtree patterns into main patterns dictionary.
@@ -285,4 +299,9 @@ protected function mineSubTrees(int $threshold): array
285299

286300
return $patterns;
287301
}
302+
303+
private function getDepth(): int
304+
{
305+
return $this->depth;
306+
}
288307
}

0 commit comments

Comments
 (0)