TamilBench / code /v2 /league.py
esscor's picture
v2 results and code
b81a0d5 verified
Raw History Blame Contribute Delete
1.38 kB
import json,glob,os,sys
sys.path.insert(0,"..")
from tokenizers import Tokenizer
from items import ITEMS
en={(d["link"],d["question_number"]):d for d in map(json.loads,open("eng_Latn.jsonl"))}
ta={(d["link"],d["question_number"]):d for d in map(json.loads,open("tam_Taml.jsonl"))}
common=[k for k in ta if k in en]; print("aligned q",len(common))
pe={};pt={}
for k in common: pe[k[0]]=en[k]["flores_passage"]; pt[k[0]]=ta[k]["flores_passage"]
keys=list(pt); print("passages",len(keys))
E=[pe[k] for k in keys]; T=[pt[k] for k in keys]
tw=sum(len(t.split()) for t in T); ew=sum(len(t.split()) for t in E)
tl=[i["tl"] for i in ITEMS]; en50=[i["en"] for i in ITEMS]; ta50=[i["ta"] for i in ITEMS]
rows=[]
for f in sorted(glob.glob("toks/*.json")):
k=os.path.basename(f)[:-5]; t=Tokenizer.from_file(f)
n=lambda L: sum(len(t.encode(x,add_special_tokens=False).ids) for x in L)
ne,nt=n(E),n(T); n50e,n50t,n50l=n(en50),n(ta50),n(tl)
rows.append(dict(model=k,vocab=t.get_vocab_size(),flores_en=ne,flores_ta=nt,ratio=round(nt/ne,2),tok_per_ta_word=round(nt/tw,2),tok_per_en_word=round(ne/ew,2),chars_per_ta_tok=round(sum(map(len,T))/nt,2),tb_en=n50e,tb_ta=n50t,tb_tl=n50l,tl_ratio=round(n50l/n50e,2)))
rows.sort(key=lambda r:r["ratio"])
for r in rows: print(r)
json.dump(dict(passages=len(keys),ta_words=tw,en_words=ew,rows=rows),open("tokenizer_league.json","w"),indent=1)