datasynth-generators 3.1.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
//! Quality issue labels for ML training and data quality tracking.
//!
//! This module provides labeling structures for tracking data quality issues
//! injected into synthetic data. Labels can be exported for use in training
//! ML models for data quality detection.

use serde::{Deserialize, Serialize};

use datasynth_core::uuid_factory::{DeterministicUuidFactory, GeneratorType};

/// Type of data quality issue.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LabeledIssueType {
    /// Missing value (null, empty, or placeholder)
    MissingValue,
    /// Typo or character error
    Typo,
    /// Format variation from standard
    FormatVariation,
    /// Duplicate record (exact or near)
    Duplicate,
    /// Encoding issue (mojibake, corruption)
    EncodingIssue,
    /// Inconsistent data (e.g., mismatched formats)
    Inconsistency,
    /// Out of range value
    OutOfRange,
    /// Invalid reference (foreign key violation)
    InvalidReference,
}

impl LabeledIssueType {
    /// Get the display name for this issue type.
    pub fn display_name(&self) -> &'static str {
        match self {
            LabeledIssueType::MissingValue => "Missing Value",
            LabeledIssueType::Typo => "Typo",
            LabeledIssueType::FormatVariation => "Format Variation",
            LabeledIssueType::Duplicate => "Duplicate",
            LabeledIssueType::EncodingIssue => "Encoding Issue",
            LabeledIssueType::Inconsistency => "Inconsistency",
            LabeledIssueType::OutOfRange => "Out of Range",
            LabeledIssueType::InvalidReference => "Invalid Reference",
        }
    }

    /// Get the severity level (1-5, with 5 being most severe).
    pub fn default_severity(&self) -> u8 {
        match self {
            LabeledIssueType::MissingValue => 3,
            LabeledIssueType::Typo => 2,
            LabeledIssueType::FormatVariation => 1,
            LabeledIssueType::Duplicate => 4,
            LabeledIssueType::EncodingIssue => 3,
            LabeledIssueType::Inconsistency => 2,
            LabeledIssueType::OutOfRange => 4,
            LabeledIssueType::InvalidReference => 5,
        }
    }
}

/// Subtype providing more detail about the issue.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum QualityIssueSubtype {
    // Missing value subtypes
    NullValue,
    EmptyString,
    Placeholder,
    SystematicMissing,

    // Typo subtypes
    Substitution,
    Transposition,
    Insertion,
    Deletion,
    OcrError,
    Homophone,

    // Format variation subtypes
    DateFormatVariation,
    AmountFormatVariation,
    IdentifierFormatVariation,
    CaseVariation,

    // Duplicate subtypes
    ExactDuplicate,
    NearDuplicate,
    FuzzyDuplicate,

    // Encoding subtypes
    Mojibake,
    HtmlEntityCorruption,
    BomIssue,
    CharacterCorruption,

    // Generic
    Other(String),
}

/// A label describing a data quality issue for ML training.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualityIssueLabel {
    /// Unique identifier for this issue
    pub issue_id: String,
    /// Type of quality issue
    pub issue_type: LabeledIssueType,
    /// More specific subtype
    pub subtype: Option<QualityIssueSubtype>,
    /// ID of the affected document/record
    pub document_id: String,
    /// Name of the affected field
    pub field_name: String,
    /// Original value before modification (if available)
    pub original_value: Option<String>,
    /// Modified/corrupted value (if applicable)
    pub modified_value: Option<String>,
    /// Severity level (1-5)
    pub severity: u8,
    /// Name of the processor that created this issue
    pub processor: String,
    /// Additional metadata
    #[serde(default)]
    pub metadata: std::collections::HashMap<String, String>,
}

impl QualityIssueLabel {
    /// Create a new quality issue label.
    pub fn new(
        issue_type: LabeledIssueType,
        document_id: impl Into<String>,
        field_name: impl Into<String>,
        processor: impl Into<String>,
    ) -> Self {
        let uuid_factory = DeterministicUuidFactory::new(0, GeneratorType::Anomaly);
        Self {
            issue_id: uuid_factory.next().to_string(),
            issue_type,
            subtype: None,
            document_id: document_id.into(),
            field_name: field_name.into(),
            original_value: None,
            modified_value: None,
            severity: issue_type.default_severity(),
            processor: processor.into(),
            metadata: std::collections::HashMap::new(),
        }
    }

    /// Set the subtype.
    pub fn with_subtype(mut self, subtype: QualityIssueSubtype) -> Self {
        self.subtype = Some(subtype);
        self
    }

    /// Set the original value.
    pub fn with_original(mut self, value: impl Into<String>) -> Self {
        self.original_value = Some(value.into());
        self
    }

    /// Set the modified value.
    pub fn with_modified(mut self, value: impl Into<String>) -> Self {
        self.modified_value = Some(value.into());
        self
    }

