aprender-core 0.31.2

Next-generation machine learning library in pure Rust
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
//! PII (Personally Identifiable Information) filtering (GH-453)
//!
//! Detects and redacts PII patterns from text data before fine-tuning.
//! Supports: email, phone, SSN, credit card, IP address.

/// Types of PII detected.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum PiiType {
    /// Email address (user@domain.tld)
    Email,
    /// Phone number (US format)
    Phone,
    /// Social Security Number (XXX-XX-XXXX)
    Ssn,
    /// Credit card number (13-19 digits)
    CreditCard,
    /// IPv4 address
    IpAddress,
}

impl PiiType {
    /// Redaction placeholder for this PII type.
    #[must_use]
    pub fn redaction_tag(self) -> &'static str {
        match self {
            Self::Email => "[EMAIL]",
            Self::Phone => "[PHONE]",
            Self::Ssn => "[SSN]",
            Self::CreditCard => "[CREDIT_CARD]",
            Self::IpAddress => "[IP_ADDRESS]",
        }
    }
}

/// A detected PII occurrence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PiiMatch {
    /// Type of PII detected
    pub pii_type: PiiType,
    /// Byte offset start in the original text
    pub start: usize,
    /// Byte offset end in the original text
    pub end: usize,
    /// The matched text
    pub matched: String,
}

/// Scan text for PII patterns. Returns all matches.
#[must_use]
pub fn scan_pii(text: &str) -> Vec<PiiMatch> {
    let mut matches = Vec::new();
    scan_emails(text, &mut matches);
    scan_ssns(text, &mut matches);
    scan_credit_cards(text, &mut matches);
    scan_phones(text, &mut matches);
    scan_ip_addresses(text, &mut matches);
    // Sort by start position, deduplicate overlaps
    matches.sort_by_key(|m| m.start);
    deduplicate_overlapping(&mut matches);
    matches
}

/// Filter (redact) all PII from text. Returns cleaned text.
#[must_use]
pub fn filter_pii(text: &str) -> String {
    let matches = scan_pii(text);
    if matches.is_empty() {
        return text.to_string();
    }
    let mut result = String::with_capacity(text.len());
    let mut last_end = 0;
    for m in &matches {
        if m.start > last_end {
            result.push_str(&text[last_end..m.start]);
        }
        result.push_str(m.pii_type.redaction_tag());
        last_end = m.end;
    }
    if last_end < text.len() {
        result.push_str(&text[last_end..]);
    }
    result
}

/// Filter PII from JSONL data. Each line is parsed as JSON, text fields are scanned.
/// Returns (filtered_lines, total_pii_count).
pub fn filter_pii_jsonl(input: &str) -> (String, usize) {
    let mut output = String::new();
    let mut total_pii = 0;

    for line in input.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        // Parse JSON, filter string values
        if let Ok(mut value) = serde_json::from_str::<serde_json::Value>(line) {
            let count = filter_json_value(&mut value);
            total_pii += count;
            if let Ok(json_str) = serde_json::to_string(&value) {
                output.push_str(&json_str);
                output.push('\n');
            }
        } else {
            // Pass through invalid JSON unchanged
            output.push_str(line);
            output.push('\n');
        }
    }

    (output, total_pii)
}

/// Recursively filter PII from JSON string values. Returns count of PII found.
fn filter_json_value(value: &mut serde_json::Value) -> usize {
    match value {
        serde_json::Value::String(s) => {
            let matches = scan_pii(s);
            let count = matches.len();
            if count > 0 {
                *s = filter_pii(s);
            }
            count
        }
        serde_json::Value::Array(arr) => arr.iter_mut().map(filter_json_value).sum(),
        serde_json::Value::Object(obj) => obj.values_mut().map(filter_json_value).sum(),
        _ => 0,
    }
}

// ── Pattern Scanners ──────────────────────────────────────────

