Reza2kn's picture
Shenava Rizeh v1.6
c977a3e verified
Raw History Blame Contribute Delete
3.08 kB
"""Compose r16 manifest: replay x2 + Ganjoor sample + Hydrogen commentary; drop any text sharing a 6-gram with benchmark refs."""
import json,re,sys,os,glob,collections,unicodedata,pandas as pd
sys.path[:0]=["/home/rezo/value_study/claude_r15","/home/rezo/value_study/phase2"]
from tzf_io import read_tzf
from tok import Tok
from shenavasanj import fair_text
from rapidfuzz.distance import Levenshtein
B="/home/rezo/value_study/";O=B+"claude_r16"
d=read_tzf(B+"claude_tinyasr/m/en-fa.tzf");cfg=d["cfg"];sh,ls=cfg["shared"],cfg["lang_size"]
T=Tok(d["pieces"],list(range(sh))+list(range(sh+ls,sh+2*ls)))
DIG=str.maketrans("0123456789٠١٢٣٤٥٦٧٨٩","۰۱۲۳۴۵۶۷۸۹۰۱۲۳۴۵۶۷۸۹");HAR=re.compile("[ً-ٰٟۖ-ۭ]")
def norm(t):
t=unicodedata.normalize("NFC",t or "").replace("ي","ی").replace("ك","ک").replace("ى","ی").replace("ة","ه")
t=HAR.sub("",t).translate(DIG).replace(",","،").replace("?","؟").replace(";","؛")
t=re.sub(r"\s*‌\s*","‌",t);t=re.sub(r"\s+"," ",t).strip();return t
def grams(t):
w=fair_text(t).split();return {" ".join(w[i:i+6]) for i in range(len(w)-5)}
BG=set()
for l in open(B+"codex_fullbench/full_manifest.jsonl"): BG|=grams(json.loads(l)["ref"])
st=collections.Counter();hrs=collections.Counter();rows=[]
def add(src,path,dur,text):
t=norm(text)
if not t: st[src,"empty"]+=1;return
if grams(t)&BG: st[src,"bench"]+=1;return
ids=T.encode(t)
if ids is None:
t=" ".join(w for w in t.split() if T.encode(w) is not None);ids=T.encode(t)
if not ids: st[src,"untok"]+=1;return
if len(ids)+5>dur*25: st[src,"too_many_tokens"]+=1;return
rows.append({"audio_filepath":path,"duration":dur,"text":t,"src":src});st[src,"keep"]+=1;hrs[src]+=dur/3600
for r in map(json.loads,open(B+"claude_r15/r15_exp2.jsonl")):
rows.append(r);rows.append(dict(r));hrs["replay_x2"]+=2*r["duration"]/3600
for r in map(json.loads,open(O+"/gj_train.jsonl")): add("ganjoor",r["audio_filepath"],r["duration"],r["text"])
A=pd.read_parquet(B+"claude_gj/audit.parquet")[["shard","row","hyp"]].set_index(["shard","row"])["hyp"].to_dict()
def nc(s): return re.sub(r"[^\w]","",unicodedata.normalize("NFC",s or "").replace("ي","ی").replace("ك","ک").replace("‌",""))
for f in sorted(glob.glob(B+"claude_gj/lab/*.jsonl")):
for x in map(json.loads,open(f)):
p=O+'/caudio/%02d_%05d.flac'%(x['shard'],x['row'])
if not os.path.exists(p): st["comm","noaudio"]+=1;continue
if not x["hyp"] or x.get("finish")!="stop" or not (1<=x["dur"]<=40): st["comm","bad"]+=1;continue
a,b=nc(x["hyp"]),nc(A.get((x["shard"],x["row"])))
if Levenshtein.distance(a,b)/max(1,len(a))>0.4: st["comm","disagree>40"]+=1;continue
add("ganjoor_comm",p,x["dur"],x["hyp"].replace("\n"," "))
print(dict(st));print({k:round(v,1) for k,v in hrs.items()})
import random;random.seed(0);random.shuffle(rows)
with open(O+"/r16_train.jsonl","w") as o:
for r in rows: o.write(json.dumps(r,ensure_ascii=False)+"\n")
print("rows",len(rows),"hours",round(sum(r["duration"] for r in rows)/3600,1))