-
Notifications
You must be signed in to change notification settings - Fork 48
/
evaluate.py
66 lines (55 loc) · 1.8 KB
/
evaluate.py
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
62
63
64
65
66
import json
from os import listdir
from os.path import join
import pandas as pd
from nltk.translate.bleu_score import sentence_bleu, SmoothingFunction
from engine.hoaian import HoaiAn
import numpy as np
import warnings
warnings.filterwarnings("ignore")
def scoring(question, answers):
bot_answer = HoaiAn.reply("local", question)
if len(answers) == 0:
answer = ""
return 0, bot_answer, answer
bleus = [(answer, sentence_bleu([bot_answer.lower().split()], answer.lower().split(), weights=[1, 0, 0, 0])) for
answer in answers]
answer, bleu = max(bleus, key=lambda x: x[1])
return bleu, bot_answer, answer
def evaluate_file(filepath):
print("")
print(filepath)
talks = json.load(open(filepath))
output = {
"talk": 0,
"question": 0
}
scores = []
for talk in talks:
output["talk"] += 1
questions = talk["question"]
for question in questions:
output["question"] += 1
bleu, bot_answer, answer = scoring(question, talk["answer"])
scores.append(bleu)
if bleu < 0.8:
print("\nQuestion :", question)
print("Correct :", answer)
print("Actual :", bot_answer)
print("Bleu : ", bleu)
print()
output["score"] = np.mean(scores)
return output
HoaiAn.reply("local", ":build HoaiAn")
HoaiAn.reply("local", ":reset")
HoaiAn.reply("local", ":trace")
def evaluate(files):
output = [evaluate_file(filepath) for filepath in files]
df = pd.DataFrame(output, columns=["talk", "question", "score"])
print(df)
print("Score:",df["score"].mean())
TEST_FOLDER = "data/hoaian01/raw"
files = []
for file in listdir(TEST_FOLDER):
files.append(join(TEST_FOLDER, file))
evaluate(files)