-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval.py
More file actions
48 lines (37 loc) · 1.62 KB
/
Copy patheval.py
File metadata and controls
48 lines (37 loc) · 1.62 KB
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
import argparse
from utils import prepare_answer, read_jsonl, normalize_str, get_list_str
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", type=str,
default="webqsp", help="choose the dataset from {cwq, webqsp, grailqa, simpleqa, webquestions}.")
parser.add_argument("--file_path", type=str,
default="./output/original/lmp_webqsp_llama-3_1hop.jsonl", help="the model output file name.")
args = parser.parse_args()
def match(answer: str, result: str) -> bool:
""" If LLM result matches with the answer. """
answer = [normalize_str(i) for i in answer]
for ans in answer:
if ans in normalize_str(result): # exact match
return True
return False
def reverse_match(answer: str, result: str) -> bool: # e.g question: when ...? answer: 2014 world series, result: 2014
""" If the answer matches with the LLM result. """
answer = [normalize_str(i) for i in answer]
result = [normalize_str(i) for i in get_list_str(result)]
for res in result:
if any([res in i for i in answer]):
return True
return False
answers = prepare_answer(args.dataset)
results = read_jsonl(args.file_path)
hits = []
for result in results:
answer = answers[result['question']]
result = result['result']
if match(answer, result) or reverse_match(answer, result):
hits.append(1)
else:
hits.append(0)
print("# of Correct: {}".format(sum(hits)))
print("# of Wrong: {}".format(len(hits)- sum(hits)))
print("Hit@1: {}".format(sum(hits) / len(hits)))
# print([i for i, v in enumerate(hits) if v == 0])