-
Notifications
You must be signed in to change notification settings - Fork 1
/
OracleExperiment.py
413 lines (331 loc) · 15 KB
/
OracleExperiment.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import fio
import copy
import subprocess
import json
import numpy
import cmd
tmpdir = "../../data/tmp/"
RougeHeader = ['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_Tac(refs, model):
#return the Rouge scores given the reference summary and the models
#write the files
fio.SaveList(model, tmpdir+'model.txt', '\n')
for i, ref in enumerate(refs):
fio.SaveList(ref, tmpdir+'ref%d.txt'%(i+1), '\n')
retcode = subprocess.call(['./get_rouge_tac'], shell=True)
if retcode != 0:
print("Failed!")
exit(-1)
else:
print "Passed!"
row = []
for scorename in RougeNames:
filename = tmpdir + "OUT_"+scorename+".csv"
lines = fio.ReadFile(filename)
try:
scorevalues = lines[1].split(',')
score = scorevalues[1].strip()
row.append(score)
score = scorevalues[2].strip()
row.append(score)
score = scorevalues[3].strip()
row.append(score)
except Exception:
print filename, scorename, lines
return row
def getRouge(ref, model):
#return the Rouge scores given the reference summary and the models
#write the files
fio.SaveList(ref, tmpdir+'ref.txt', '\n')
fio.SaveList(model, tmpdir+'model.txt', '\n')
retcode = subprocess.call(['./get_rouge'], shell=True)
if retcode != 0:
print("Failed!")
exit(-1)
else:
print "Passed!"
row = []
for scorename in RougeNames:
filename = tmpdir + "OUT_"+scorename+".csv"
lines = fio.ReadFile(filename)
try:
scorevalues = lines[1].split(',')
score = scorevalues[1].strip()
row.append(score)
score = scorevalues[2].strip()
row.append(score)
score = scorevalues[3].strip()
row.append(score)
except Exception:
print filename, scorename, lines
return row
def getKey(ref, model):
return "@".join(ref) + ":" + "@".join(model)
def Greedy(oracledir, np, L, metric='R1-F'):
#sheets = range(0,1)
sheets = range(0,12)
RIndex = RougeHeader.index(metric)
assert(RIndex != -1)
for i, sheet in enumerate(sheets):
week = i + 1
#Add a cache to make it faster
Cache = {}
cachefile = oracledir + str(week) + '/' + 'cache.json'
if fio.IsExist(cachefile):
with open(cachefile, 'r') as fin:
Cache = json.load(fin)
#for type in ['POI']:
for type in ['POI', 'MP', 'LP']:
#read TA's summmary
reffile = oracledir + str(week) + '/' + type + '.ref.summary'
lines = fio.ReadFile(reffile)
ref = [line.strip() for line in lines]
#read Phrases
phrasefile = oracledir + str(week) + '/' + type + '.' + str(np) + '.key'
lines = fio.ReadFile(phrasefile)
candidates = [line.strip() for line in lines]
summary = []
Length = 0
maxSum = []
maxScore = 0
Round = 1
Changed = True
while Changed:
Changed = False
for phrase in candidates:
WC = len(phrase.split())
if Length + WC > L: continue
TmpSum = copy.deepcopy(summary)
TmpSum.append(phrase)
#get Rouge Score
cacheKey = getKey(ref, TmpSum)
if cacheKey in Cache:
scores = Cache[cacheKey]
print "Hit"
else:
scores = getRouge(ref, TmpSum)
Cache[cacheKey] = scores
s = float(scores[RIndex])
#s = scores[RIndex]
if s > maxScore:
maxSum = TmpSum
maxScore = s
Changed = True
if Changed:
#write the results
sumfile = oracledir + str(week) + '/' + type + '.' + str(np) + '.L' + str(L) + "." + str(metric) + '.R' + str(Round) +'.summary'
fio.SaveList(maxSum, sumfile, '\r\n')
summary = maxSum
Length = 0
for s in maxSum:
Length = Length + len(s.split())
Round = Round + 1
newCandidates = []
#remove the candidate from the existing summary
for phrase in candidates:
if phrase not in maxSum:
newCandidates.append(phrase)
candidates = newCandidates
with open(cachefile, 'w') as outfile:
json.dump(Cache, outfile, indent=2)
def getOracleRouge(oracledir, np, L, metric, outputdir):
#sheets = range(0,1)
sheets = range(0,12)
body = []
for i, sheet in enumerate(sheets):
week = i + 1
#Add a cache to make it faster
Cache = {}
cachefile = oracledir + str(week) + '/' + 'cache.json'
print cachefile
if fio.IsExist(cachefile):
with open(cachefile, 'r') as fin:
Cache = json.load(fin)
for type in ['POI', 'MP']:#, 'LP'
row = []
row.append(week)
#read TA's summmary
reffile = oracledir + str(week) + '/' + type + '.ref.summary'
lines = fio.ReadFile(reffile)
ref = [line.strip() for line in lines]
Round = 1
while True:
sumfile = oracledir + str(week) + '/' + type + '.' + str(np) + '.L' + str(L) + "." + str(metric) + '.R' + str(Round) +'.summary'
if not fio.IsExist(sumfile): break
Round = Round + 1
Round = Round - 1
sumfile = oracledir + str(week) + '/' + type + '.' + str(np) + '.L' + str(L) + "." + str(metric) + '.R' + str(Round) +'.summary'
lines = fio.ReadFile(sumfile)
TmpSum = [line.strip() for line in lines]
cacheKey = getKey(ref, TmpSum)
if cacheKey in Cache:
scores = Cache[cacheKey]
print "Hit"
else:
print "Miss", cacheKey
print sumfile
scores = getRouge(ref, TmpSum)
Cache[cacheKey] = scores
#exit()
row = row + scores
body.append(row)
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)
fio.WriteMatrix(outputdir + "rouge." + str(np) + '.L' + str(L) + "." + str(metric) + ".txt", body, header)
def getOracleRougeSplit(oracledir, np, L, metric, outputdir):
#sheets = range(0,1)
sheets = range(0,12)
body = []
for i, sheet in enumerate(sheets):
week = i + 1
#Add a cache to make it faster
Cache = {}
cachefile = oracledir + str(week) + '/' + 'cache.json'
print cachefile
if fio.IsExist(cachefile):
with open(cachefile, 'r') as fin:
Cache = json.load(fin)
row = []
for type in ['POI', 'MP', 'LP']:
row.append(week)
#read TA's summmary
reffile = oracledir + str(week) + '/' + type + '.ref.summary'
lines = fio.ReadFile(reffile)
ref = [line.strip() for line in lines]
Round = 1
while True:
sumfile = oracledir + str(week) + '/' + type + '.' + str(np) + '.L' + str(L) + "." + str(metric) + '.R' + str(Round) +'.summary'
if not fio.IsExist(sumfile): break
Round = Round + 1
Round = Round - 1
sumfile = oracledir + str(week) + '/' + type + '.' + str(np) + '.L' + str(L) + "." + str(metric) + '.R' + str(Round) +'.summary'
if fio.IsExist(sumfile):
import os
ssfile = oracledir + str(week) + '/' + type + '.' + str(np) + '.L' + str(L) + ".summary"
cmd = 'cp ' + sumfile + ' ' + ssfile
print cmd
os.system(cmd)
lines = fio.ReadFile(sumfile)
TmpSum = [line.strip() for line in lines]
cacheKey = getKey(ref, TmpSum)
if cacheKey in Cache:
scores = Cache[cacheKey]
print "Hit"
else:
print "Miss", cacheKey
print sumfile
scores = getRouge(ref, TmpSum)
Cache[cacheKey] = scores
#exit()
row = row + scores
else:
row = row + [0]*len(RougeHeader)
body.append(row)
print body
print "RougeHeader", len(RougeHeader)
header = ['week'] + RougeHeader*3
row = []
row.append("average")
print len(header)
for i in range(1, len(header)):
scores = [float(xx[i]) for xx in body]
row.append(numpy.mean(scores))
body.append(row)
fio.WriteMatrix(outputdir + "rouge." + str(np) + '.L' + str(L) + "." + str(metric) + ".txt", body, header)
def TestRouge():
#ref = ["police killed the gunman"]
#S1 = ["police kill the gunman"]
#S2 = ["the gunman kill police"]
S1_1 = ['Unable to finish the problem set before the quiz',
'Unable to understand why R is useful',
'Some questions were not answered',
'The problem set was confusing',
'Did not understand plots in R, or mean and median'
]
S1_3=['There were two fundamental issues with the class. One, the students felt rushed and were unable to complete the required exercises, and two, the teaching assistant was seen as disorganized and rushed.']
S1_4 = ['The recitation was a good time to learn about the many useful features of R, and how to use them, e.g., plotting data sets and finding the mean and median. The questions in the problem set were well-organized to this end. The application to stock prices was a helpful demonstration.']
S1_2 = ['Learned some useful built-in functions of R',
'Practiced programming in R',
'The question types in the problem set were good',
'Application of R to stock values was helpful',
'The organization of the recitation was good']
S2_1=['Unfinished In-Class Questions',
'Knowing when to use R',
'Calculating Means/Medians',
'Using / Creating a Histogram',
'Using Rstudio']
S2_2 = ['Enjoyed Practice Session / In Class Questions',
'Liked learning about capabilities of rstudio',
'Liked Graphical Depictions / Diagrams',
'Liked Learning about Practical Applications of R',
'Enjoyed Calculating R']
S2_3=['Students had trouble knowing when to use R, and would have liked more examples. Some students had trouble using Rstudio. They were also concerned about the questions in the practice session, as they did not get to go over them.']
S2_4 = ['Students seemed to like using rstudio to calculate R, and creating graphics and seeing applications (such as stock values) for R. They liked the practice problems, and would have liked more of them.']
S1_5=['we cannot solve the last two questions of ps if we can solve them before quiz it will be very good ',
'Still not very clear how to use R/what data we were using at all times today when we were discussing R',
'Ps assistant couldn\'t manage her time properly.',
'question 3 in ps 1 was confusing',
'we can plot normal dist when the values are not continous but for it to work we need a lot independent variables , but like how much ?in the question it was 100',
]
S1_6=['things we can do with r',
'Question types',
'I did not think that these questions can be created in lectures.',
'Interesting to combine the R with statistics on stock values',
'In the first part of class, we practised R programming, whuch was easy to understand.',
]
S2_5=['The r experience was a bit Fast and hard to follow and the ps TA made simple questions look like a bit more complicated',
'in r function there can be more examples',
'Still not very clear how to use R/what data we were using at all times today when we were discussing R',
'inferring informations from histograph is a little confusing since it is new for me',
'Rstudio ps was not good I couldn\'t concentrate much',
]
S2_6=['In the second part, we solved questions, which was helpful for us to remember our probability knowledge.',
'Most interesting thing was to learn that R is capable of many things like reading Excel or txt files.',
'Make a result of collection of data, look like normal distribution',
'the graphics in R and importing data to R',
'learning how to calculate in R',
]
body = []
#getRouge(ref, S2)
r1 = getRouge(S1_1, S2_1)
r2 = getRouge(S1_2, S2_2)
r3 = getRouge(S1_3, S2_3)
r4 = getRouge(S1_4, S2_4)
r5 = getRouge(S1_5, S2_5)
r6 = getRouge(S1_6, S2_6)
body.append(r1)
body.append(r2)
body.append(r3)
body.append(r4)
body.append(r5)
body.append(r6)
print body
fio.WriteMatrix('log.txt', body)
if __name__ == '__main__':
oracledir = "../../data/oracle/"
datadir = "../../data/oracle/"
#TestRouge()
# for L in [10, 15, 20, 25, 30, 35, 40, 45, 50]:
# for np in ['syntax', 'chunk']:
# for metric in ['R1-F', 'R2-F', 'RSU4-F']:
# Greedy(oracledir, np, L, metric)
#
# for L in [10, 15, 20, 25, 30, 35, 40, 45, 50]:
# for np in ['syntax', 'chunk']:
# for metric in ['R1-F', 'R2-F', 'RSU4-F']:
# getOracleRouge(oracledir, np, L, metric, datadir)
# for L in [30]:
# for np in ['sentence']:
# for metric in ['R2-F']:
# Greedy(oracledir, np, L, metric)
for L in [30]:
for np in ['sentence']:
for metric in ['R2-F']:
getOracleRougeSplit(oracledir, np, L, metric, datadir)
print "done"