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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! # Object Encoder Module
//!
//! This module implements an object encoder for hyperdimensional computing using
//! vector symbolic architectures (VSAs). It takes as input a JSON object (using
//! [serde_json::Value]) and produces an encoded hypervector. It maintains two codebooks:
//!
//! 1. **Token Codebook:** Maps each unique JSON token (from keys or values) to a randomly generated hypervector.
//! 2. **Object (Document) Codebook:** Maps a stringified JSON object to its encoded hypervector.
//!
//! Each key–value pair in the JSON object is encoded by binding the hypervector for the key with that for the value.
//! The final object representation is computed by bundling all these bound hypervectors together.
//!
//! This encoder is generic over any VSA type (i.e. any type implementing the [`VSA`] trait).
//! The methods [`token_codebook`], [`object_codebook`], and [`get_encoded_object`] allow you to inspect or retrieve the generated codebooks.
use serde_json::Value;
use std::collections::HashMap;
use crate::{Hypervector, TieBreaker, VSA};
/// An object encoder that produces hypervector encodings for JSON objects.
///
/// # Type Parameters
///
/// * `V` - A type that implements the [`VSA`] trait.
pub struct ObjectEncoder<V: VSA> {
dim: usize,
tie_breaker: TieBreaker,
/// Codebook mapping unique tokens (from keys or values) to hypervectors.
token_codebook: HashMap<String, Hypervector<V>>,
/// Codebook mapping stringified JSON objects to their encoded hypervector.
object_codebook: HashMap<String, Hypervector<V>>,
}
impl<V: VSA> ObjectEncoder<V> {
/// Creates a new `ObjectEncoder`.
///
/// # Arguments
///
/// * `dim` - The dimension of the hypervectors to generate.
/// * `tie_breaker` - The tie-breaking rule used during bundling.
pub fn new(dim: usize, tie_breaker: TieBreaker) -> Self {
Self {
dim,
tie_breaker,
token_codebook: HashMap::new(),
object_codebook: HashMap::new(),
}
}
/// Retrieves (or generates) the hypervector associated with a given token.
///
/// Tokens are represented as strings. If the token does not exist in the codebook,
/// a new random hypervector is generated (using [`Hypervector::generate`]) and stored.
///
/// # Arguments
///
/// * `token` - A string slice representing the token.
pub fn get_token_vector(&mut self, token: &str) -> Hypervector<V> {
self.token_codebook
.entry(token.to_string())
.or_insert_with(|| Hypervector::<V>::generate(self.dim))
.clone()
}
/// Encodes a JSON object into a hypervector.
///
/// The JSON object is expected to be a map. For each key–value pair:
///
/// 1. The key (a string) is used as a token.
/// 2. If the value is an array, each element is encoded:
/// - If an element is an object, it is encoded recursively.
/// - Otherwise, the element is converted to a token (via [`Self::value_to_token`]) and its hypervector is generated.
/// The resulting hypervectors are bundled together.
/// 3. The key hypervector is bound with the (possibly bundled) value hypervector.
///
/// All resulting bound hypervectors are bundled (using the VSA’s bundling operator)
/// to yield a final hypervector representing the object. The computed hypervector is stored in the object codebook.
/// On subsequent calls with the same JSON object, the stored encoding is returned.
///
/// # Arguments
///
/// * `json_obj` - A reference to the JSON object to encode.
pub fn encode_object(&mut self, json_obj: &Value) -> Hypervector<V> {
// Use the string representation as a key.
let obj_str = json_obj.to_string();
if let Some(existing) = self.object_codebook.get(&obj_str) {
return existing.clone();
}
let obj = json_obj
.as_object()
.expect("Expected a JSON object for encoding");
let mut bound_vectors = Vec::with_capacity(obj.len());
for (key, value) in obj {
let key_vector = self.get_token_vector(key);
let value_vector = match value {
// For an array value, encode each element then bundle them.
Value::Array(arr) => {
let mut elem_vectors = Vec::with_capacity(arr.len());
for elem in arr {
// If an element is an object, encode it recursively.
if elem.is_object() {
elem_vectors.push(self.encode_object(elem));
} else {
elem_vectors.push(self.get_token_vector(&Self::value_to_token(elem)));
}
}
// If the array is empty, use a default token.
if elem_vectors.is_empty() {
self.get_token_vector("empty_array")
} else if elem_vectors.len() == 1 {
elem_vectors.remove(0)
} else {
Hypervector::<V>::bundle_many(&elem_vectors, self.tie_breaker)
}
}
// For all other value types, convert the value to a token.
_ => {
let token = Self::value_to_token(value);
self.get_token_vector(&token)
}
};
// Bind the key vector with the (possibly bundled) value vector.
let bound = key_vector.bind(&value_vector);
bound_vectors.push(bound);
}
// Bundle all bound vectors into a single hypervector.
let object_vector = Hypervector::<V>::bundle_many(&bound_vectors, self.tie_breaker);
self.object_codebook.insert(obj_str, object_vector.clone());
object_vector
}
/// Retrieves an encoded hypervector for a given JSON object, if it was encoded previously.
///
/// Returns `None` if the JSON object has not been encoded.
///
/// # Arguments
///
/// * `json_obj` - A reference to the JSON object.
pub fn get_encoded_object(&self, json_obj: &Value) -> Option<&Hypervector<V>> {
self.object_codebook.get(&json_obj.to_string())
}
/// Converts a JSON value into a string token.
///
/// For simple types (strings, numbers, booleans) the conversion is straightforward.
/// For other types (arrays or objects), the entire JSON value is stringified.
///
/// # Arguments
///
/// * `value` - A reference to the JSON value.
fn value_to_token(value: &Value) -> String {
match value {
Value::String(s) => s.clone(),
Value::Number(n) => n.to_string(),
Value::Bool(b) => b.to_string(),
_ => value.to_string(),
}
}
/// Returns a reference to the token codebook.
///
/// This codebook maps each token (from keys or primitive values) to its generated hypervector.
pub fn token_codebook(&self) -> &HashMap<String, Hypervector<V>> {
&self.token_codebook
}
/// Returns a reference to the object (document) codebook.
///
/// This codebook maps the string representation of JSON objects to their encoded hypervectors.
pub fn object_codebook(&self) -> &HashMap<String, Hypervector<V>> {
&self.object_codebook
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mbat::MBAT;
use crate::TieBreaker;
use serde_json::json;
/// Test that calling `get_token_vector` with the same token yields identical hypervectors.
#[test]
fn test_token_vector_consistency() {
let mut encoder = ObjectEncoder::<MBAT>::new(1000, TieBreaker::AlwaysPositive);
let token = "exampleToken";
let vec1 = encoder.get_token_vector(token);
let vec2 = encoder.get_token_vector(token);
assert_eq!(
vec1, vec2,
"Token vectors should be consistent across multiple calls"
);
}
/// Test encoding a JSON object.
#[test]
fn test_encode_object() {
let mut encoder = ObjectEncoder::<MBAT>::new(1000, TieBreaker::AlwaysPositive);
let json_obj = json!({
"firstName": "John",
"lastName": "Doe",
"isActive": true
});
let encoded1 = encoder.encode_object(&json_obj);
let retrieved = encoder
.get_encoded_object(&json_obj)
.expect("Encoded object should be stored in the codebook");
assert_eq!(
encoded1, *retrieved,
"Stored encoding should match computed encoding"
);
let encoded2 = encoder.encode_object(&json_obj);
assert_eq!(
encoded1, encoded2,
"Re-encoding the same object should produce the same hypervector"
);
}
/// Test that different JSON objects produce different encodings.
#[test]
fn test_different_objects_have_different_encodings() {
let mut encoder = ObjectEncoder::<MBAT>::new(1000, TieBreaker::AlwaysPositive);
let json_obj1 = json!({
"firstName": "John",
"lastName": "Doe",
"isActive": true
});
let json_obj2 = json!({
"firstName": "Jane",
"lastName": "Doe",
"isActive": false
});
let encoded1 = encoder.encode_object(&json_obj1);
let encoded2 = encoder.encode_object(&json_obj2);
assert_ne!(
encoded1, encoded2,
"Different JSON objects should have different encodings"
);
}
/// Additional test: Generate 10 random entity hypervectors, verify that they are nearly orthogonal,
/// then bind all possible combinations. For a particular pair, the re-computed binding should have a cosine
/// similarity of ~1 with the stored binding for that pair, while bindings for other pairs remain nearly orthogonal.
#[test]
fn test_entity_binding_retrieval() {
let mut encoder = ObjectEncoder::<MBAT>::new(1000, TieBreaker::AlwaysPositive);
let num_entities = 10;
let mut entity_vectors = Vec::new();
// Generate hypervectors for tokens "entity0", "entity1", ... "entity9"
for i in 0..num_entities {
let token = format!("entity{}", i);
let hv = encoder.get_token_vector(&token);
entity_vectors.push(hv);
}
// Check that all entity hypervectors are nearly orthogonal.
for i in 0..num_entities {
for j in (i + 1)..num_entities {
let sim = entity_vectors[i].cosine_similarity(&entity_vectors[j]);
assert!(
sim < 0.2,
"Entity hypervectors {} and {} are not orthogonal enough: sim = {}",
i,
j,
sim
);
}
}
// Compute and store bindings for all unique pairs.
let mut pair_bindings = Vec::new();
for i in 0..num_entities {
for j in (i + 1)..num_entities {
let binding = entity_vectors[i].bind(&entity_vectors[j]);
pair_bindings.push(((i, j), binding));
}
}
// Choose a particular pair, e.g., (3, 7), for which to "retrieve" the binding.
let query_pair = (3, 7);
let query_binding = entity_vectors[query_pair.0].bind(&entity_vectors[query_pair.1]);
// Compare the query binding to each stored binding.
for &((i, j), ref binding) in &pair_bindings {
let sim = query_binding.cosine_similarity(binding);
if (i, j) == query_pair {
assert!(
(sim - 1.0).abs() < 1e-6,
"Correct pair ({},{}) similarity not close to 1: sim = {}",
i,
j,
sim
);
} else {
assert!(
sim < 0.1,
"Binding for pair ({},{}) has unexpected similarity with query pair: sim = {}",
i,
j,
sim
);
}
}
}
/// Test retrieval of the codebooks.
#[test]
fn test_fetch_codebooks() {
let mut encoder = ObjectEncoder::<MBAT>::new(1000, TieBreaker::AlwaysPositive);
// Encode a couple of objects.
let json_obj1 = json!({
"firstName": "John",
"lastName": "Doe",
"isActive": true
});
let json_obj2 = json!({
"firstName": "Jane",
"lastName": "Smith",
"isActive": false
});
encoder.encode_object(&json_obj1);
encoder.encode_object(&json_obj2);
// Fetch token codebook.
let token_book = encoder.token_codebook();
assert!(
!token_book.is_empty(),
"Token codebook should not be empty after encoding objects"
);
println!("Token codebook has {} entries.", token_book.len());
// Fetch object (document) codebook.
let object_book = encoder.object_codebook();
assert!(
!object_book.is_empty(),
"Object codebook should not be empty after encoding objects"
);
println!("Object codebook has {} entries.", object_book.len());
}
}
#[cfg(test)]
mod ssp_tests {
use super::*;
use crate::ssp::SSP;
use crate::TieBreaker;
use serde_json::json;
/// Test that calling `get_token_vector` with the same token yields identical hypervectors for SSP.
#[test]
fn test_ssp_token_vector_consistency() {
let mut encoder = ObjectEncoder::<SSP>::new(1000, TieBreaker::AlwaysPositive);
let token = "exampleToken";
let vec1 = encoder.get_token_vector(token);
let vec2 = encoder.get_token_vector(token);
assert_eq!(
vec1, vec2,
"SSP token vectors should be consistent across multiple calls"
);
}
/// Test encoding a JSON object using SSP.
#[test]
fn test_ssp_encode_object() {
let mut encoder = ObjectEncoder::<SSP>::new(1000, TieBreaker::AlwaysPositive);
let json_obj = json!({
"firstName": "John",
"lastName": "Doe",
"isActive": true
});
let encoded1 = encoder.encode_object(&json_obj);
let retrieved = encoder
.get_encoded_object(&json_obj)
.expect("SSP: Encoded object should be stored in the codebook");
assert_eq!(
encoded1, *retrieved,
"SSP: Stored encoding should match computed encoding"
);
let encoded2 = encoder.encode_object(&json_obj);
assert_eq!(
encoded1, encoded2,
"SSP: Re-encoding the same object should produce the same hypervector"
);
}
/// Test that different JSON objects produce different encodings using SSP.
#[test]
fn test_ssp_different_objects_have_different_encodings() {
let mut encoder = ObjectEncoder::<SSP>::new(1000, TieBreaker::AlwaysPositive);
let json_obj1 = json!({
"firstName": "John",
"lastName": "Doe",
"isActive": true
});
let json_obj2 = json!({
"firstName": "Jane",
"lastName": "Doe",
"isActive": false
});
let encoded1 = encoder.encode_object(&json_obj1);
let encoded2 = encoder.encode_object(&json_obj2);
assert_ne!(
encoded1, encoded2,
"SSP: Different JSON objects should have different encodings"
);
}
/// Additional test: Generate 10 random entity hypervectors using SSP, verify that they are nearly orthogonal,
/// then bind all possible combinations. For a particular pair, the re-computed binding should have a cosine
/// similarity of ~1 with the stored binding for that pair, while bindings for other pairs remain nearly orthogonal.
#[test]
fn test_ssp_entity_binding_retrieval() {
let mut encoder = ObjectEncoder::<SSP>::new(1000, TieBreaker::AlwaysPositive);
let num_entities = 10;
let mut entity_vectors = Vec::new();
// Generate hypervectors for tokens "entity0", "entity1", ... "entity9"
for i in 0..num_entities {
let token = format!("entity{}", i);
let hv = encoder.get_token_vector(&token);
entity_vectors.push(hv);
}
// Check that all entity hypervectors are nearly orthogonal.
for i in 0..num_entities {
for j in (i + 1)..num_entities {
let sim = entity_vectors[i].cosine_similarity(&entity_vectors[j]);
assert!(
sim < 0.2,
"SSP: Entity hypervectors {} and {} are not orthogonal enough: sim = {}",
i,
j,
sim
);
}
}
// Compute and store bindings for all unique pairs.
let mut pair_bindings = Vec::new();
for i in 0..num_entities {
for j in (i + 1)..num_entities {
let binding = entity_vectors[i].bind(&entity_vectors[j]);
pair_bindings.push(((i, j), binding));
}
}
// Choose a particular pair, e.g., (3, 7), for which to "retrieve" the binding.
let query_pair = (3, 7);
let query_binding = entity_vectors[query_pair.0].bind(&entity_vectors[query_pair.1]);
// Compare the query binding to each stored binding.
for &((i, j), ref binding) in &pair_bindings {
let sim = query_binding.cosine_similarity(binding);
if (i, j) == query_pair {
assert!(
(sim - 1.0).abs() < 1e-6,
"SSP: Correct pair ({},{}) similarity not close to 1: sim = {}",
i,
j,
sim
);
} else {
assert!(
sim < 0.1,
"SSP: Binding for pair ({},{}) has unexpected similarity with query pair: sim = {}",
i,
j,
sim
);
}
}
}
/// Test retrieval of the codebooks using SSP.
#[test]
fn test_ssp_fetch_codebooks() {
let mut encoder = ObjectEncoder::<SSP>::new(1000, TieBreaker::AlwaysPositive);
// Encode a couple of objects.
let json_obj1 = json!({
"firstName": "John",
"lastName": "Doe",
"isActive": true
});
let json_obj2 = json!({
"firstName": "Jane",
"lastName": "Smith",
"isActive": false
});
encoder.encode_object(&json_obj1);
encoder.encode_object(&json_obj2);
// Fetch token codebook.
let token_book = encoder.token_codebook();
assert!(
!token_book.is_empty(),
"SSP: Token codebook should not be empty after encoding objects"
);
println!("SSP: Token codebook has {} entries.", token_book.len());
// Fetch object (document) codebook.
let object_book = encoder.object_codebook();
assert!(
!object_book.is_empty(),
"SSP: Object codebook should not be empty after encoding objects"
);
println!("SSP: Object codebook has {} entries.", object_book.len());
}
}
#[cfg(test)]
mod fhrr_tests {
use super::*;
use crate::fhrr::FHRR;
use crate::TieBreaker;
use serde_json::json;
/// Test that calling `get_token_vector` with the same token yields identical FHRR hypervectors.
#[test]
fn test_fhrr_token_vector_consistency() {
let mut encoder = ObjectEncoder::<FHRR>::new(1000, TieBreaker::AlwaysPositive);
let token = "exampleToken";
let vec1 = encoder.get_token_vector(token);
let vec2 = encoder.get_token_vector(token);
assert_eq!(
vec1, vec2,
"FHRR token vectors should be consistent across multiple calls"
);
}
/// Test encoding a JSON object using FHRR.
#[test]
fn test_fhrr_encode_object() {
let mut encoder = ObjectEncoder::<FHRR>::new(1000, TieBreaker::AlwaysPositive);
let json_obj = json!({
"firstName": "Alice",
"lastName": "Smith",
"isActive": true
});
let encoded1 = encoder.encode_object(&json_obj);
let retrieved = encoder
.get_encoded_object(&json_obj)
.expect("FHRR: Encoded object should be stored in the codebook");
assert_eq!(
encoded1, *retrieved,
"FHRR: Stored encoding should match computed encoding"
);
let encoded2 = encoder.encode_object(&json_obj);
assert_eq!(
encoded1, encoded2,
"FHRR: Re-encoding the same object should produce the same hypervector"
);
}
/// Test that different JSON objects produce different FHRR encodings.
#[test]
fn test_fhrr_different_objects_have_different_encodings() {
let mut encoder = ObjectEncoder::<FHRR>::new(1000, TieBreaker::AlwaysPositive);
let json_obj1 = json!({
"firstName": "Alice",
"lastName": "Smith",
"isActive": true
});
let json_obj2 = json!({
"firstName": "Bob",
"lastName": "Jones",
"isActive": false
});
let encoded1 = encoder.encode_object(&json_obj1);
let encoded2 = encoder.encode_object(&json_obj2);
assert_ne!(
encoded1, encoded2,
"FHRR: Different JSON objects should have different encodings"
);
}
/// Test that generated entity hypervectors for FHRR are nearly orthogonal and that bindings retrieve the correct pair.
#[test]
fn test_fhrr_entity_binding_retrieval() {
let mut encoder = ObjectEncoder::<FHRR>::new(1000, TieBreaker::AlwaysPositive);
let num_entities = 10;
let mut entity_vectors = Vec::new();
// Generate hypervectors for tokens "entity0", "entity1", ... "entity9"
for i in 0..num_entities {
let token = format!("entity{}", i);
let hv = encoder.get_token_vector(&token);
entity_vectors.push(hv);
}
// Check that all entity hypervectors are nearly orthogonal.
for i in 0..num_entities {
for j in (i + 1)..num_entities {
let sim = entity_vectors[i].cosine_similarity(&entity_vectors[j]);
assert!(
sim.abs() < 0.2,
"FHRR: Entity hypervectors {} and {} are not orthogonal enough: sim = {}",
i,
j,
sim
);
}
}
// Compute and store bindings for all unique pairs.
let mut pair_bindings = Vec::new();
for i in 0..num_entities {
for j in (i + 1)..num_entities {
let binding = entity_vectors[i].bind(&entity_vectors[j]);
pair_bindings.push(((i, j), binding));
}
}
// Choose a particular pair, e.g., (3, 7), for which to "retrieve" the binding.
let query_pair = (3, 7);
let query_binding = entity_vectors[query_pair.0].bind(&entity_vectors[query_pair.1]);
// Compare the query binding to each stored binding.
for &((i, j), ref binding) in &pair_bindings {
let sim = query_binding.cosine_similarity(binding);
if (i, j) == query_pair {
assert!(
(sim - 1.0).abs() < 1e-6,
"FHRR: Correct pair ({},{}) similarity not close to 1: sim = {}",
i,
j,
sim
);
} else {
// Relaxed threshold for non-bound pairs.
assert!(
sim.abs() < 0.12,
"FHRR: Binding for pair ({},{}) has unexpected similarity with query pair: sim = {}",
i,
j,
sim
);
}
}
}
/// Test retrieval of the token and object codebooks for FHRR.
#[test]
fn test_fhrr_fetch_codebooks() {
let mut encoder = ObjectEncoder::<FHRR>::new(1000, TieBreaker::AlwaysPositive);
// Encode a couple of objects.
let json_obj1 = json!({
"firstName": "Alice",
"lastName": "Smith",
"isActive": true
});
let json_obj2 = json!({
"firstName": "Bob",
"lastName": "Jones",
"isActive": false
});
encoder.encode_object(&json_obj1);
encoder.encode_object(&json_obj2);
// Fetch token codebook.
let token_book = encoder.token_codebook();
assert!(
!token_book.is_empty(),
"FHRR: Token codebook should not be empty after encoding objects"
);
println!("FHRR: Token codebook has {} entries.", token_book.len());
// Fetch object (document) codebook.
let object_book = encoder.object_codebook();
assert!(
!object_book.is_empty(),
"FHRR: Object codebook should not be empty after encoding objects"
);
println!("FHRR: Object codebook has {} entries.", object_book.len());
}
}