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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Content-based filtering utilities.
//!
//! Lightweight similarity functions operating directly on content metadata
//! (tags, categories, text features) without requiring user interaction data.
//!
//! These primitives complement the richer `content::similarity` module by
//! providing quick overlap-based similarity for cold-start scenarios or when
//! full embeddings are unavailable.
#![allow(dead_code)]
use std::collections::HashSet;
// ---------------------------------------------------------------------------
// ContentSimilarity
// ---------------------------------------------------------------------------
/// Stateless helper for computing set-based content similarity.
///
/// All methods are pure functions, so the struct carries no state and may be
/// constructed freely.
pub struct ContentSimilarity;
impl ContentSimilarity {
/// Creates a new [`ContentSimilarity`] helper.
#[must_use]
pub fn new() -> Self {
Self
}
/// Jaccard similarity between two tag lists.
///
/// Returns the size of the intersection divided by the size of the union
/// of the two tag sets. Returns `0.0` when both lists are empty.
///
/// Duplicate tags within a single list are deduplicated before comparison.
///
/// # Examples
///
/// ```
/// use oximedia_recommend::content_filter::ContentSimilarity;
///
/// let sim = ContentSimilarity::jaccard(&["action", "sci-fi"], &["action", "horror"]);
/// // intersection = {"action"}, union = {"action","sci-fi","horror"} → 1/3
/// assert!((sim - 1.0/3.0).abs() < 1e-6);
/// ```
#[must_use]
pub fn jaccard(tags_a: &[&str], tags_b: &[&str]) -> f32 {
let set_a: HashSet<&str> = tags_a.iter().copied().collect();
let set_b: HashSet<&str> = tags_b.iter().copied().collect();
let intersection = set_a.intersection(&set_b).count();
let union = set_a.union(&set_b).count();
if union == 0 {
return 0.0;
}
intersection as f32 / union as f32
}
/// Dice similarity coefficient between two tag lists.
///
/// `2 * |A ∩ B| / (|A| + |B|)` — tends to weight similarity higher than
/// Jaccard for sets with near-equal sizes.
///
/// Returns `0.0` when both lists are empty.
#[must_use]
pub fn dice(tags_a: &[&str], tags_b: &[&str]) -> f32 {
let set_a: HashSet<&str> = tags_a.iter().copied().collect();
let set_b: HashSet<&str> = tags_b.iter().copied().collect();
let intersection = set_a.intersection(&set_b).count();
let total = set_a.len() + set_b.len();
if total == 0 {
return 0.0;
}
2.0 * intersection as f32 / total as f32
}
/// Overlap coefficient (Szymkiewicz–Simpson) between two tag lists.
///
/// `|A ∩ B| / min(|A|, |B|)` — returns `1.0` when the smaller set is a
/// perfect subset of the larger. Returns `0.0` when either set is empty.
#[must_use]
pub fn overlap(tags_a: &[&str], tags_b: &[&str]) -> f32 {
let set_a: HashSet<&str> = tags_a.iter().copied().collect();
let set_b: HashSet<&str> = tags_b.iter().copied().collect();
let min_size = set_a.len().min(set_b.len());
if min_size == 0 {
return 0.0;
}
let intersection = set_a.intersection(&set_b).count();
intersection as f32 / min_size as f32
}
/// Cosine similarity over a binary tag-vector representation.
///
/// Builds the union vocabulary, encodes each tag list as a binary vector,
/// then computes the standard cosine similarity. Returns `0.0` when
/// either list is empty.
#[must_use]
pub fn cosine_binary(tags_a: &[&str], tags_b: &[&str]) -> f32 {
let set_a: HashSet<&str> = tags_a.iter().copied().collect();
let set_b: HashSet<&str> = tags_b.iter().copied().collect();
if set_a.is_empty() || set_b.is_empty() {
return 0.0;
}
let dot = set_a.intersection(&set_b).count() as f32;
let norm_a = (set_a.len() as f32).sqrt();
let norm_b = (set_b.len() as f32).sqrt();
dot / (norm_a * norm_b)
}
}
impl Default for ContentSimilarity {
fn default() -> Self {
Self::new()
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_jaccard_identical() {
let sim = ContentSimilarity::jaccard(&["a", "b", "c"], &["a", "b", "c"]);
assert!(
(sim - 1.0).abs() < 1e-6,
"identical sets: expected 1.0, got {sim}"
);
}
#[test]
fn test_jaccard_disjoint() {
let sim = ContentSimilarity::jaccard(&["a", "b"], &["c", "d"]);
assert!(sim.abs() < 1e-6, "disjoint sets: expected 0.0, got {sim}");
}
#[test]
fn test_jaccard_partial_overlap() {
let sim = ContentSimilarity::jaccard(&["action", "sci-fi"], &["action", "horror"]);
let expected = 1.0_f32 / 3.0;
assert!(
(sim - expected).abs() < 1e-6,
"expected {expected}, got {sim}"
);
}
#[test]
fn test_jaccard_empty_both() {
let sim = ContentSimilarity::jaccard(&[], &[]);
assert!(sim.abs() < 1e-6);
}
#[test]
fn test_jaccard_one_empty() {
let sim = ContentSimilarity::jaccard(&["a"], &[]);
assert!(sim.abs() < 1e-6);
}
#[test]
fn test_jaccard_deduplicates() {
// Duplicate "a" in both lists shouldn't inflate the score
let sim_dup = ContentSimilarity::jaccard(&["a", "a", "b"], &["a", "b"]);
let sim_clean = ContentSimilarity::jaccard(&["a", "b"], &["a", "b"]);
assert!(
(sim_dup - sim_clean).abs() < 1e-6,
"duplicates should be ignored"
);
}
#[test]
fn test_dice_identical() {
let sim = ContentSimilarity::dice(&["x", "y"], &["x", "y"]);
assert!((sim - 1.0).abs() < 1e-6);
}
#[test]
fn test_dice_disjoint() {
let sim = ContentSimilarity::dice(&["a"], &["b"]);
assert!(sim.abs() < 1e-6);
}
#[test]
fn test_overlap_subset() {
// {"a"} ⊆ {"a","b","c"} → overlap = 1.0
let sim = ContentSimilarity::overlap(&["a"], &["a", "b", "c"]);
assert!((sim - 1.0).abs() < 1e-6, "subset: expected 1.0, got {sim}");
}
#[test]
fn test_cosine_binary_identical() {
let sim = ContentSimilarity::cosine_binary(&["a", "b"], &["a", "b"]);
assert!((sim - 1.0).abs() < 1e-6);
}
#[test]
fn test_cosine_binary_orthogonal() {
let sim = ContentSimilarity::cosine_binary(&["a"], &["b"]);
assert!(sim.abs() < 1e-6);
}
}