    /// Set both original and modified values.
    pub fn with_values(mut self, original: impl Into<String>, modified: impl Into<String>) -> Self {
        self.original_value = Some(original.into());
        self.modified_value = Some(modified.into());
        self
    }

    /// Set the severity level.
    pub fn with_severity(mut self, severity: u8) -> Self {
        self.severity = severity.clamp(1, 5);
        self
    }

    /// Add metadata.
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Create a missing value label.
    pub fn missing_value(
        document_id: impl Into<String>,
        field_name: impl Into<String>,
        processor: impl Into<String>,
    ) -> Self {
        Self::new(
            LabeledIssueType::MissingValue,
            document_id,
            field_name,
            processor,
        )
    }

    /// Create a typo label.
    pub fn typo(
        document_id: impl Into<String>,
        field_name: impl Into<String>,
        original: impl Into<String>,
        modified: impl Into<String>,
        processor: impl Into<String>,
    ) -> Self {
        Self::new(LabeledIssueType::Typo, document_id, field_name, processor)
            .with_values(original, modified)
    }

    /// Create a format variation label.
    pub fn format_variation(
        document_id: impl Into<String>,
        field_name: impl Into<String>,
        original: impl Into<String>,
        modified: impl Into<String>,
        processor: impl Into<String>,
    ) -> Self {
        Self::new(
            LabeledIssueType::FormatVariation,
            document_id,
            field_name,
            processor,
        )
        .with_values(original, modified)
    }

    /// Create a duplicate label.
    pub fn duplicate(
        document_id: impl Into<String>,
        original_doc_id: impl Into<String>,
        processor: impl Into<String>,
    ) -> Self {
        Self::new(
            LabeledIssueType::Duplicate,
            document_id,
            "_record",
            processor,
        )
        .with_metadata("original_document_id", original_doc_id)
    }
}

/// Collection of quality issue labels with aggregation methods.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QualityLabels {
    /// All labels in this collection
    pub labels: Vec<QualityIssueLabel>,
}

impl QualityLabels {
    /// Create a new empty label collection.
    pub fn new() -> Self {
        Self { labels: Vec::new() }
    }

    /// Create with pre-allocated capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            labels: Vec::with_capacity(capacity),
        }
    }

    /// Add a label.
    pub fn add(&mut self, label: QualityIssueLabel) {
        self.labels.push(label);
    }

    /// Extend with more labels.
    pub fn extend(&mut self, labels: impl IntoIterator<Item = QualityIssueLabel>) {
        self.labels.extend(labels);
    }

    /// Get total number of labels.
    pub fn len(&self) -> usize {
        self.labels.len()
    }

    /// Check if empty.
    pub fn is_empty(&self) -> bool {
        self.labels.is_empty()
    }

    /// Count labels by type.
    pub fn count_by_type(&self) -> std::collections::HashMap<LabeledIssueType, usize> {
        let mut counts = std::collections::HashMap::new();
        for label in &self.labels {
            *counts.entry(label.issue_type).or_insert(0) += 1;
        }
        counts
    }

    /// Count labels by processor.
    pub fn count_by_processor(&self) -> std::collections::HashMap<String, usize> {
        let mut counts = std::collections::HashMap::new();
        for label in &self.labels {
            *counts.entry(label.processor.clone()).or_insert(0) += 1;
        }
        counts
    }

    /// Get labels for a specific document.
    pub fn for_document(&self, document_id: &str) -> Vec<&QualityIssueLabel> {
        self.labels
            .iter()
            .filter(|l| l.document_id == document_id)
            .collect()
    }

    /// Get labels for a specific field.
    pub fn for_field(&self, field_name: &str) -> Vec<&QualityIssueLabel> {
        self.labels
            .iter()
            .filter(|l| l.field_name == field_name)
            .collect()
    }

    /// Get labels of a specific type.
    pub fn of_type(&self, issue_type: LabeledIssueType) -> Vec<&QualityIssueLabel> {
        self.labels
            .iter()
            .filter(|l| l.issue_type == issue_type)
            .collect()
    }

    /// Get summary statistics.
    pub fn summary(&self) -> QualityLabelSummary {
        let counts = self.count_by_type();
        QualityLabelSummary {
            total_labels: self.labels.len(),
            missing_values: *counts.get(&LabeledIssueType::MissingValue).unwrap_or(&0),
            typos: *counts.get(&LabeledIssueType::Typo).unwrap_or(&0),
            format_variations: *counts.get(&LabeledIssueType::FormatVariation).unwrap_or(&0),
            duplicates: *counts.get(&LabeledIssueType::Duplicate).unwrap_or(&0),
            encoding_issues: *counts.get(&LabeledIssueType::EncodingIssue).unwrap_or(&0),
            unique_documents: self
                .labels
                .iter()
                .map(|l| &l.document_id)
                .collect::<std::collections::HashSet<_>>()
                .len(),
            unique_fields: self
                .labels
                .iter()
                .map(|l| &l.field_name)
                .collect::<std::collections::HashSet<_>>()
                .len(),
        }
    }

    /// Convert to CSV rows.
    pub fn to_csv_rows(&self) -> Vec<Vec<String>> {
        self.labels
            .iter()
            .map(|l| {
                vec![
                    l.issue_id.clone(),
                    format!("{:?}", l.issue_type),
                    l.subtype
                        .as_ref()
                        .map(|s| format!("{s:?}"))
                        .unwrap_or_default(),
                    l.document_id.clone(),
                    l.field_name.clone(),
                    l.original_value.clone().unwrap_or_default(),
                    l.modified_value.clone().unwrap_or_default(),
                    l.severity.to_string(),
                    l.processor.clone(),
                ]
            })
            .collect()
    }

    /// Get CSV header.
    pub fn csv_header() -> Vec<&'static str> {
        vec![
            "issue_id",
            "issue_type",
            "subtype",
            "document_id",
            "field_name",
            "original_value",
            "modified_value",
            "severity",
            "processor",
        ]
    }
}

