Downloads · 30 days
151
22% of all-time downloads
thealper2/t5-small-nl-to-regex
t5-small-nl-to-regex is a text generation model from thealper2. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as mit.
google-t5/t5-small fully fine-tuned to translate an English description of a text pattern into a regular expression, on the three DeepRegex corpora (KB13, NL-RX-Synth, NL-RX-Turk).
Downloads · 30 days
151
22% of all-time downloads
All-time downloads
690
Public
Parameters
60.5M
242 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors242 MB · 99%
From the Hugging Face model README
google-t5/t5-small fully fine-tuned to translate an English description of a text pattern into a regular expression, on the three DeepRegex corpora (KB13, NL-RX-Synth, NL-RX-Turk).
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("thealper2/t5-small-nl-to-regex")
model = AutoModelForSeq2SeqLM.from_pretrained("thealper2/t5-small-nl-to-regex")
PREFIX = "translate natural language to regex: "
# T5's SentencePiece vocabulary cannot represent \\ ^ ` { } ~, so the
# model emits them as '#b #c #g #l #r #n'. Decode them back:
CODEC = {"#b": "\\", "#c": "^", "#g": "`", "#l": "{", "#r": "}", "#n": "~"}
def decode_regex(text):
out, i = [], 0
while i < len(text):
pair = text[i:i + 2]
if pair in CODEC:
out.append(CODEC[pair]); i += 2
elif pair == "#h":
out.append("#"); i += 2
else:
out.append(text[i]); i += 1
return "".join(out)
def generate_regex(text):
ids = tokenizer(PREFIX + text, return_tensors="pt")
out = model.generate(**ids, max_length=128, num_beams=4, do_sample=False)
return decode_regex(tokenizer.decode(out[0], skip_special_tokens=True))
generate_regex("lines that contain the word 'dog'")
Outputs follow the DeepRegex regex representation, which is not Python re syntax. It adds two operators:
| Operator | Meaning | Example |
|---|---|---|
~X | complement — matches every string X does not | ~(.*e.*) |
X&Y | intersection — matches strings matched by both | ([A-Z])&([AEIOUaeiou]) |
Matching is whole-line (full-match) semantics. re.compile accepts these strings but reads ~ and & as literal characters, so outputs must be translated before use with Python re.
String literals follow the datasets' own preprocessing: words in quotes are normalised to the placeholders dog, truck, ring, lake in order of appearance. A description mentioning two quoted words yields a regex over dog and truck, which the caller substitutes back.
20824 examples from the three DeepRegex corpora:
| Corpus | Examples | Unique regexes | Description style |
|---|---|---|---|
| KB13 | 824 | 732 | Kushman & Barzilay (2013), human-written |
| NL-RX-Synth | 10000 | 9648 | synthetic descriptions generated from the regex |
| NL-RX-Turk | 10000 | 9648 | Mechanical Turk paraphrases of the NL-RX-Synth regexes |
Split 80/10/10 with seed 42 (train 16659 / validation 2082 / test 2083), stratified by corpus. NL-RX-Synth and NL-RX-Turk have byte-identical targ.txt files, so they are split on a shared permutation of the line index; otherwise every NL-RX-Turk test regex would also be an NL-RX-Synth training target.
127 of 2083 test regexes (6.1%) also occur as a training target; these are genuine repeats in the corpora and were not removed.
Full fine-tuning of all parameters — no LoRA, adapters or quantisation.
| Setting | Value |
|---|---|
| Base model | google-t5/t5-small |
| Parameters | 60,506,624 |
| Task prefix | translate natural language to regex: |
| Learning rate | 0.0003 |
| Weight decay | 0.01 |
| Warmup ratio | 0.05 |
| LR schedule | linear |
| Epochs (max) | 15.0 |
| Effective batch size | 64 |
| Max source / target length | 128 / 128 |
| Precision | bf16 |
| Early stopping patience | 3 epochs on validation exact match |
| Best checkpoint | checkpoint-3915 |
Held-out test split (2083 examples), beam search with num_beams=4, do_sample=False.
\b assertion, so it is reported with its coverage.| Metric | Value |
|---|---|
| Exact match | 66.78% |
| Regex validity | 99.76% |
| Functional accuracy | 78.54% |
| DFA equivalence (exact, 77.39% of examples decidable) | 80.40% |
| Probe agreement (approximate) | 79.98% |
| Character similarity | 0.9016 |
| Baseline: most frequent training regex | 0.00% exact match |
| Corpus | n | Exact match | Functional accuracy | Validity |
|---|---|---|---|---|
| KB13 | 83 | 42.17% | 68.67% | 100.00% |
| NL-RX-Synth | 1000 | 85.90% | 89.70% | 99.90% |
| NL-RX-Turk | 1000 | 49.70% | 68.20% | 99.60% |
re; ~ and & need translation before use.dog, truck, ring, lake), inherited from the datasets' preprocessing. The model does not copy real literals.\b, roughly 20% of the corpus.Datasets from Locascio et al., Neural Generation of Regular Expressions from Natural Language with Minimal Domain Knowledge (EMNLP 2016), and Kushman & Barzilay, Using Semantic Unification to Generate Regular Expressions from Natural Language (NAACL 2013).