datasynth-generators 2.2.0

50+ data generators covering GL, P2P, O2C, S2C, HR, manufacturing, audit, tax, treasury, and ESG
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
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
//! Duplicate record generation for data quality simulation.
//!
//! Simulates realistic duplicate scenarios:
//! - Exact duplicates (complete record duplication)
//! - Near duplicates (minor variations)
//! - Fuzzy duplicates (similar but not identical)
//! - Cross-system duplicates (different identifiers, same entity)

use chrono::{Duration, NaiveDate};
use rand::Rng;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

/// Type of duplicate.
#[derive(Debug, Clone, PartialEq)]
pub enum DuplicateType {
    /// Complete exact duplicate.
    Exact,
    /// Near duplicate with minor variations.
    Near {
        /// Fields that vary.
        varying_fields: Vec<String>,
    },
    /// Fuzzy duplicate with significant but recognizable differences.
    Fuzzy {
        /// Similarity threshold (0.0 - 1.0).
        similarity: f64,
    },
    /// Cross-system duplicate (same entity, different identifiers).
    CrossSystem {
        /// Source system identifier.
        source_system: String,
        /// Target system identifier.
        target_system: String,
    },
}

/// Configuration for duplicate generation.
#[derive(Debug, Clone)]
pub struct DuplicateConfig {
    /// Overall duplicate rate.
    pub duplicate_rate: f64,
    /// Exact duplicate rate (of duplicates).
    pub exact_rate: f64,
    /// Near duplicate rate (of duplicates).
    pub near_rate: f64,
    /// Fuzzy duplicate rate (of duplicates).
    pub fuzzy_rate: f64,
    /// Maximum days between duplicate entries.
    pub max_date_offset_days: i64,
    /// Fields that commonly vary in near duplicates.
    pub varying_fields: Vec<String>,
    /// Amount variance for near duplicates (percentage).
    pub amount_variance: f64,
}

impl Default for DuplicateConfig {
    fn default() -> Self {
        Self {
            duplicate_rate: 0.005, // 0.5% of records get duplicated
            exact_rate: 0.3,       // 30% of duplicates are exact
            near_rate: 0.5,        // 50% are near duplicates
            fuzzy_rate: 0.2,       // 20% are fuzzy
            max_date_offset_days: 5,
            varying_fields: vec![
                "entry_date".to_string(),
                "created_by".to_string(),
                "description".to_string(),
            ],
            amount_variance: 0.01, // 1% variance
        }
    }
}

/// A duplicate record with metadata.
#[derive(Debug, Clone)]
pub struct DuplicateRecord<T: Clone> {
    /// The original record.
    pub original: T,
    /// The duplicate record.
    pub duplicate: T,
    /// Type of duplicate.
    pub duplicate_type: DuplicateType,
    /// Fields that differ.
    pub differing_fields: Vec<String>,
    /// Duplicate ID for tracking.
    pub duplicate_id: String,
}

/// Trait for records that can be duplicated.
pub trait Duplicatable: Clone {
    /// Returns the record's unique identifier.
    fn get_id(&self) -> String;

    /// Sets a new identifier.
    fn set_id(&mut self, id: String);

    /// Gets a field value by name.
    fn get_field(&self, field: &str) -> Option<String>;

    /// Sets a field value by name.
    fn set_field(&mut self, field: &str, value: &str);

    /// Gets the amount (for amount-bearing records).
    fn get_amount(&self) -> Option<Decimal>;

    /// Sets the amount.
    fn set_amount(&mut self, amount: Decimal);

    /// Gets the date.
    fn get_date(&self) -> Option<NaiveDate>;

    /// Sets the date.
    fn set_date(&mut self, date: NaiveDate);
}

/// Duplicate generator.
pub struct DuplicateGenerator {
    config: DuplicateConfig,
    stats: DuplicateStats,
    next_duplicate_id: u64,
}

