-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculateCosineSimilarity.php
67 lines (53 loc) · 1.7 KB
/
calculateCosineSimilarity.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
<?php
// https://github.com/n-gram-hub/NLP-depot/blob/master/calculateDotProduct.php
require 'calculateDotProduct.php';
function pow_callback($e){
return pow($e, 2);
}
function range_callback($e){
return $e >= -1 && $e <= 1 ? 1 : 0;
}
function elementIsOutOfRange($vector){
return array_product(array_map('range_callback', $vector)) == 0;
}
/**
*
* Calculates the cosine similarity of two vectors.
*
* @param array $vector1 The first vector
* @param array $vector2 The second vector
*
* @author https://github.com/n-gram-hub
*
* @return float
*
*/
function calculateCosineSimilarity(array $vector1, array $vector2){
// calculate dot product
try {
$dotProduct = calculateDotProduct($vector1, $vector2);
} catch (LengthException $e) {
echo "LengthException: " . $e->getMessage();
} catch (UnexpectedValueException $e) {
echo "UnexpectedValueException: " . $e->getMessage();
} catch (Exception $e) {
echo "Exception: " . $e->getMessage();
}
// check if any element falls within the -1/1 range
if(elementIsOutOfRange($vector1) || elementIsOutOfRange($vector2)){
throw new RangeException("Numbers must fall between -1.0 and 1.0");
}
// norms
$v1n = sqrt(array_sum(array_map('pow_callback', $vector1)));
$v2n = sqrt(array_sum(array_map('pow_callback', $vector2)));
return $dotProduct / ($v1n * $v2n);
}
try{
echo calculateCosineSimilarity([0.1,1.0,0.22,0,1],[0.1,0.1,1,0,1]);
} catch (RangeException $e){
echo "Exception: " . $e->getMessage();
} catch (ArgumentCountError $e) {
echo "ArgumentCountError: " . $e->getMessage();
} catch (TypeError $e) {
echo "TypeError: " . $e->getMessage();
}