Datasets:
Add Turkish Text Normalization (TN/ITN) dataset: 17k rule-based pairs + reproducible pipeline, validation, dataset card
5c0e24f verified Download build_dataset.py from yagmurtuncer/turkish-text-normalization: direct link, hf CLI and curl.
- Browser
- Download file 6 kB
-
https://huggingface.co/datasets/yagmurtuncer/turkish-text-normalization/resolve/main/build_dataset.py
- Command line
-
hf download hf://datasets/yagmurtuncer/turkish-text-normalization/build_dataset.py
-
curl -L -o build_dataset.py https://huggingface.co/datasets/yagmurtuncer/turkish-text-normalization/resolve/main/build_dataset.py
6 kB
| # -*- coding: utf-8 -*- | |
| """ | |
| Turkish Text Normalization (TN/ITN) dataset builder. | |
| Deterministic, rule-based generation of written<->spoken Turkish pairs. | |
| Reproducible: fixed seed. No external data sources. | |
| """ | |
| import csv, json, os, random | |
| SEED = 42 | |
| random.seed(SEED) | |
| ONES = ["", "bir","iki","üç","dört","beş","altı","yedi","sekiz","dokuz"] | |
| TENS = ["", "on","yirmi","otuz","kırk","elli","altmış","yetmiş","seksen","doksan"] | |
| SCALES = ["", "bin", "milyon", "milyar"] | |
| def three_digit(n): # 0..999 -> words | |
| out = [] | |
| h = n // 100 | |
| if h == 1: out.append("yüz") | |
| elif h > 1: out.append(ONES[h] + " yüz") | |
| t = (n % 100) // 10 | |
| if t: out.append(TENS[t]) | |
| o = n % 10 | |
| if o: out.append(ONES[o]) | |
| return " ".join(out) | |
| def num_to_tr(n): # 0..10^12-1 -> Turkish words (cardinal) | |
| if n == 0: return "sıfır" | |
| chunks = [] | |
| x = n | |
| while x > 0: | |
| chunks.append(x % 1000); x //= 1000 | |
| words = [] | |
| for i in range(len(chunks)-1, -1, -1): | |
| c = chunks[i] | |
| if c == 0: continue | |
| if i == 1 and c == 1: | |
| words.append("bin") # "bin", not "bir bin" | |
| else: | |
| words.append(three_digit(c)) | |
| if i > 0: words.append(SCALES[i]) | |
| return " ".join(words) | |
| ORDINAL_LAST = { | |
| "bir":"birinci","iki":"ikinci","üç":"üçüncü","dört":"dördüncü","beş":"beşinci", | |
| "altı":"altıncı","yedi":"yedinci","sekiz":"sekizinci","dokuz":"dokuzuncu", | |
| "on":"onuncu","yirmi":"yirminci","otuz":"otuzuncu","kırk":"kırkıncı","elli":"ellinci", | |
| "altmış":"altmışıncı","yetmiş":"yetmişinci","seksen":"sekseninci","doksan":"doksanıncı", | |
| "yüz":"yüzüncü","bin":"bininci","milyon":"milyonuncu","milyar":"milyarıncı", | |
| } | |
| def ordinal_tr(n): | |
| w = num_to_tr(n).split() | |
| w[-1] = ORDINAL_LAST[w[-1]] | |
| return " ".join(w) | |
| MONTHS = ["","ocak","şubat","mart","nisan","mayıs","haziran","temmuz","ağustos","eylül","ekim","kasım","aralık"] | |
| def dec_to_tr(int_part, frac_str): | |
| return num_to_tr(int_part) + " virgül " + " ".join(num_to_tr(int(d)) if d!="0" else "sıfır" for d in frac_str) \ | |
| if False else num_to_tr(int_part) + " virgül " + num_to_tr(int(frac_str)) if not frac_str.startswith("0") else \ | |
| num_to_tr(int_part) + " virgül " + " ".join("sıfır" if d=="0" else num_to_tr(int(d)) for d in frac_str) | |
| rows = [] | |
| def add(cat, written, spoken): | |
| spoken = " ".join(spoken.split()) | |
| rows.append((cat, written, spoken)) | |
| # 1) Cardinals | |
| seen=set() | |
| for _ in range(9000): | |
| r = random.random() | |
| if r < 0.5: n = random.randint(0, 100) | |
| elif r < 0.8: n = random.randint(101, 9999) | |
| elif r < 0.95: n = random.randint(10000, 999999) | |
| else: n = random.randint(10**6, 10**9) | |
| if n in seen: continue | |
| seen.add(n) | |
| add("cardinal", str(n), num_to_tr(n)) | |
| # 2) Ordinals | |
| seen=set() | |
| for _ in range(3500): | |
| n = random.choice([random.randint(1,100), random.randint(1,9999)]) | |
| if n in seen: continue | |
| seen.add(n) | |
| add("ordinal", f"{n}.", ordinal_tr(n)) | |
| # 3) Decimals | |
| seen=set() | |
| for _ in range(3500): | |
| ip = random.randint(0, 9999) | |
| fl = random.choice([1,1,2,2,2,3]) | |
| fr = "".join(random.choice("0123456789") for _ in range(fl)) | |
| key=(ip,fr) | |
| if key in seen: continue | |
| seen.add(key) | |
| frac_words = " ".join("sıfır" if d=="0" else num_to_tr(int(d)) for d in fr) if fr[0]=="0" else num_to_tr(int(fr)) | |
| add("decimal", f"{ip},{fr}", f"{num_to_tr(ip)} virgül {frac_words}") | |
| # 4) Percentages | |
| seen=set() | |
| for _ in range(2500): | |
| whole = random.random() < 0.7 | |
| if whole: | |
| v = random.randint(0,100); w=f"%{v}"; sp=f"yüzde {num_to_tr(v)}" | |
| else: | |
| ip=random.randint(0,100); fr=random.choice(["5","25","5","75","50"]); w=f"%{ip},{fr}" | |
| frac_words = num_to_tr(int(fr)) | |
| sp=f"yüzde {num_to_tr(ip)} virgül {frac_words}" | |
| if w in seen: continue | |
| seen.add(w); add("percentage", w, sp) | |
| # 5) Currency | |
| CUR = [("TL","lira"),("TL","türk lirası"),("$","dolar"),("€","avro"),("₺","lira")] | |
| seen=set() | |
| for _ in range(3200): | |
| n = random.choice([random.randint(1,999), random.randint(1,99999), random.randint(1,9999)]) | |
| sym,word = random.choice(CUR) | |
| if sym in ("$","€","₺"): w=f"{sym}{n}" | |
| else: w=f"{n} {sym}" | |
| key=(n,sym,word) | |
| if key in seen: continue | |
| seen.add(key); add("currency", w, f"{num_to_tr(n)} {word}") | |
| # 6) Dates DD.MM.YYYY | |
| seen=set() | |
| for _ in range(3200): | |
| d=random.randint(1,28); m=random.randint(1,12); y=random.randint(1900,2035) | |
| w=f"{d:02d}.{m:02d}.{y}" | |
| if w in seen: continue | |
| seen.add(w) | |
| add("date", w, f"{num_to_tr(d)} {MONTHS[m]} {num_to_tr(y)}") | |
| # 7) Times HH:MM | |
| seen=set() | |
| for _ in range(2200): | |
| h=random.randint(0,23); mn=random.randint(0,59) | |
| w=f"{h:02d}:{mn:02d}" | |
| if w in seen: continue | |
| seen.add(w) | |
| if mn==0: sp=f"saat {num_to_tr(h)}" | |
| elif mn==30 and random.random()<0.5: sp=f"saat {num_to_tr(h)} buçuk" | |
| else: sp=f"saat {num_to_tr(h)} {num_to_tr(mn)}" | |
| add("time", w, sp) | |
| # dedup globally on (written, spoken) | |
| uniq = {} | |
| for cat,w,s in rows: | |
| uniq[(w,s)] = (cat,w,s) | |
| data = list(uniq.values()) | |
| random.shuffle(data) | |
| # split 90/10 | |
| cut = int(len(data)*0.9) | |
| train, test = data[:cut], data[cut:] | |
| os.makedirs("data", exist_ok=True) | |
| def dump(path, part): | |
| with open(path,"w",encoding="utf-8",newline="") as f: | |
| wr=csv.writer(f); wr.writerow(["id","category","written","spoken"]) | |
| for i,(cat,w,s) in enumerate(part): wr.writerow([i,cat,w,s]) | |
| dump("data/train.csv", train) | |
| dump("data/test.csv", test) | |
| from collections import Counter | |
| cats = Counter(c for c,_,_ in data) | |
| stats = {"total": len(data), "train": len(train), "test": len(test), | |
| "by_category": dict(sorted(cats.items())), "seed": SEED} | |
| json.dump(stats, open("stats.json","w",encoding="utf-8"), ensure_ascii=False, indent=2) | |
| print(json.dumps(stats, ensure_ascii=False, indent=2)) | |
| print("\n--- ornekler ---") | |
| for cat,w,s in random.sample(data, 14): | |
| print(f"[{cat:11}] {w:16} -> {s}") | |