/// Statistics for duplicate generation.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DuplicateStats {
    /// Total records processed.
    pub total_processed: usize,
    /// Total duplicates created.
    pub total_duplicates: usize,
    /// Exact duplicates.
    pub exact_duplicates: usize,
    /// Near duplicates.
    pub near_duplicates: usize,
    /// Fuzzy duplicates.
    pub fuzzy_duplicates: usize,
    /// Cross-system duplicates.
    pub cross_system_duplicates: usize,
}

impl DuplicateGenerator {
    /// Creates a new duplicate generator.
    pub fn new(config: DuplicateConfig) -> Self {
        Self {
            config,
            stats: DuplicateStats::default(),
            next_duplicate_id: 1,
        }
    }

    /// Determines if a record should be duplicated.
    pub fn should_duplicate<R: Rng>(&self, rng: &mut R) -> bool {
        rng.random::<f64>() < self.config.duplicate_rate
    }

    /// Creates a duplicate of a record.
    pub fn create_duplicate<T: Duplicatable, R: Rng>(
        &mut self,
        record: &T,
        rng: &mut R,
    ) -> DuplicateRecord<T> {
        self.stats.total_processed += 1;
        self.stats.total_duplicates += 1;

        let duplicate_type = self.select_duplicate_type(rng);
        let mut duplicate = record.clone();
        let mut differing_fields = Vec::new();

        // Generate new ID
        let new_id = format!("{}-DUP{}", record.get_id(), self.next_duplicate_id);
        self.next_duplicate_id += 1;
        duplicate.set_id(new_id);
        differing_fields.push("id".to_string());

        match &duplicate_type {
            DuplicateType::Exact => {
                self.stats.exact_duplicates += 1;
                // No other changes needed
            }
            DuplicateType::Near { varying_fields } => {
                self.stats.near_duplicates += 1;
                self.apply_near_duplicate_variations(&mut duplicate, varying_fields, rng);
                differing_fields.extend(varying_fields.clone());
            }
            DuplicateType::Fuzzy { similarity } => {
                self.stats.fuzzy_duplicates += 1;
                let varied = self.apply_fuzzy_variations(&mut duplicate, *similarity, rng);
                differing_fields.extend(varied);
            }
            DuplicateType::CrossSystem {
                source_system: _,
                target_system,
            } => {
                self.stats.cross_system_duplicates += 1;
                // Change system identifier
                if let Some(_current_id) = duplicate.get_field("system_id") {
                    duplicate.set_field("system_id", target_system);
                    differing_fields.push("system_id".to_string());
                }
            }
        }

        let duplicate_id = format!("DUP{:08}", self.stats.total_duplicates);

        DuplicateRecord {
            original: record.clone(),
            duplicate,
            duplicate_type,
            differing_fields,
            duplicate_id,
        }
    }

    /// Selects the type of duplicate to create.
    fn select_duplicate_type<R: Rng>(&self, rng: &mut R) -> DuplicateType {
        let r = rng.random::<f64>();

        if r < self.config.exact_rate {
            DuplicateType::Exact
        } else if r < self.config.exact_rate + self.config.near_rate {
            DuplicateType::Near {
                varying_fields: self.config.varying_fields.clone(),
            }
        } else {
            DuplicateType::Fuzzy {
                similarity: rng.random_range(0.8..0.95),
            }
        }
    }

    /// Applies near-duplicate variations.
    fn apply_near_duplicate_variations<T: Duplicatable, R: Rng>(
        &self,
        record: &mut T,
        varying_fields: &[String],
        rng: &mut R,
    ) {
        for field in varying_fields {
            match field.as_str() {
                "entry_date" | "date" => {
                    if let Some(date) = record.get_date() {
                        let offset = rng.random_range(
                            -self.config.max_date_offset_days..=self.config.max_date_offset_days,
                        );
                        record.set_date(date + Duration::days(offset));
                    }
                }
                "amount" | "debit_amount" | "credit_amount" => {
                    if let Some(amount) = record.get_amount() {
                        let variance = 1.0
                            + rng.random_range(
                                -self.config.amount_variance..self.config.amount_variance,
                            );
                        let new_amount =
                            amount * Decimal::from_f64_retain(variance).unwrap_or(Decimal::ONE);
                        record.set_amount(new_amount.round_dp(2));
                    }
                }
                "description" => {
                    if let Some(desc) = record.get_field("description") {
                        // Add minor variation
                        let variations = [
                            format!("{desc} "),
                            format!(" {desc}"),
                            desc.to_uppercase(),
                            desc.to_lowercase(),
                        ];
                        let variation = &variations[rng.random_range(0..variations.len())];
                        record.set_field("description", variation);
                    }
                }
                _ => {
                    // Generic variation: add whitespace
                    if let Some(value) = record.get_field(field) {
                        record.set_field(field, &format!("{value} "));
                    }
                }
            }
        }
    }

