1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1);
|
4
|
|
|
|
5
|
|
|
namespace PhpScience\PageRank\Service;
|
6
|
|
|
|
7
|
|
|
use PhpScience\PageRank\Data\NodeCollectionInterface;
|
8
|
|
|
use PhpScience\PageRank\Service\PageRankAlgorithm\NormalizerInterface;
|
9
|
|
|
use PhpScience\PageRank\Service\PageRankAlgorithm\RankingInterface;
|
10
|
|
|
use PhpScience\PageRank\Strategy\NodeDataSourceStrategyInterface;
|
11
|
|
|
|
12
|
1 |
|
class PageRankAlgorithm implements PageRankAlgorithmInterface
|
13
|
|
|
{
|
14
|
|
|
private NodeDataSourceStrategyInterface $nodeDataStrategy;
|
15
|
|
|
private RankingInterface $ranking;
|
16
|
|
|
private NormalizerInterface $normalizer;
|
17
|
|
|
|
18
|
2 |
|
public function __construct(
|
19
|
|
|
RankingInterface $ranking,
|
20
|
|
|
NodeDataSourceStrategyInterface $nodeDataStrategy,
|
21
|
|
|
NormalizerInterface $normalizer
|
22
|
|
|
) {
|
23
|
2 |
|
$this->nodeDataStrategy = $nodeDataStrategy;
|
24
|
2 |
|
$this->ranking = $ranking;
|
25
|
2 |
|
$this->normalizer = $normalizer;
|
26
|
2 |
|
}
|
27
|
|
|
|
28
|
1 |
|
public function run(int $maxIterate): NodeCollectionInterface
|
29
|
|
|
{
|
30
|
1 |
|
$this->initiateRanking();
|
31
|
1 |
|
$this->runBatch($maxIterate);
|
32
|
|
|
|
33
|
1 |
|
return $this->normalize();
|
34
|
|
|
}
|
35
|
|
|
|
36
|
2 |
|
public function initiateRanking(): NodeCollectionInterface
|
37
|
|
|
{
|
38
|
2 |
|
$nodeCollection = $this->nodeDataStrategy->getNodeCollection();
|
39
|
|
|
|
40
|
2 |
|
$this->ranking->calculateInitialRank($nodeCollection);
|
41
|
2 |
|
$this->nodeDataStrategy->updateNodes($nodeCollection);
|
42
|
|
|
|
43
|
2 |
|
return $nodeCollection;
|
44
|
|
|
}
|
45
|
|
|
|
46
|
2 |
|
public function runBatch(int $maxIterate): NodeCollectionInterface
|
47
|
|
|
{
|
48
|
2 |
|
$nodeCollection = $this->nodeDataStrategy->getNodeCollection();
|
49
|
|
|
|
50
|
2 |
|
$this->powerIterate($nodeCollection, $maxIterate);
|
51
|
|
|
|
52
|
2 |
|
return $nodeCollection;
|
53
|
|
|
}
|
54
|
|
|
|
55
|
1 |
|
public function normalize(): NodeCollectionInterface
|
56
|
|
|
{
|
57
|
1 |
|
$nodeCollection = $this->nodeDataStrategy->getNodeCollection();
|
58
|
1 |
|
$min = $this->nodeDataStrategy->getLowestRank();
|
59
|
1 |
|
$max = $this->nodeDataStrategy->getHighestRank();
|
60
|
|
|
|
61
|
1 |
|
$this->normalizer->normalize(
|
62
|
1 |
|
$nodeCollection,
|
63
|
|
|
$min,
|
64
|
|
|
$max
|
65
|
|
|
);
|
66
|
|
|
|
67
|
1 |
|
return $nodeCollection;
|
68
|
|
|
}
|
69
|
|
|
|
70
|
2 |
|
private function powerIterate(
|
71
|
|
|
NodeCollectionInterface $nodeCollection,
|
72
|
|
|
int $maxIterate
|
73
|
|
|
): void {
|
74
|
2 |
|
$noneRepresentableDiffCount = 0;
|
75
|
2 |
|
$i = 0;
|
76
|
|
|
|
77
|
|
|
while (
|
78
|
2 |
|
$i < $maxIterate
|
79
|
2 |
|
&& $noneRepresentableDiffCount < $nodeCollection->getAllNodeCount()
|
80
|
|
|
) {
|
81
|
2 |
|
$i++;
|
82
|
|
|
|
83
|
|
|
$noneRepresentableDiffCount = $this
|
84
|
2 |
|
->ranking
|
85
|
2 |
|
->calculateRankPerIteration($nodeCollection);
|
86
|
|
|
|
87
|
2 |
|
$this->nodeDataStrategy->updateNodes($nodeCollection);
|
88
|
|
|
}
|
89
|
2 |
|
}
|
90
|
|
|
}
|
91
|
|
|
|