#!/usr/bin/env python3 import json, random, hashlib, time from pathlib import Path OUT = Path('/workspace/agillm_math_numeracy_synth/train.jsonl') META = Path('/workspace/agillm_math_numeracy_synth/metadata.json') README = Path('/workspace/agillm_math_numeracy_synth/README.md') N = 2_000_000 SEED = 731095 rng = random.Random(SEED) objects = ['apples','coins','books','marbles','pencils','blocks','tokens','stickers'] names = ['Ada','Ben','Cara','Dion','Eli','Fay','Gus','Hana'] counts = {} def jwrite(f, text, kind): counts[kind] = counts.get(kind, 0) + 1 f.write(json.dumps({'text': text.strip() + '\n', 'kind': kind}, ensure_ascii=True, separators=(',', ':')) + '\n') def choose(xs): return xs[rng.randrange(len(xs))] def gen_add(big=False): m = 9999 if big else 100 a,b = rng.randint(0,m), rng.randint(0,m); c=a+b return choose([f'{a} + {b} = {c}.', f'{a}+{b}={c}', f'Compute {a} + {b}. Answer: {c}.', f'What is {a} plus {b}? {c}.', f'The sum of {a} and {b} is {c}.']), 'multi_digit_addition' if big else 'addition' def gen_sub(): b,c = rng.randint(0,1000), rng.randint(0,1000); a=b+c return choose([f'{a} - {b} = {c}.', f'{a}-{b}={c}', f'Compute {a} minus {b}. Answer: {c}.', f'What is {a} - {b}? {c}.']), 'subtraction' def gen_mul(): a,b = (rng.randint(0,20), rng.randint(0,20)) if rng.random()<0.75 else (rng.randint(0,99), rng.randint(0,12)) c=a*b return choose([f'{a} * {b} = {c}.', f'{a} times {b} equals {c}.', f'Compute {a} x {b}. Answer: {c}.', f'The product of {a} and {b} is {c}.']), 'multiplication' def gen_div(): b,c = rng.randint(1,20), rng.randint(0,100); a=b*c return choose([f'{a} / {b} = {c}.', f'{a} divided by {b} equals {c}.', f'Compute {a} / {b}. Answer: {c}.']), 'exact_division' def gen_compare(): a,b = rng.randint(-200,200), rng.randint(-200,200) sym = '>' if a>b else '<' if ab else 'less than' if aa: a,b=b,a return f'{name} has {a} {obj} and gives away {b}. {name} has {a-b} {obj} left.', 'word_subtraction' def gen_multistep(): a,b,c = rng.randint(0,50), rng.randint(0,50), rng.randint(0,50) if rng.random()<0.5: r=a+b-c return f'{a} + {b} - {c} = {r}. First {a} + {b} = {a+b}; then {a+b} - {c} = {r}.', 'two_step' r=a*b+c return f'{a} * {b} + {c} = {r}. First {a} * {b} = {a*b}; then {a*b} + {c} = {r}.', 'two_step' def gen_fraction(): d=rng.randint(2,12); n=rng.randint(1,d-1); k=rng.randint(2,10) return f'{n}/{d} = {n*k}/{d*k}. Multiplying numerator and denominator by {k} makes an equivalent fraction.', 'fractions' def gen_money(): cents=rng.randint(0,9999); dollars=cents//100; rem=cents%100 return f'{cents} cents = {dollars} dollars and {rem} cents.', 'money' gens = [gen_add, lambda: gen_add(True), gen_sub, gen_mul, gen_div, gen_compare, gen_order_parity, gen_place, gen_missing, gen_word, gen_multistep, gen_fraction, gen_money] weights = [0.18,0.07,0.14,0.14,0.08,0.08,0.08,0.06,0.07,0.07,0.05,0.04,0.04] def weighted_gen(): total = sum(weights) r=rng.random() * total; acc=0.0 for g,w in zip(gens,weights): acc += w if r <= acc: return g() return gens[-1]() start=time.time(); sha=hashlib.sha256(); rows=0 with OUT.open('w', encoding='utf-8') as f: # Exhaustive core facts first, repeated in clean forms. for a in range(0,101): for b in range(0,101): jwrite(f, f'{a} + {b} = {a+b}.', 'addition_table'); rows+=1 if a >= b: jwrite(f, f'{a} - {b} = {a-b}.', 'subtraction_table'); rows+=1 for a in range(0,21): for b in range(0,21): jwrite(f, f'{a} * {b} = {a*b}.', 'multiplication_table'); rows+=1 if b != 0: jwrite(f, f'{a*b} / {b} = {a}.', 'division_table'); rows+=1 while rows < N: text, kind = weighted_gen() jwrite(f, text, kind); rows += 1 if rows % 250000 == 0: print(f'generated {rows:,}', flush=True) # Hash after close. with OUT.open('rb') as f: for chunk in iter(lambda: f.read(1<<20), b''): sha.update(chunk) meta = {'name':'agillm-math-numeracy-synthetic','rows':rows,'seed':SEED,'sha256':sha.hexdigest(),'counts':counts,'created_unix':int(time.time()),'description':'Synthetic numeracy pretraining JSONL with exact arithmetic facts, comparisons, place value, word problems, and simple step-by-step arithmetic.'} META.write_text(json.dumps(meta, indent=2, sort_keys=True) + '\n') README.write_text('''---\nlicense: apache-2.0\ntags:\n- synthetic\n- math\n- numeracy\n- pretraining\n---\n\n# AGILLM Math Numeracy Synthetic\n\nSynthetic plain-text numeracy corpus for AGILLM pretraining. Each JSONL row has `text` and `kind`.\n\nCoverage includes addition/subtraction/multiplication/division facts, multi-digit arithmetic, comparisons, order/parity, place value, missing-number equations, simple word problems, fractions, money, and two-step arithmetic.\n\nGenerated deterministically with `generate_math_numeracy.py`.\n''') print(json.dumps(meta, indent=2, sort_keys=True)) print(f'elapsed_sec={time.time()-start:.1f}')