    /// Applies fuzzy variations (more significant changes).
    fn apply_fuzzy_variations<T: Duplicatable, R: Rng>(
        &self,
        record: &mut T,
        similarity: f64,
        rng: &mut R,
    ) -> Vec<String> {
        let mut varied_fields = Vec::new();
        let change_probability = 1.0 - similarity;

        // Amount variation
        if rng.random::<f64>() < change_probability {
            if let Some(amount) = record.get_amount() {
                let variance = 1.0 + rng.random_range(-0.1..0.1); // Up to 10% variation
                let new_amount =
                    amount * Decimal::from_f64_retain(variance).unwrap_or(Decimal::ONE);
                record.set_amount(new_amount.round_dp(2));
                varied_fields.push("amount".to_string());
            }
        }

        // Date variation
        if rng.random::<f64>() < change_probability {
            if let Some(date) = record.get_date() {
                let offset = rng.random_range(-30..=30);
                record.set_date(date + Duration::days(offset));
                varied_fields.push("date".to_string());
            }
        }

        // Description variation
        if rng.random::<f64>() < change_probability {
            if let Some(desc) = record.get_field("description") {
                // Introduce typos or abbreviations
                let abbreviated = abbreviate_text(&desc);
                record.set_field("description", &abbreviated);
                varied_fields.push("description".to_string());
            }
        }

        varied_fields
    }

    /// Returns statistics.
    pub fn stats(&self) -> &DuplicateStats {
        &self.stats
    }

    /// Resets statistics.
    pub fn reset_stats(&mut self) {
        self.stats = DuplicateStats::default();
    }
}

/// Abbreviates text by replacing common words.
fn abbreviate_text(text: &str) -> String {
    let abbreviations = [
        ("Account", "Acct"),
        ("Payment", "Pmt"),
        ("Invoice", "Inv"),
        ("Number", "No"),
        ("Department", "Dept"),
        ("Company", "Co"),
        ("Corporation", "Corp"),
        ("International", "Intl"),
        ("Management", "Mgmt"),
        ("Reference", "Ref"),
    ];

    let mut result = text.to_string();
    for (full, abbr) in abbreviations {
        result = result.replace(full, abbr);
    }
    result
}

/// Detects potential duplicates in a dataset.
pub struct DuplicateDetector {
    /// Similarity threshold for fuzzy matching.
    similarity_threshold: f64,
    /// Fields to compare.
    comparison_fields: Vec<String>,
}

impl DuplicateDetector {
    /// Creates a new duplicate detector.
    pub fn new(similarity_threshold: f64, comparison_fields: Vec<String>) -> Self {
        Self {
            similarity_threshold,
            comparison_fields,
        }
    }

    /// Calculates similarity between two strings (Jaccard similarity).
    pub fn string_similarity(&self, a: &str, b: &str) -> f64 {
        if a == b {
            return 1.0;
        }

        let a_chars: std::collections::HashSet<char> = a.chars().collect();
        let b_chars: std::collections::HashSet<char> = b.chars().collect();

        let intersection = a_chars.intersection(&b_chars).count();
        let union = a_chars.union(&b_chars).count();

        if union == 0 {
            0.0
        } else {
            intersection as f64 / union as f64
        }
    }

