Abstract
We study the problem of recognizing structured text, i.e. text that follows certain formats, and propose to improve the recognition accuracy of structured text by specifying regular expressions (regexes) for biasing. A biased recognizer recognizes text that matches the specified regexes with significantly improved accuracy, at the cost of a generally small degradation on other text. The biasing is realized by modeling regexes as a Weighted Finite-State Transducer (WFST) and injecting it into the decoder via dynamic replacement. A single hyperparameter controls the biasing strength. The method is useful for recognizing text lines with known formats or containing words from a domain vocabulary. Examples include driver license numbers, drug names in prescriptions, etc. We demonstrate the efficacy of regex biasing on datasets of printed and handwritten structured text and measures its side effects.
Introduction
Standard Optical Character Recognition (OCR) systems often struggle with "structured text"—strings that follow a specific format but contain ambiguous characters. For example, a driver's license number might contain a "1" (one) that looks identical to an "l" (lower-case L), or a "0" (zero) that looks like an "O". While a human uses context to know that a license number field usually contains digits, a standard OCR model might pick the visually "closest" character, leading to errors.
The traditional fix is to fine-tune the model on domain-specific data, but this is expensive and often impossible due to privacy hurdles. This research proposes a way to inject "prior knowledge" about text formats into the system at inference time using regular expressions, without retraining the underlying neural network.
Methodology
The core of the approach leverages the Weighted Finite-State Transducer (WFST) framework. The process involves three main steps:
- Regex to WFST Conversion: User-defined regular expressions (e.g.,
\d{2}/\d{2}/\d{4}) are converted into a Deterministic Finite Automaton (DFA) and then into a WFST ($R$). - Scoring and Biasing: A scoring WFST ($S_\alpha$) is introduced to assign a weight ($\alpha$) to every transition. Composing these ($T_r = S_\alpha \circ R$) creates a "biased" model where a lower $\alpha$ increases the likelihood of the decoder matching the regex.
- Dynamic Replacement: To maintain efficiency, a special non-terminal symbol (
$REGEX) is inserted into the base language model. At runtime, the decoder dynamically swaps this symbol for the specific regex WFST, avoiding the need to rebuild the entire language model.
Experimental Evaluation
The method was evaluated across three distinct scenarios:
- Driver Licenses: 4,422 images of simulated US driver's licenses with poor lighting and low resolution.
- Passport MRZ: 8,040 text lines from Machine Readable Zones on passports, known for high structure but often low-quality scans.
- IAM Dataset: A standard handwriting benchmark used to test "vocabulary biasing" for uncommon names.
Key Results
- Accuracy Gains: On driver license fields, the Word Error Rate (WER) dropped from 5.7% to 3.7% (a 35% relative improvement). For Passport MRZ, the Character Error Rate (CER) was reduced by 36% (8.5% to 5.4%), effectively resolving "0 vs O" and "1 vs I" confusions.
- Vocabulary Biasing: For out-of-vocabulary names in the IAM dataset, the error rate plummeted from 36.0% to 10.3% when biased toward a provided list.
- Efficiency: Regex biasing significantly pruned the search space. In Passport MRZ tests, inference time dropped from 14.6ms to 1.0ms per line.
Conclusion
Regex biasing provides a powerful, zero-shot method for domain adaptation in OCR. By restricting the possible character sequences at the decoding stage, the system achieves higher accuracy and faster inference. While "over-biasing" remains a potential side effect if the biasing strength $\alpha$ is too low, the method offers a flexible and efficient solution for recognizing structured text in sensitive or data-scarce domains.
Summary updated