import json import random def winogrande_format(row): array = row["pronoun"] position_idx = json.loads(array)[1][0] # Turn unicode into proper chinese characters sent = str(u"{}".format(row["sent"])) start_idx = 0 for i, tok in enumerate(json.loads(row["toks"])): tok = str(u"{}".format(tok)) cur_start_idx = sent.find(tok) if i == position_idx: break sent = sent[cur_start_idx + len(tok):] start_idx += cur_start_idx + len(tok) # +1 to give room for an optional space row["sentence"] = row["sent"][:start_idx] + row["sent"][start_idx:start_idx+len(tok)+1].replace(tok, "_") + row["sent"][start_idx+len(tok)+1:] sol = json.loads(row["solution"]) cor_answer_idx = random.choice([1, 2]) incor_answer_idx = 2 if cor_answer_idx == 1 else 1 cor_answer = str(u"{}".format(sol[0][0])) if sol[0][-1] == True else str(u"{}".format(sol[1][0])) incor_answer = str(u"{}".format(sol[0][0])) if sol[0][-1] == False else str(u"{}".format(sol[1][0])) row[f"option{cor_answer_idx}"] = cor_answer row[f"option{incor_answer_idx}"] = incor_answer row["answer"] = cor_answer_idx return row # ds = ds.apply(winogrande_format, axis=1)