PII De-identification for Thai Text with Presidio + WangchanBERTa (AI Experiment)
TL;DR Three minutes, top to bottom: It works. Bridging Presidio’s EntityRecognizer contract to PyThaiNLP’s thainer-v2 engine (WangchanBERTa fine-tuned on Thai NER corpus) gives a usable PII de-identification pipeline for Thai text in ~250 lines of glue code. Final headline numbers (60-case test corpus, strict mode): F1 = 66% overall (P=80%, R=56%) EMAIL, IP, URL, THAI_NATIONAL_ID (with checksum), THAI_PHONE_NUMBER, MONEY: 88-100% F1 PERSON: 57% F1 (80% precision, 44% recall — the free-text bottleneck) The Thai national ID recognizer is the new piece: 13 digits with the official mod-11 checksum algorithm. Distinguishes category prefixes (1-8 valid for citizens, 0/9 reserved) and rejects bad checksums at the recognizer layer. Score 0.95 if checksum valid, 0.5 if just format match. The Presidio default English NER is destructive on Thai text. spaCy’s English NER running on Thai sentences produces many false positives (PERSON on เบอร์โทร, ORG on ลูกค้า). Solution: gate the English NER recognizer behind a “text is mostly non-Thai” check; route Thai text to WangchanBERTa. The interesting finding: a WangchanBERTa recognizer that filters out spans containing common Thai particles (ผม, อยู่, ที่, ฝากเงิน) drops the over-tagging rate by ~60% with no measurable recall loss. The model knows it’s a PERSON, it just doesn’t know where the name stops. Tokenizer drift is real: WangchanBERTa’s tokenizer emits <unk> for OOV characters, and the 5-char <unk> token doesn’t match the 1-2 source chars it represents. Naive offset arithmetic drifts after the first <unk>. Fix: walk the source text alongside the token stream and search for each token’s actual position. Don’t ship a recognizer without a no-PII control case. Case 4 in the test corpus (a weather sentence in Thai) is the most important test — it shows whether your pipeline over-fires. Real PII systems miss more from false positives than from false negatives in production logs. This post walks through the integration step by step, the test corpus, the quantitative evaluation, the checksum-validated Thai national ID, the things I’d do differently in a real production deployment, and the honest list of what still doesn’t work. ...