oxirs-embed 0.2.4

Knowledge graph embeddings with TransE, ComplEx, and custom models
Documentation
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Cross-encoder re-ranker for embedding search results.
//!
//! Implements a lightweight simulation of cross-encoder scoring using
//! token-overlap (Jaccard) similarity.  In a production system the `score`
//! function would call a transformer model; here it is kept deterministic and
//! dependency-free for testing purposes.

use std::collections::HashSet;

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// A (query, document) pair submitted for re-ranking, together with the
/// initial retrieval score produced by an upstream embedding model.
#[derive(Debug, Clone)]
pub struct CandidatePair {
    /// The user query.
    pub query: String,
    /// The candidate document / passage.
    pub document: String,
    /// Score produced by the first-stage retrieval model.
    pub initial_score: f32,
}

/// The outcome of re-ranking a single candidate document.
#[derive(Debug, Clone)]
pub struct RerankResult {
    /// The candidate document text.
    pub document: String,
    /// Score from the first-stage retrieval model.
    pub initial_score: f32,
    /// Score assigned by the cross-encoder.
    pub rerank_score: f32,
    /// 1-based rank in the sorted result list (lower is better).
    pub rank: usize,
}

/// Configuration for a `CrossEncoder` instance.
#[derive(Debug, Clone)]
pub struct CrossEncoderConfig {
    /// Maximum token length (currently advisory; ignored in the simulation).
    pub max_length: usize,
    /// When `true`, scores are min-max normalised to `[0, 1]` before being
    /// returned.
    pub normalize_scores: bool,
    /// How many pairs to process per batch.
    pub batch_size: usize,
}

impl Default for CrossEncoderConfig {
    fn default() -> Self {
        CrossEncoderConfig {
            max_length: 512,
            normalize_scores: false,
            batch_size: 32,
        }
    }
}

/// Stateful cross-encoder that tracks the total number of pairs scored.
pub struct CrossEncoder {
    config: CrossEncoderConfig,
    total_scored: u64,
}

impl CrossEncoder {
    /// Create a new `CrossEncoder` with the given configuration.
    pub fn new(config: CrossEncoderConfig) -> Self {
        CrossEncoder {
            config,
            total_scored: 0,
        }
    }

    /// Score a single `(query, document)` pair using token-overlap similarity.
    pub fn score(&mut self, pair: &CandidatePair) -> f32 {
        self.total_scored += 1;
        token_overlap_score(&pair.query, &pair.document)
    }

    /// Score multiple pairs at once.  Respects `batch_size` but processes
    /// sequentially in the simulation (no concurrency overhead).
    pub fn score_batch(&mut self, pairs: &[CandidatePair]) -> Vec<f32> {
        pairs.iter().map(|p| self.score(p)).collect()
    }

