-
Notifications
You must be signed in to change notification settings - Fork 2
/
CosineSimilarity.php
101 lines (83 loc) · 2.3 KB
/
CosineSimilarity.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
class CosineSimilarity
{
protected $data_a;
protected $data_b;
public function __construct(array $data_a, array $data_b)
{
$this->data_a = $data_a;
$this->data_b = $data_b;
}
/**
* Mengambil nilai similaritas (kesamaan)
* Rentang hasil: 0 - 1
* Rumus: sum(ai * bi) / (root(sum(ai^2)) * root(sum(bi^2)))
*
* @return float
*/
public function calculate(): float
{
$top = $this->getTop();
$div = $this->getDivider();
return $top / $div;
}
/**
* Kalkulasi nilai atas (angka yang mau dibagi)
* Rumus: sum(ai * bi)
*
* @return float
*/
private function getTop(): float
{
$data_a = $this->data_a;
$data_b = $this->data_b;
$sum = 0;
foreach ($data_a as $i => $a) {
$b = isset($data_b[$i]) ? $data_b[$i] : null;
// Jika salah satu dari a atau b nilainya null ...
if (is_null($a) || is_null($b)) {
continue; // ... skip
}
$sum += $a * $b;
}
return $sum;
}
/**
* Kalkulasi nilai pembagi
* Rumus: root(sum(ai^2)) * root(sum(bi^2))
*
* @return float
*/
private function getDivider(): float
{
$data_a = $this->data_a;
$data_b = $this->data_b;
$root_sum_square_a = $this->rootSumSquares($data_a);
$root_sum_square_b = $this->rootSumSquares($data_b);
return $root_sum_square_a * $root_sum_square_b;
}
private function rootSumSquares(array $data): float
{
// Kalkulasi nilai kuadrat masing-masing item dari data
// [1, null, 2] -> [1, 0, 4] (null diubah jadi 0)
$squares = array_map(function($x) {
return is_null($x) ? 0 : $x * $x;
}, $data);
// Summary hasil kuadrat
// [1, 0, 4] -> 5
$sum_squares = array_sum($squares);
// Kembalikan akar dari summary
return sqrt($sum_squares);
}
/**
* Static function untuk menyederhanakan
* pemanggilan fungsi calculate tanpa harus
* inisiasi class terlebih dehulu
*
* @return float
*/
public static function calc(array $data_a, array $data_b): float
{
return (new static($data_a, $data_b))->calculate();
}
}