-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
381cdb9
commit abd5acb
Showing
12 changed files
with
423 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpScience\PageRank\Service\PageRankAlgorithm; | ||
|
||
use PhpScience\PageRank\Data\NodeCollectionInterface; | ||
|
||
class Normalizer implements NormalizerInterface | ||
{ | ||
private float $scaleBottom; | ||
private float $scaleTop; | ||
|
||
public function __construct( | ||
float $scaleBottom = 1, | ||
float $scaleTop = 10 | ||
) { | ||
$this->scaleBottom = $scaleBottom; | ||
$this->scaleTop = $scaleTop; | ||
} | ||
|
||
public function normalize( | ||
NodeCollectionInterface $nodeCollection, | ||
float $lowestRank, | ||
float $highestRank | ||
): void { | ||
$divider = $this->getDivider($lowestRank, $highestRank); | ||
|
||
foreach ($nodeCollection->getNodes() as $node) { | ||
$rank = $this->getScaledRank( | ||
$node->getRank(), | ||
$lowestRank, | ||
$divider | ||
); | ||
$node->setRank($rank); | ||
} | ||
} | ||
|
||
private function getDivider(float $lowestRank, float $highestRank): float | ||
{ | ||
$divider = $highestRank - $lowestRank; | ||
|
||
if (.0 === $divider) { | ||
$divider = 1; | ||
} | ||
|
||
return $divider; | ||
} | ||
|
||
private function getScaledRank( | ||
float $value, | ||
float $lowestRank, | ||
float $divider | ||
): float { | ||
$normalized = ($value - $lowestRank) / $divider; | ||
$multiplier = $this->scaleTop - $this->scaleBottom; | ||
|
||
return ($normalized * $multiplier) + $this->scaleBottom; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpScience\PageRank\Builder; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class NodeBuilderTest extends TestCase | ||
{ | ||
private NodeBuilder $nodeBuilder; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->nodeBuilder = new NodeBuilder(); | ||
} | ||
|
||
public function testBuild(): void | ||
{ | ||
$expected = 0.25; | ||
|
||
$data = [ | ||
'id' => 1, | ||
'rank' => $expected | ||
]; | ||
|
||
$actual = $this->nodeBuilder->build($data); | ||
|
||
static::assertSame($expected, $actual->getRank()); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
tests/unit/Service/PageRankAlgorithm/NormalizerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpScience\PageRank\Service\PageRankAlgorithm; | ||
|
||
use PhpScience\PageRank\Data\NodeCollectionInterface; | ||
use PhpScience\PageRank\Data\NodeInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class NormalizerTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider dataProviderNormalize | ||
* | ||
* @param float $originalRank | ||
* @param float $scaleBottom | ||
* @param float $scaleTop | ||
* @param float $lowestRank | ||
* @param float $highestRank | ||
* @param float $expectedRank | ||
*/ | ||
public function testNormalize( | ||
float $originalRank, | ||
float $scaleBottom, | ||
float $scaleTop, | ||
float $lowestRank, | ||
float $highestRank, | ||
float $expectedRank | ||
): void { | ||
$nodeCollection = $this | ||
->createMock(NodeCollectionInterface::class); | ||
$node = $this | ||
->createMock(NodeInterface::class); | ||
|
||
$nodeCollection | ||
->expects($this->once()) | ||
->method('getNodes') | ||
->willReturn([$node]); | ||
|
||
$node | ||
->expects($this->once()) | ||
->method('getRank') | ||
->willReturn($originalRank); | ||
|
||
$node | ||
->expects($this->once()) | ||
->method('setRank') | ||
->with($expectedRank); | ||
|
||
$normalizer = new Normalizer( | ||
$scaleBottom, | ||
$scaleTop | ||
); | ||
|
||
$normalizer->normalize( | ||
$nodeCollection, | ||
$lowestRank, | ||
$highestRank | ||
); | ||
} | ||
|
||
public function dataProviderNormalize(): array | ||
{ | ||
return [ | ||
'realistic' => [ | ||
1.234, | ||
1.0, | ||
10.0, | ||
-5.0, | ||
5.0, | ||
6.6106 | ||
], | ||
'division_by_zero' => [ | ||
5, | ||
1.0, | ||
10.0, | ||
5.0, | ||
5.0, | ||
1.0 | ||
], | ||
'division_by_float_epsilon' => [ | ||
5, | ||
1.0, | ||
10.0, | ||
5.0, | ||
5.0 + PHP_FLOAT_EPSILON, | ||
1.0 | ||
], | ||
'scale_from_minus' => [ | ||
0.12577, | ||
-5, | ||
5, | ||
100, | ||
1000, | ||
-6.109713666666667 | ||
] | ||
]; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/unit/Service/PageRankAlgorithm/RankComparatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpScience\PageRank\Service\PageRankAlgorithm; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class RankComparatorTest extends TestCase | ||
{ | ||
private RankComparator $rankComparator; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->rankComparator = new RankComparator(); | ||
} | ||
|
||
/** | ||
* @dataProvider dataProviderIsEqual | ||
* | ||
* @param float $rank1 | ||
* @param float $rank2 | ||
* @param bool $expected | ||
*/ | ||
public function testIsEqual( | ||
float $rank1, | ||
float $rank2, | ||
bool $expected | ||
): void { | ||
$actual = $this->rankComparator->isEqual($rank1, $rank2); | ||
|
||
static::assertSame($expected, $actual); | ||
} | ||
|
||
public function dataProviderIsEqual(): array | ||
{ | ||
return [ | ||
'not_equal' => [ | ||
.1, .2, false | ||
], | ||
'equal' => [ | ||
.1, .1, true | ||
], | ||
'absolute_value_of_minus' => [ | ||
.1, .2, false | ||
], | ||
'absolute_of_minus_one_is_bigger_than_float_epsilon' => [ | ||
1, 2, false | ||
], | ||
'smallest_representable_difference' => [ | ||
1, 1 + PHP_FLOAT_EPSILON, false | ||
], | ||
'non_representable_difference' => [ | ||
1, 1 + 2.2204460492503e-17, true | ||
] | ||
]; | ||
} | ||
} |
Oops, something went wrong.