fn scan_emails(text: &str, matches: &mut Vec<PiiMatch>) {
    // Simple email pattern: word@word.word
    let bytes = text.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'@' && i > 0 {
            // Find local part (before @)
            let local_start = find_email_local_start(bytes, i);
            // Find domain part (after @)
            if let Some(domain_end) = find_email_domain_end(bytes, i + 1) {
                if domain_end > i + 1 {
                    let start = local_start;
                    let end = domain_end;
                    let matched = &text[start..end];
                    // Validate: must have at least one dot in domain
                    if matched[matched.find('@').unwrap_or(0) + 1..].contains('.') {
                        matches.push(PiiMatch {
                            pii_type: PiiType::Email,
                            start,
                            end,
                            matched: matched.to_string(),
                        });
                    }
                }
            }
        }
        i += 1;
    }
}

fn find_email_local_start(bytes: &[u8], at_pos: usize) -> usize {
    let mut pos = at_pos;
    while pos > 0 {
        let c = bytes[pos - 1];
        if c.is_ascii_alphanumeric() || c == b'.' || c == b'_' || c == b'+' || c == b'-' {
            pos -= 1;
        } else {
            break;
        }
    }
    pos
}

fn find_email_domain_end(bytes: &[u8], start: usize) -> Option<usize> {
    let mut pos = start;
    while pos < bytes.len() {
        let c = bytes[pos];
        if c.is_ascii_alphanumeric() || c == b'.' || c == b'-' {
            pos += 1;
        } else {
            break;
        }
    }
    // Must have at least 1 char
    if pos > start {
        Some(pos)
    } else {
        None
    }
}

fn scan_ssns(text: &str, matches: &mut Vec<PiiMatch>) {
    // SSN: XXX-XX-XXXX (exactly 3-2-4 digits with dashes)
    let bytes = text.as_bytes();
    let mut i = 0;
    while i + 10 < bytes.len() {
        if is_digit(bytes[i])
            && is_digit(bytes[i + 1])
            && is_digit(bytes[i + 2])
            && bytes[i + 3] == b'-'
            && is_digit(bytes[i + 4])
            && is_digit(bytes[i + 5])
            && bytes[i + 6] == b'-'
            && is_digit(bytes[i + 7])
            && is_digit(bytes[i + 8])
            && is_digit(bytes[i + 9])
            && is_digit(bytes[i + 10])
        {
            // Ensure not part of a longer number
            let before_ok = i == 0 || !is_digit(bytes[i - 1]);
            let after_ok = i + 11 >= bytes.len() || !is_digit(bytes[i + 11]);
            if before_ok && after_ok {
                matches.push(PiiMatch {
                    pii_type: PiiType::Ssn,
                    start: i,
                    end: i + 11,
                    matched: text[i..i + 11].to_string(),
                });
                i += 11;
                continue;
            }
        }
        i += 1;
    }
}

fn scan_credit_cards(text: &str, matches: &mut Vec<PiiMatch>) {
    // Credit cards: 13-19 consecutive digits (possibly separated by spaces/dashes)
    let bytes = text.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if is_digit(bytes[i]) {
            let start = i;
            let mut digits = 0;
            let mut j = i;
            while j < bytes.len() && digits < 20 {
                if is_digit(bytes[j]) {
                    digits += 1;
                    j += 1;
                } else if (bytes[j] == b' ' || bytes[j] == b'-')
                    && digits > 0
                    && j + 1 < bytes.len()
                    && is_digit(bytes[j + 1])
                {
                    j += 1;
                } else {
                    break;
                }
            }
            if digits >= 13 && digits <= 19 {
                // Ensure not part of a longer sequence
                let before_ok = start == 0 || !is_digit(bytes[start - 1]);
                let after_ok = j >= bytes.len() || !is_digit(bytes[j]);
                if before_ok && after_ok {
                    matches.push(PiiMatch {
                        pii_type: PiiType::CreditCard,
                        start,
                        end: j,
                        matched: text[start..j].to_string(),
                    });
                    i = j;
                    continue;
                }
            }
            i = j;
        } else {
            i += 1;
        }
    }
}