    /// Re-rank `candidates` against `query`.
    ///
    /// Returns a list of [`RerankResult`] sorted by `rerank_score` descending,
    /// with `rank` fields assigned from 1.
    pub fn rerank(
        &mut self,
        query: &str,
        candidates: &[String],
        initial_scores: &[f32],
    ) -> Vec<RerankResult> {
        let n = candidates.len().min(initial_scores.len());
        let pairs: Vec<CandidatePair> = (0..n)
            .map(|i| CandidatePair {
                query: query.to_string(),
                document: candidates[i].clone(),
                initial_score: initial_scores[i],
            })
            .collect();

        let mut raw_scores = self.score_batch(&pairs);

        if self.config.normalize_scores {
            raw_scores = normalize_scores(&raw_scores);
        }

        let mut results: Vec<RerankResult> = (0..n)
            .map(|i| RerankResult {
                document: candidates[i].clone(),
                initial_score: initial_scores[i],
                rerank_score: raw_scores[i],
                rank: 0, // filled below
            })
            .collect();

        // Sort descending by rerank_score.
        results.sort_by(|a, b| {
            b.rerank_score
                .partial_cmp(&a.rerank_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Assign 1-based ranks.
        for (idx, r) in results.iter_mut().enumerate() {
            r.rank = idx + 1;
        }

        results
    }

    /// Like `rerank` but truncates the output to the top-`k` results.
    pub fn top_k(
        &mut self,
        query: &str,
        candidates: &[String],
        initial_scores: &[f32],
        k: usize,
    ) -> Vec<RerankResult> {
        let mut all = self.rerank(query, candidates, initial_scores);
        all.truncate(k);
        all
    }

    /// Total number of individual pairs that have been scored so far.
    pub fn total_scored(&self) -> u64 {
        self.total_scored
    }
}

// ---------------------------------------------------------------------------
// Internal helpers (also exposed for tests)
// ---------------------------------------------------------------------------

/// Jaccard similarity over whitespace-tokenised sets.
///
/// Returns `1.0` for identical strings (even empty ones) and `0.0` for
/// completely disjoint token sets.
pub(crate) fn token_overlap_score(a: &str, b: &str) -> f32 {
    let set_a: HashSet<&str> = a.split_whitespace().collect();
    let set_b: HashSet<&str> = b.split_whitespace().collect();

    if set_a.is_empty() && set_b.is_empty() {
        // Both empty → treat as identical.
        return 1.0;
    }

    let intersection = set_a.intersection(&set_b).count();
    let union = set_a.union(&set_b).count();
    if union == 0 {
        0.0
    } else {
        intersection as f32 / union as f32
    }
}

/// Min-max normalise a slice of scores to `[0, 1]`.
///
/// If all scores are equal the output is all-zeros (undefined range).
pub(crate) fn normalize_scores(scores: &[f32]) -> Vec<f32> {
    if scores.is_empty() {
        return Vec::new();
    }

    let min = scores.iter().copied().fold(f32::MAX, f32::min);
    let max = scores.iter().copied().fold(f32::MIN, f32::max);

    let range = max - min;
    if range == 0.0 {
        return vec![0.0; scores.len()];
    }
    scores.iter().map(|&s| (s - min) / range).collect()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    fn default_encoder() -> CrossEncoder {
        CrossEncoder::new(CrossEncoderConfig::default())
    }

    fn norming_encoder() -> CrossEncoder {
        CrossEncoder::new(CrossEncoderConfig {
            normalize_scores: true,
            ..CrossEncoderConfig::default()
        })
    }

    // --- token_overlap_score ---

    #[test]
    fn test_token_overlap_identical_strings() {
        let score = token_overlap_score("the quick brown fox", "the quick brown fox");
        assert!(
            (score - 1.0).abs() < 1e-6,
            "identical strings should score 1.0"
        );
    }

    #[test]
    fn test_token_overlap_disjoint_strings() {
        let score = token_overlap_score("apple orange", "banana grape");
        assert!(
            (score - 0.0).abs() < 1e-6,
            "disjoint strings should score 0.0"
        );
    }

    #[test]
    fn test_token_overlap_partial_match() {
        let score = token_overlap_score("the fox", "the cat");
        // intersection={the}, union={the,fox,cat} → 1/3
        assert!((score - 1.0 / 3.0).abs() < 1e-5);
    }

    #[test]
    fn test_token_overlap_both_empty() {
        let score = token_overlap_score("", "");
        assert!((score - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_token_overlap_one_empty() {
        let score = token_overlap_score("hello", "");
        assert!((score - 0.0).abs() < 1e-6);
    }

    // --- normalize_scores ---

    #[test]
    fn test_normalize_scores_range() {
        let scores = vec![0.1f32, 0.5, 0.9];
        let norm = normalize_scores(&scores);
        // All normalised values must lie in [0, 1].
        for &v in &norm {
            assert!(v >= 0.0, "normalised value {v} is below 0");
            assert!(v <= 1.0, "normalised value {v} is above 1");
        }
    }

    #[test]
    fn test_normalize_scores_min_is_zero() {
        let scores = vec![2.0f32, 4.0, 6.0];
        let norm = normalize_scores(&scores);
        assert!((norm[0] - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_normalize_scores_max_is_one() {
        let scores = vec![2.0f32, 4.0, 6.0];
        let norm = normalize_scores(&scores);
        assert!((norm[2] - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_normalize_scores_all_equal() {
        let scores = vec![3.0f32, 3.0, 3.0];
        let norm = normalize_scores(&scores);
        assert!(norm.iter().all(|&v| v == 0.0));
    }

    #[test]
    fn test_normalize_scores_empty() {
        let norm = normalize_scores(&[]);
        assert!(norm.is_empty());
    }

    // --- CrossEncoder::score ---

    #[test]
    fn test_score_identical() {
        let mut enc = default_encoder();
        let pair = CandidatePair {
            query: "foo bar".into(),
            document: "foo bar".into(),
            initial_score: 0.9,
        };
        let s = enc.score(&pair);
        assert!((s - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_score_disjoint() {
        let mut enc = default_encoder();
        let pair = CandidatePair {
            query: "apple".into(),
            document: "banana".into(),
            initial_score: 0.1,
        };
        let s = enc.score(&pair);
        assert!((s - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_score_increments_total_scored() {
        let mut enc = default_encoder();
        assert_eq!(enc.total_scored(), 0);
        let pair = CandidatePair {
            query: "x".into(),
            document: "y".into(),
            initial_score: 0.0,
        };
        enc.score(&pair);
        assert_eq!(enc.total_scored(), 1);
    }

    // --- CrossEncoder::score_batch ---

    #[test]
    fn test_score_batch_length_matches_input() {
        let mut enc = default_encoder();
        let pairs: Vec<CandidatePair> = (0..5)
            .map(|i| CandidatePair {
                query: format!("query {i}"),
                document: format!("doc {i}"),
                initial_score: 0.5,
            })
            .collect();
        let scores = enc.score_batch(&pairs);
        assert_eq!(scores.len(), 5);
    }

    #[test]
    fn test_score_batch_increments_total_scored() {
        let mut enc = default_encoder();
        let pairs: Vec<CandidatePair> = (0..10)
            .map(|i| CandidatePair {
                query: "q".into(),
                document: format!("d {i}"),
                initial_score: 0.0,
            })
            .collect();
        enc.score_batch(&pairs);
        assert_eq!(enc.total_scored(), 10);
    }

    // --- CrossEncoder::rerank ---

    #[test]
    fn test_rerank_sorted_descending() {
        let mut enc = default_encoder();
        let candidates = vec![
            "apple".to_string(),
            "apple banana".to_string(),
            "apple banana cherry".to_string(),
        ];
        let query = "apple banana cherry";
        let initial = vec![0.3, 0.6, 0.9];
        let results = enc.rerank(query, &candidates, &initial);
        // Results should be sorted by rerank_score descending.
        for w in results.windows(2) {
            assert!(w[0].rerank_score >= w[1].rerank_score);
        }
    }

    #[test]
    fn test_rerank_rank_field_correct() {
        let mut enc = default_encoder();
        let candidates = vec!["a b c".to_string(), "x y z".to_string()];
        let results = enc.rerank("a b c", &candidates, &[0.5, 0.5]);
        assert_eq!(results[0].rank, 1);
        assert_eq!(results[1].rank, 2);
    }

    #[test]
    fn test_rerank_empty_candidates() {
        let mut enc = default_encoder();
        let results = enc.rerank("query", &[], &[]);
        assert!(results.is_empty());
    }

    #[test]
    fn test_rerank_total_scored_increments() {
        let mut enc = default_encoder();
        let docs: Vec<String> = (0..3).map(|i| format!("doc {i}")).collect();
        let scores: Vec<f32> = (0..3).map(|i| i as f32 * 0.1).collect();
        enc.rerank("q", &docs, &scores);
        assert_eq!(enc.total_scored(), 3);
    }

    // --- CrossEncoder::top_k ---

    #[test]
    fn test_top_k_limits_output() {
        let mut enc = default_encoder();
        let docs: Vec<String> = (0..10).map(|i| format!("word{i} text")).collect();
        let initial: Vec<f32> = (0..10).map(|i| i as f32 * 0.1).collect();
        let results = enc.top_k("word5 text", &docs, &initial, 3);
        assert_eq!(results.len(), 3);
    }

    #[test]
    fn test_top_k_returns_all_when_k_exceeds_count() {
        let mut enc = default_encoder();
        let docs = vec!["a".to_string(), "b".to_string()];
        let results = enc.top_k("a", &docs, &[0.5, 0.2], 100);
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_top_k_rank_starts_at_one() {
        let mut enc = default_encoder();
        let docs = vec!["hello world".to_string(), "foo bar".to_string()];
        let results = enc.top_k("hello world", &docs, &[0.5, 0.5], 2);
        assert_eq!(results[0].rank, 1);
    }

    // --- normalize_scores integration ---

    #[test]
    fn test_rerank_with_normalisation_range() {
        let mut enc = norming_encoder();
        let docs = vec!["a b".to_string(), "c d".to_string(), "e f".to_string()];
        let initial = vec![0.1, 0.5, 0.9];
        let results = enc.rerank("a b", &docs, &initial);
        for r in &results {
            assert!(r.rerank_score >= 0.0 && r.rerank_score <= 1.0);
        }
    }
}