calculate.py 1.29 KB
Newer Older
Rayyyyy's avatar
Rayyyyy committed
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
49
50
51
52
53
54
55
56
57
58
59
60
61
import json
import re
import os



path="=/output_dir/llamavidnew_subplot_all"
def extract_scores(text):
    # Define the keys to locate in the text
    keys = ["score_accuracy", "score_relevance"]
    scores = []

    for key in keys:
        # Find the index where each key starts
        start_index = text.find(key)
        if start_index == -1:
            continue  # Skip if key is not found

        # Find the start of the number which is after the colon and space
        start_number_index = text.find(":", start_index) + 2
        end_number_index = text.find(",", start_number_index)  # Assuming the number ends before a comma

        # Extract and convert the number to float
        score = float(text[start_number_index:end_number_index])
        scores.append(score)

    return scores



accu=0
rele=0
total=0
file_list=os.listdir(path)



for i in file_list:
    file_path=os.path.join(path,i)
    with open(file_path,"r") as f:
        data=json.load(f)

    # print(file_path)
    text=data[0]["explain"]
    # print(text)
    scores=extract_scores(text)
    # print("score",scores)

    try:
        accu += scores[0]
        rele += scores[1]
    except:
        accu +=0
        rele+=0
  

accu = accu/ len(file_list)
rele = rele/ len(file_list)
total= (accu + rele ) 

print(accu,rele,total)