-
Notifications
You must be signed in to change notification settings - Fork 1
/
ILP_GetRouge.py
152 lines (118 loc) · 5.41 KB
/
ILP_GetRouge.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import OracleExperiment
import fio
import numpy
import json
tmpdir = "../../data/tmp/"
RougeHeader = ['R1-R', 'R1-P', 'R1-F', 'R2-R', 'R2-P', 'R2-F', 'RSU4-R', 'RSU4-P', 'RSU4-F',]
RougeHeaderSplit = ['R1-R', 'R1-P', 'R1-F', 'R2-R', 'R2-P', 'R2-F', 'RSU4-R', 'RSU4-P', 'RSU4-F','R1-R', 'R1-P', 'R1-F', 'R2-R', 'R2-P', 'R2-F', 'RSU4-R', 'RSU4-P', 'RSU4-F','R1-R', 'R1-P', 'R1-F', 'R2-R', 'R2-P', 'R2-F', 'RSU4-R', 'RSU4-P', 'RSU4-F',]
RougeNames = ['ROUGE-1','ROUGE-2', 'ROUGE-SUX']
def getRouge(datadir, np, L, outputdir, Lambda):
#sheets = range(0,1)
sheets = range(0,12)
body = []
for type in ['POI', 'MP', 'LP']:
for i, sheet in enumerate(sheets):
week = i + 1
Cache = {}
cachefile = datadir + str(week) + '/' + 'cache.json'
print cachefile
if fio.IsExist(cachefile):
with open(cachefile, 'r') as fin:
Cache = json.load(fin)
row = []
row.append(week)
#read TA's summmary
reffile = datadir + str(week) + '/' + type + '.ref.summary'
lines = fio.ReadFile(reffile)
ref = [line.strip() for line in lines]
if Lambda == None:
sumfile = datadir + str(week) + '/' + type + '.' + str(np) + '.L' + str(L) + '.summary'
else:
sumfile = datadir + str(week) + '/' + type + '.' + str(np) + '.L' + str(L) + "." + str(Lambda) + '.summary'
lines = fio.ReadFile(sumfile)
TmpSum = [line.strip() for line in lines]
cacheKey = OracleExperiment.getKey(ref, TmpSum)
if cacheKey in Cache:
scores = Cache[cacheKey]
print "Hit"
else:
print "Miss", cacheKey
print sumfile
scores = OracleExperiment.getRouge(ref, TmpSum)
Cache[cacheKey] = scores
row = row + scores
body.append(row)
with open(cachefile, 'w') as outfile:
json.dump(Cache, outfile, indent=2)
header = ['week'] + RougeHeader
row = []
row.append("average")
for i in range(1, len(header)):
scores = [float(xx[i]) for xx in body]
row.append(numpy.mean(scores))
body.append(row)
if Lambda == None:
fio.WriteMatrix(outputdir + "rouge." + str(np) + '.L' + str(L) + ".txt", body, header)
else:
fio.WriteMatrix(outputdir + "rouge." + str(np) + '.L' + str(L) + "." + str(Lambda) + ".txt", body, header)
def getRougeSplit(datadir, np, L, outputdir, Lambda):
#sheets = range(0,1)
sheets = range(0,12)
body = []
for i, sheet in enumerate(sheets):
week = i + 1
Cache = {}
cachefile = datadir + str(week) + '/' + 'cache.json'
print cachefile
if fio.IsExist(cachefile):
with open(cachefile, 'r') as fin:
Cache = json.load(fin)
row = []
row.append(week)
for type in ['POI', 'MP', 'LP']:
#read TA's summmary
reffile = datadir + str(week) + '/' + type + '.ref.summary'
lines = fio.ReadFile(reffile)
ref = [line.strip() for line in lines]
if Lambda == None:
sumfile = datadir + str(week) + '/' + type + '.' + str(np) + '.L' + str(L) + '.summary'
else:
sumfile = datadir + str(week) + '/' + type + '.' + str(np) + '.L' + str(L) + "." + str(Lambda) + '.summary'
lines = fio.ReadFile(sumfile)
TmpSum = [line.strip() for line in lines]
cacheKey = OracleExperiment.getKey(ref, TmpSum)
if cacheKey in Cache:
scores = Cache[cacheKey]
print "Hit"
else:
print "Miss", cacheKey
print sumfile
scores = OracleExperiment.getRouge(ref, TmpSum)
Cache[cacheKey] = scores
row = row + scores
body.append(row)
with open(cachefile, 'w') as outfile:
json.dump(Cache, outfile, indent=2)
header = ['week'] + RougeHeaderSplit
row = []
row.append("average")
for i in range(1, len(header)):
scores = [float(xx[i]) for xx in body]
row.append(numpy.mean(scores))
body.append(row)
if Lambda == None:
fio.WriteMatrix(outputdir + "rouge." + str(np) + '.L' + str(L) + ".txt", body, header)
else:
fio.WriteMatrix(outputdir + "rouge." + str(np) + '.L' + str(L) + "." + str(Lambda) + ".txt", body, header)
if __name__ == '__main__':
import sys
ilpdir = sys.argv[1]
#ilpdir = "../../data/ILP1_Sentence_MC_Length/"
#ilpdir = "../../data/ILP1_Sentence_MC_Bigram/"
from config import ConfigFile
config = ConfigFile()
for L in [config.get_length_limit()]:
for np in ['sentence']:
getRouge(ilpdir, np, L, ilpdir, Lambda = None)
#getRougeSplit(ilpdir, np, L, ilpdir, Lambda = None)
print "done"