/// Summary statistics for quality labels.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QualityLabelSummary {
    /// Total number of labels
    pub total_labels: usize,
    /// Number of missing value issues
    pub missing_values: usize,
    /// Number of typo issues
    pub typos: usize,
    /// Number of format variation issues
    pub format_variations: usize,
    /// Number of duplicate issues
    pub duplicates: usize,
    /// Number of encoding issues
    pub encoding_issues: usize,
    /// Number of unique documents affected
    pub unique_documents: usize,
    /// Number of unique fields affected
    pub unique_fields: usize,
}

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

    #[test]
    fn test_label_creation() {
        let label = QualityIssueLabel::new(
            LabeledIssueType::Typo,
            "doc-123",
            "vendor_name",
            "typo_processor",
        )
        .with_values("Acme Corp", "Acne Corp")
        .with_subtype(QualityIssueSubtype::Substitution);

        assert_eq!(label.issue_type, LabeledIssueType::Typo);
        assert_eq!(label.document_id, "doc-123");
        assert_eq!(label.field_name, "vendor_name");
        assert_eq!(label.original_value, Some("Acme Corp".to_string()));
        assert_eq!(label.modified_value, Some("Acne Corp".to_string()));
    }

    #[test]
    fn test_label_helpers() {
        let missing = QualityIssueLabel::missing_value("doc-1", "amount", "missing_processor");
        assert_eq!(missing.issue_type, LabeledIssueType::MissingValue);

        let typo = QualityIssueLabel::typo("doc-2", "name", "John", "Jphn", "typo_processor");
        assert_eq!(typo.issue_type, LabeledIssueType::Typo);
        assert_eq!(typo.original_value, Some("John".to_string()));

        let duplicate = QualityIssueLabel::duplicate("doc-3", "doc-1", "dup_processor");
        assert_eq!(duplicate.issue_type, LabeledIssueType::Duplicate);
    }

    #[test]
    fn test_quality_labels_collection() {
        let mut labels = QualityLabels::new();
        labels.add(QualityIssueLabel::missing_value("doc-1", "field1", "proc1"));
        labels.add(QualityIssueLabel::typo(
            "doc-1", "field2", "a", "b", "proc2",
        ));
        labels.add(QualityIssueLabel::typo(
            "doc-2", "field1", "x", "y", "proc2",
        ));

        assert_eq!(labels.len(), 3);

        let counts = labels.count_by_type();
        assert_eq!(*counts.get(&LabeledIssueType::MissingValue).unwrap(), 1);
        assert_eq!(*counts.get(&LabeledIssueType::Typo).unwrap(), 2);

        let doc1_labels = labels.for_document("doc-1");
        assert_eq!(doc1_labels.len(), 2);
    }

    #[test]
    fn test_summary() {
        let mut labels = QualityLabels::new();
        labels.add(QualityIssueLabel::missing_value("doc-1", "field1", "proc1"));
        labels.add(QualityIssueLabel::typo(
            "doc-1", "field2", "a", "b", "proc2",
        ));
        labels.add(QualityIssueLabel::format_variation(
            "doc-2",
            "date",
            "2024-01-01",
            "01/01/2024",
            "proc3",
        ));

        let summary = labels.summary();
        assert_eq!(summary.total_labels, 3);
        assert_eq!(summary.missing_values, 1);
        assert_eq!(summary.typos, 1);
        assert_eq!(summary.format_variations, 1);
        assert_eq!(summary.unique_documents, 2);
        assert_eq!(summary.unique_fields, 3);
    }

    #[test]
    fn test_csv_export() {
        let mut labels = QualityLabels::new();
        labels.add(QualityIssueLabel::typo(
            "doc-1",
            "name",
            "Test",
            "Tset",
            "typo_proc",
        ));

        let header = QualityLabels::csv_header();
        assert_eq!(header.len(), 9);

        let rows = labels.to_csv_rows();
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].len(), 9);
    }
}