| |
| 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 a<b else '=' |
| word = 'greater than' if a>b else 'less than' if a<b else 'equal to' |
| return choose([f'{a} {sym} {b}.', f'{a} is {word} {b}.', f'Compare {a} and {b}: {a} {sym} {b}.']), 'comparison' |
|
|
| def gen_order_parity(): |
| n = rng.randint(-1000,1000) |
| if rng.random()<0.5: |
| return choose([f'The number after {n} is {n+1}.', f'The number before {n} is {n-1}.', f'{n-1}, {n}, {n+1}.']), 'number_order' |
| par = 'even' if n % 2 == 0 else 'odd' |
| return f'{n} is {par}.', 'parity' |
|
|
| def gen_place(): |
| n = rng.randint(0,9999); ones=n%10; tens=(n//10)%10; hundreds=(n//100)%10; thousands=(n//1000)%10 |
| return choose([f'In {n}, the ones digit is {ones}.', f'In {n}, the tens digit is {tens}.', f'In {n}, the hundreds digit is {hundreds}.', f'In {n}, the thousands digit is {thousands}.', f'{n} = {thousands} thousands + {hundreds} hundreds + {tens} tens + {ones} ones.']), 'place_value' |
|
|
| def gen_missing(): |
| a = rng.randint(0,100); x = rng.randint(0,100); b = a+x |
| if rng.random()<0.5: |
| return f'Solve x + {a} = {b}. x = {x}. Check: {x} + {a} = {b}.', 'missing_number' |
| return f'Solve {b} - x = {a}. x = {x}. Check: {b} - {x} = {a}.', 'missing_number' |
|
|
| def gen_word(): |
| name=choose(names); obj=choose(objects); a=rng.randint(0,30); b=rng.randint(0,30) |
| if rng.random()<0.55: |
| return f'{name} has {a} {obj} and gets {b} more {obj}. {name} has {a+b} {obj}.', 'word_addition' |
| if b>a: 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: |
| |
| 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) |
| |
| 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}') |
|
|