<?php
 class vB_RelatedText_TfIdf { use vB_Trait_NoSerialize; private array $idf; public function __construct(vB_RelatedText_Vocabulary $vocabularly) { $this->idf = $this->calculateIdf($vocabularly); } public function transform(array &$data) : void { foreach ($data AS &$record) { foreach ($record AS $ordinal => $count) { $record[$ordinal] = $count * $this->idf[$ordinal]; } } } private function calculateIdf(vB_RelatedText_Vocabulary $vocabulary) : array { $total = $vocabulary->getTotalDocCount(); $idf = []; foreach ($vocabulary->getDocCounts() AS $ordinal => $count) { $idf[$ordinal] = log($total/$count, 2); } return $idf; } } 