    /// Checks if two records are potential duplicates.
    pub fn are_duplicates<T: Duplicatable>(&self, a: &T, b: &T) -> bool {
        let mut total_similarity = 0.0;
        let mut field_count = 0;

        for field in &self.comparison_fields {
            if let (Some(val_a), Some(val_b)) = (a.get_field(field), b.get_field(field)) {
                total_similarity += self.string_similarity(&val_a, &val_b);
                field_count += 1;
            }
        }

        // Also compare amounts if available
        if let (Some(amt_a), Some(amt_b)) = (a.get_amount(), b.get_amount()) {
            let amt_a_f64: f64 = amt_a.try_into().unwrap_or(0.0);
            let amt_b_f64: f64 = amt_b.try_into().unwrap_or(0.0);

            if amt_a_f64.abs() > 0.0 {
                let ratio = (amt_a_f64 - amt_b_f64).abs() / amt_a_f64.abs();
                total_similarity += 1.0 - ratio.min(1.0);
                field_count += 1;
            }
        }

        if field_count == 0 {
            return false;
        }

        let avg_similarity = total_similarity / field_count as f64;
        avg_similarity >= self.similarity_threshold
    }

    /// Finds all duplicate pairs in a collection.
    pub fn find_duplicates<T: Duplicatable>(&self, records: &[T]) -> Vec<(usize, usize, f64)> {
        let mut duplicates = Vec::new();

        for i in 0..records.len() {
            for j in (i + 1)..records.len() {
                if self.are_duplicates(&records[i], &records[j]) {
                    let mut similarity = 0.0;
                    let mut count = 0;

                    for field in &self.comparison_fields {
                        if let (Some(a), Some(b)) =
                            (records[i].get_field(field), records[j].get_field(field))
                        {
                            similarity += self.string_similarity(&a, &b);
                            count += 1;
                        }
                    }

                    if count > 0 {
                        duplicates.push((i, j, similarity / count as f64));
                    }
                }
            }
        }

        duplicates
    }
}

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

    // Simple test struct implementing Duplicatable
    #[derive(Clone)]
    struct TestRecord {
        id: String,
        description: String,
        amount: Decimal,
        date: NaiveDate,
    }

    impl Duplicatable for TestRecord {
        fn get_id(&self) -> String {
            self.id.clone()
        }

        fn set_id(&mut self, id: String) {
            self.id = id;
        }

        fn get_field(&self, field: &str) -> Option<String> {
            match field {
                "description" => Some(self.description.clone()),
                "id" => Some(self.id.clone()),
                _ => None,
            }
        }

        fn set_field(&mut self, field: &str, value: &str) {
            if field == "description" {
                self.description = value.to_string();
            }
        }

        fn get_amount(&self) -> Option<Decimal> {
            Some(self.amount)
        }

        fn set_amount(&mut self, amount: Decimal) {
            self.amount = amount;
        }

        fn get_date(&self) -> Option<NaiveDate> {
            Some(self.date)
        }

        fn set_date(&mut self, date: NaiveDate) {
            self.date = date;
        }
    }

    #[test]
    fn test_duplicate_generation() {
        use rand::SeedableRng;
        use rand_chacha::ChaCha8Rng;
        use rust_decimal_macros::dec;

        let config = DuplicateConfig::default();
        let mut generator = DuplicateGenerator::new(config);
        let mut rng = ChaCha8Rng::seed_from_u64(42);

        let record = TestRecord {
            id: "JE001".to_string(),
            description: "Test Entry".to_string(),
            amount: dec!(1000),
            date: NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(),
        };

        let duplicate = generator.create_duplicate(&record, &mut rng);

        assert_ne!(duplicate.duplicate.get_id(), record.get_id());
        assert_eq!(generator.stats().total_duplicates, 1);
    }

    #[test]
    fn test_string_similarity() {
        let detector = DuplicateDetector::new(0.8, vec!["description".to_string()]);

        assert_eq!(detector.string_similarity("hello", "hello"), 1.0);
        assert!(detector.string_similarity("hello", "helo") > 0.8);
        assert!(detector.string_similarity("abc", "xyz") < 0.5);
    }

    #[test]
    fn test_abbreviate_text() {
        let text = "Account Payment Invoice";
        let abbreviated = abbreviate_text(text);
        assert_eq!(abbreviated, "Acct Pmt Inv");
    }
}