forked from alexvlis/extractive-document-summarization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_trial.py
78 lines (60 loc) · 2.98 KB
/
test_trial.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
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
from keras.models import load_model
from preprocessing.dataload import loadTestData
from preprocessing.rouge import Rouge
def dummy_rouge(sentence_arr, summary_arr, alpha=0.5):
return np.random.rand()
def dummy_loadTestData():
testing_data = [ [ np.array(["This sentence is important for doc0." ,
"Such a sentence is irrelevent for doc 0."]),
np.random.rand(2,5,300),
np.array(["This sentence is important for doc0."]) ],
[ np.array(["Lol that sentence is awesome for do1." ,
"No way, this is irrelevent"]),
np.random.rand(2,5,300),
np.array(["Lol that sentence is awesome for do1."]) ] ]
return testing_data
def test(model, testing_data, batch_size = 128, upper_bound = 100, threshold = 1, metric = "ROUGE1"):
"""
Build the actual summaries for test data and evaluate them
To do:
- load the actual x_test (embed test sentences) and y_test (compute rouge score)
Parameters:
testing_data - np.array
ex: [ doc1, doc2, ... , docn]
where doci = [sentences, x_test, summary]
where sentences = np.array of string
x_test = np.array of matrices (embedded sentences)
summaries = np.array of sentences
Returns:
eval - float between 0 and 1.
"""
evals = []
for doc in testing_data:
sentences = doc[0]
#x_test_old = doc[1]
#s1 = x_test_old.shape[0]
#(s3,s4) = x_test_old[0].shape
#x_test = np.random.rand(s1,1,378,s4)
#for i in range(s1) :
# x_test[i] = np.array( [ np.pad(x_test_old[i], ((378-s3,0),(0,0)), 'constant') ] )
true_summary = doc[2]
sentences_num = np.random.randint(len(sentences))
predicted_summary = np.random.choice(sentences.tolist(), sentences_num, replace=False)
# print(predicted_summary)
if metric == "ROUGE1" :
N = 1
elif metric == "ROUGE2":
N = 0
evals.append(dummy_rouge( predicted_summary, true_summary, alpha = N))
return np.mean(evals)
def main():
model = load_model('model.h5')
#testing_data = dummy_loadTestData()
testing_data = loadTestData("./data/DUC2002_Summarization_Documents")
rouge1_score = test(model, testing_data, upper_bound=100, metric = "ROUGE1")
#rouge2_score = test(model, testing_data, upper_bound=5, metric = "ROUGE2")
print("")
print(rouge1_score)
if __name__ == "__main__":
main()