fn scan_phones(text: &str, matches: &mut Vec<PiiMatch>) {
    // US phone: (XXX) XXX-XXXX or XXX-XXX-XXXX or +1XXXXXXXXXX
    let bytes = text.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        // Pattern: (XXX) XXX-XXXX
        if bytes[i] == b'('
            && i + 13 < bytes.len()
            && is_digit(bytes[i + 1])
            && is_digit(bytes[i + 2])
            && is_digit(bytes[i + 3])
            && bytes[i + 4] == b')'
            && bytes[i + 5] == b' '
            && is_digit(bytes[i + 6])
            && is_digit(bytes[i + 7])
            && is_digit(bytes[i + 8])
            && bytes[i + 9] == b'-'
            && is_digit(bytes[i + 10])
            && is_digit(bytes[i + 11])
            && is_digit(bytes[i + 12])
            && is_digit(bytes[i + 13])
        {
            matches.push(PiiMatch {
                pii_type: PiiType::Phone,
                start: i,
                end: i + 14,
                matched: text[i..i + 14].to_string(),
            });
            i += 14;
            continue;
        }
        // Pattern: XXX-XXX-XXXX (must not be SSN which is XXX-XX-XXXX)
        if is_digit(bytes[i])
            && i + 11 < bytes.len()
            && is_digit(bytes[i + 1])
            && is_digit(bytes[i + 2])
            && bytes[i + 3] == b'-'
            && is_digit(bytes[i + 4])
            && is_digit(bytes[i + 5])
            && is_digit(bytes[i + 6])
            && bytes[i + 7] == b'-'
            && is_digit(bytes[i + 8])
            && is_digit(bytes[i + 9])
            && is_digit(bytes[i + 10])
            && is_digit(bytes[i + 11])
        {
            let before_ok = i == 0 || !is_digit(bytes[i - 1]);
            let after_ok = i + 12 >= bytes.len() || !is_digit(bytes[i + 12]);
            if before_ok && after_ok {
                matches.push(PiiMatch {
                    pii_type: PiiType::Phone,
                    start: i,
                    end: i + 12,
                    matched: text[i..i + 12].to_string(),
                });
                i += 12;
                continue;
            }
        }
        i += 1;
    }
}

fn scan_ip_addresses(text: &str, matches: &mut Vec<PiiMatch>) {
    // IPv4: X.X.X.X where each octet is 0-255
    let bytes = text.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if is_digit(bytes[i]) {
            if let Some((end, ip)) = try_parse_ipv4(bytes, i) {
                // Validate: not part of a longer number/word
                let before_ok = i == 0 || !is_digit(bytes[i - 1]);
                let after_ok = end >= bytes.len() || !is_digit(bytes[end]);
                if before_ok && after_ok {
                    matches.push(PiiMatch {
                        pii_type: PiiType::IpAddress,
                        start: i,
                        end,
                        matched: ip,
                    });
                    i = end;
                    continue;
                }
            }
        }
        i += 1;
    }
}

fn try_parse_ipv4(bytes: &[u8], start: usize) -> Option<(usize, String)> {
    let mut pos = start;
    let mut octets = 0;
    let mut parts = Vec::new();

    while octets < 4 && pos < bytes.len() {
        let num_start = pos;
        while pos < bytes.len() && is_digit(bytes[pos]) {
            pos += 1;
        }
        let num_str = std::str::from_utf8(&bytes[num_start..pos]).ok()?;
        let num: u16 = num_str.parse().ok()?;
        if num > 255 {
            return None;
        }
        parts.push(num_str.to_string());
        octets += 1;

        if octets < 4 {
            if pos >= bytes.len() || bytes[pos] != b'.' {
                return None;
            }
            pos += 1; // skip dot
        }
    }

    if octets == 4 {
        let ip = parts.join(".");
        Some((pos, ip))
    } else {
        None
    }
}

// ── Helpers ───────────────────────────────────────────────────

fn is_digit(b: u8) -> bool {
    b.is_ascii_digit()
}

fn deduplicate_overlapping(matches: &mut Vec<PiiMatch>) {
    if matches.len() <= 1 {
        return;
    }
    let mut keep = vec![true; matches.len()];
    for i in 1..matches.len() {
        if matches[i].start < matches[i - 1].end {
            // Overlapping — keep the longer match
            if matches[i].end - matches[i].start > matches[i - 1].end - matches[i - 1].start {
                keep[i - 1] = false;
            } else {
                keep[i] = false;
            }
        }
    }
    let mut i = 0;
    matches.retain(|_| {
        let k = keep[i];
        i += 1;
        k
    });
}

use serde_json;

#[cfg(test)]
#[path = "pii_tests.rs"]
mod tests;