Datasets:
File size: 6,004 Bytes
5c0e24f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | # -*- 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}")
|