-
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
c1bf117
commit c507d3b
Showing
8 changed files
with
213 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpScience\PageRank\Service; | ||
|
||
use PhpScience\PageRank\Data\NodeCollectionInterface; | ||
|
||
class Normalizer implements NormalizerInterface | ||
{ | ||
private float $min; | ||
private float $max; | ||
|
||
public function __construct( | ||
float $min = 1, | ||
float $max = 10 | ||
) { | ||
$this->min = $min; | ||
$this->max = $max; | ||
} | ||
|
||
public function normalize( | ||
NodeCollectionInterface $nodeCollection, | ||
float $lowestRank, | ||
float $highestRank | ||
): void { | ||
foreach ($nodeCollection->getNodes() as $node) { | ||
$rank = $this->getRank($node->getRank(), $lowestRank, $highestRank); | ||
$node->setRank($rank); | ||
} | ||
} | ||
|
||
private function getRank(float $value, float $min, float $max): float | ||
{ | ||
$normalized = ($value - $min) / ($max - $min); | ||
$scaled = ($normalized * ($this->max - $this->min)) + $this->min; | ||
|
||
return $scaled; | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpScience\PageRank\Service; | ||
|
||
use PhpScience\PageRank\Data\NodeCollectionInterface; | ||
|
||
interface NormalizerInterface | ||
{ | ||
/** | ||
* It normalizes and scales the ranks in the node collection. | ||
* | ||
* @param NodeCollectionInterface $nodeCollection | ||
* @param float $lowestRank | ||
* @param float $highestRank | ||
*/ | ||
public function normalize( | ||
NodeCollectionInterface $nodeCollection, | ||
float $lowestRank, | ||
float $highestRank | ||
): void; | ||
} |
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
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