1 | <?php |
||
12 | class Relevance implements JsonSerializable |
||
13 | { |
||
14 | const EXACT_MATCH = 5; |
||
15 | |||
16 | /** |
||
17 | * Relevance score |
||
18 | * |
||
19 | * Ideally in a range from 0-10, with 5 indicating a fundamentally useful |
||
20 | * result. |
||
21 | * |
||
22 | * @var float |
||
23 | */ |
||
24 | private $score; |
||
25 | |||
26 | /** |
||
27 | * A list of rationales indicating how the relevance score was generated. |
||
28 | * |
||
29 | * @var string[] |
||
30 | */ |
||
31 | private $rationales = []; |
||
32 | |||
33 | /** |
||
34 | * Construct a Relevance object |
||
35 | * |
||
36 | * @param float $score (Optional, defaults to 0.0) |
||
37 | * @param string $rationale (Optional, defaults to `"Base value"` if |
||
38 | * `$score` is non-zero`) |
||
39 | */ |
||
40 | public function __construct($score = 0.0, $rationale = "Base value") |
||
47 | |||
48 | /** |
||
49 | * Add to the relevance of a score |
||
50 | * |
||
51 | * Or, one presumes, subtract, if `$scoreIncrement` is negative! |
||
52 | * |
||
53 | * @param float $scoreIncrement |
||
54 | * @param string $rationale Rationale for this particular increment |
||
55 | */ |
||
56 | public function add($scoreIncrement, $rationale) |
||
61 | |||
62 | /** |
||
63 | * Relevance score (ideally 0-10, with 5 indicating a fundamentally useful |
||
64 | * result) |
||
65 | * |
||
66 | * @return float |
||
67 | */ |
||
68 | public function getScore() |
||
72 | |||
73 | /** |
||
74 | * List of rationale's for how the relevance score was calculated |
||
75 | * |
||
76 | * @param string $separator (Optional, defaults to `', '`) |
||
77 | * @return string List of scoring rationales |
||
78 | */ |
||
79 | public function getRationale($separator = ', ') |
||
83 | |||
84 | /** |
||
85 | * Calculate what proportion of the `$haystack` is made up of `$needle` |
||
86 | * |
||
87 | * For example `Go Dog Go!` is 40% `Go`, `Hello World` is 100% `Hello World` |
||
88 | * |
||
89 | * @param string $haystack |
||
90 | * @param string $needle |
||
91 | * @return float |
||
92 | */ |
||
93 | public static function stringProportion($haystack, $needle) |
||
101 | |||
102 | public function jsonSerialize() |
||
109 | } |
||
110 |