-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.py
170 lines (146 loc) · 4.53 KB
/
submit.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
import sys
import os
#==========================Read Learner's Infomation=============
learnerEmail = raw_input('Login (Email address): ')
learnerSecret = raw_input('One-time Password (from the assignment page. This is NOT your own account\'s password): ')
#==========================Assignment Word Sense Disambiguation=========
assignmentKey = '6li-kQHtEeaJEA6Jlpo7lQ'
partId1 = 'ttTM5'
partId2 = 'AIcBw'
partIdx = raw_input('Please enter which parts you want to submit: \n1: Part A\n2: Part B\nFor example, type "1 2" will submit part 1 and part 2\n')
#=========================Evaluation====================
import subprocess
def evaluate(files,test_files,baselines,references,scores):
score_total = 0
for i in range(len(files)):
f = files[i]
baseline = baselines[i]
reference = references[i]
test_file = test_files[i]
score = scores[i]
if not os.path.exists(f):
print 'Please save your output file', f, 'under Assignment3 directory.'
continue
# command = "./scorer2 " + f + " " + test_file
command = "scorer2 " + f + " " + test_file
print command
#res = subprocess.check_output(command,shell = True)
try:
res = subprocess.check_output(command,shell = True)
except Exception, e:
res = None
print 'scorer2 failed for',f
sys.exit()
#print res
acc = 0
if res:
try:
acc = float(res.split('\n')[2].split(' ')[2])
except Exception, e:
print 'scorer2 failed for',f
sys.exit()
print 'accuracy',acc,
if acc < baseline:
score_i = 0
elif acc >= reference:
score_i = score
else:
score_i = (score - score*(reference - acc)/(reference - baseline))
score_total += score_i
print 'score',score_i
return score_total
def evaluate_part(partIdx):
if partIdx == 1:
files = ['KNN-English.answer','KNN-Spanish.answer','KNN-Catalan.answer','SVM-English.answer','SVM-Spanish.answer','SVM-Catalan.answer']
#test_files = ['data/English-dev.key data/English.sensemap','data/Spanish-dev.key','data/Catalan-dev.key'] * 2
test_files = ['data/English-dev.key','data/Spanish-dev.key','data/Catalan-dev.key'] * 2
baselines = [0.535,0.684,0.678] * 2
references = [0.550,0.690,0.705,0.605,0.785,0.805]
scores = [10] * 6
raw_score = evaluate(files,test_files,baselines,references,scores)
return raw_score / 60.0
elif partIdx == 2:
files = ['Best-English.answer','Best-Spanish.answer','Best-Catalan.answer']
#test_files = ['data/English-dev.key data/English.sensemap','data/Spanish-dev.key','data/Catalan-dev.key']
test_files = ['data/English-dev.key','data/Spanish-dev.key','data/Catalan-dev.key']
baselines = [0.605,0.785,0.805]
references = [0.650,0.810,0.820]
scores = [20,10,10]
raw_score = evaluate(files,test_files,baselines,references,scores)
return raw_score / 40.0
output1 = '0.0'
output2 = '0.0'
if '1' in partIdx:
output1 = str(evaluate_part(1))
if '2' in partIdx:
output2 = str(evaluate_part(2))
#======================Submit Score========================
cmd = 'curl -X POST -H "Cache-Control: no-cache" -H "Content-Type: application/json" -d '
url = 'https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1'
data = {
"assignmentKey": assignmentKey,
"submitterEmail": learnerEmail,
"secret": learnerSecret,
"parts": {
partId1: {
"output": output1
},
partId2: {
"output": output2
}
}
}
'''
if partIdx == 1:
data = {
"assignmentKey": assignmentKey,
"submitterEmail": learnerEmail,
"secret": learnerSecret,
"parts": {
partId1: {
"output": output
},
partId2: {
},
partId3: {
}
}
}
elif partIdx == 2:
data = {
"assignmentKey": assignmentKey,
"submitterEmail": learnerEmail,
"secret": learnerSecret,
"parts": {
partId1: {
},
partId2: {
"output": output
},
partId3: {
}
}
}
elif partIdx == 3:
data = {
"assignmentKey": assignmentKey,
"submitterEmail": learnerEmail,
"secret": learnerSecret,
"parts": {
partId1: {
},
partId2: {
},
partId3: {
"output": output
}
}
}
else:
print 'Invalid partID'
sys.exit()
'''
curlcmd = cmd + "'" + str(data).replace("'",'"') + "'" + " '" + url + "'"
print curlcmd
print
os.system(curlcmd)