File size: 1,721 Bytes
bd4d1ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Hitung akurasi jawaban (\boxed{...} vs gtruth_answer). Contoh:
#   python scripts/evaluate_json.py --json_path data/pajak-eval.json --gtruth_answer gtruth_answer --generated_text message_content
import argparse, json, re

def extract_boxed(text):
    i = text.rfind("\\boxed{")
    if i < 0: return None
    j, depth, out = i + 7, 1, []
    while j < len(text) and depth:
        c = text[j]
        if c == "{": depth += 1
        elif c == "}": depth -= 1
        if depth: out.append(c)
        j += 1
    return "".join(out) if depth == 0 else None

def to_num(s):
    m = re.search(r"-?\d[\d.,]*", s.replace("\\,", ""))
    if not m: return None
    g = m.group(0).rstrip(".,")
    if re.fullmatch(r"\d{1,3}(\.\d{3})+(,\d+)?", g): g = g.replace(".", "").replace(",", ".")
    elif re.fullmatch(r"\d{1,3}(,\d{3})+(\.\d+)?", g): g = g.replace(",", "")
    else: g = g.replace(",", ".")
    try: return float(g)
    except ValueError: return None

def norm(s): return re.sub(r"\W+", " ", s.lower()).strip()

parser = argparse.ArgumentParser()
parser.add_argument("--json_path", required=True)
parser.add_argument("--gtruth_answer", default="gtruth_answer")
parser.add_argument("--generated_text", default="message_content")
args = parser.parse_args()

data = json.load(open(args.json_path, encoding="utf-8"))
correct = 0
for d in data:
    pred, gt = extract_boxed(d[args.generated_text]), d[args.gtruth_answer]
    if pred is None: continue
    a, b = to_num(gt), to_num(pred)
    if a is not None and b is not None: correct += abs(a - b) < 0.5
    else: correct += norm(gt) == norm(pred) or norm(gt) in norm(pred) or norm(pred) in norm(gt)
print(f"Answer accuracy: {correct/len(data):.1%} ({correct}/{len(data)})")