-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtosql.py
285 lines (222 loc) · 9.69 KB
/
tosql.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
#-------------------------------------------------------------------------------
# Name: tosql
#
# Purpose: Generates a wordcloud from written responses to questions about
# the professor, the course, or both, depending on what was
# searched for
#
# Author: Maya Shaked
#
# Created: 02/15/2018
#-------------------------------------------------------------------------------
import pandas as pd
import sqlite3
import aggregate_numerical_data as agg_num
from nltk.corpus import stopwords
import dyadic_partitioning as dy
EVALS_PART_1 = 'evals_json_version_5_part1'
EVALS_PART_2 = 'evals_json_version_5_part2'
SQL_DB_PATH = 'reevaluations.db'
STOPWORDS = stopwords("english") + ['class', 'classes', 'professor', \
'professors' 'course', 'courses', 'ta', 'tas']
def pre_process(sql_db_path, evals_part_1, evals_part_2):
'''
Takes the SQL database path as well as the two json files containing
all the evaluations and adds the numerical scores and sentiment analysis
scores from aggregate_numerical_scores. adds the dyadic partitioning
results. and cleans the dataframe
- sql_db_pth is a string
- evals_part_1 is a string
- evals_part_2 is a string
Returns a database object and a pandas dataframe
'''
db = sqlite3.connect(sql_db_path)
j1 = pd.read_json(evals_part_1, convert_dates = False)
j2 = pd.read_json(evals_part_2, convert_dates = False)
j = pd.concat([j1, j2])
j = j.set_index('unique_id')
#aggregate numerical scores re: tests, instructor, readings, assignments, as
#well as sentiment analysis scores
j = agg_num.add_score_cols(j)
j = dy.go(j, level = 10, lambda_ = 3)
j['year'] = j['year'].fillna(-1).astype(int)
j['section'] = j['section'].fillna(-1).astype(int)
j['course_number'] = j['course_number'].fillna(-1).astype(int)
j['num_responses'] = j['num_responses'].fillna(-1).astype(int)
j['low_time'] = j['low_time'].fillna(-1).astype(float)
j['avg_time'] = j['avg_time'].fillna(-1).astype(float)
j['high_time'] = j['high_time'].fillna(-1).astype(float)
j = j.where(j != -1, None)
return db, j
def gen_courses(j, db):
'''
Takes the evaluations pandas dataframe and a database object
and creates our 'courses' table
- j is a pandas DataFrame
- db is a sqlite3 database object
Does not return anything, but rather creates the 'courses' table
in our SQL database
'''
courses = j[['course', 'course_number', 'dept', 'section', 'term', 'year']]
courses.to_sql('courses', con = db, flavor = 'sqlite', index = True, index_label = 'course_id')
pass
def gen_profs(j, db):
'''
Takes the evaluations pandas dataframe and a database object
and creates our 'profs' table
- j is a pandas DataFrame
- db is a sqlite3 database object
Does not return anything, but rather creates the 'profs' table
in our SQL database
'''
profs = []
for ind, row in j.iterrows():
if type(row['instructors']) == list:
for prof in row['instructors']:
fullname = prof.split(', ')
profs.append([ind, fullname[0], fullname[-1]])
else:
profs.append([ind, None, None])
profs = pd.DataFrame(profs)
profs = profs.rename(columns = {0 : 'course_id', 1: "ln", 2 : "fn"})
profs.to_sql('profs', con = db, flavor = 'sqlite', index = False)
pass
def gen_crosslists(j, db):
'''
Takes the evaluations pandas dataframe and a database object
and creates our 'crosslists' table
- j is a pandas DataFrame
- db is a sqlite3 database object
Does not return anything, but rather creates the 'crosslists' table
in our SQL database
'''
crosslists = []
for ind, row in j.iterrows():
if type(row['identical_courses']) == str:
x = row['identical_courses'].split(', ')
for course in x:
crosslists.append([ind, course])
else:
crosslists.append([ind, None])
crosslists = pd.DataFrame(crosslists)
crosslists = crosslists.rename(columns = {0 : 'course_id', 1 : 'crosslist'})
crosslists.to_sql('crosslists', con = db, flavor = 'sqlite', index = False)
pass
def gen_evals(j, db):
'''
Takes the evaluations pandas dataframe and a database object
and creates our 'evals' table
- j is a pandas DataFrame
- db is a sqlite3 database object
Does not return anything, but rather creates the 'evals' table
in our SQL database
'''
evals = []
for ind, row in j.iterrows():
eval = [ind, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
eval[1] = row['instructor_score']
eval[2] = row['assignments_score']
eval[3] = row['overall_score']
eval[4] = row['tests_score']
eval[5] = row['num_responses']
eval[6] = row['low_time']
eval[7] = row['avg_time']
eval[8] = row['high_time']
if type(row['recommend']) == list:
eval[9] = int(row['recommend'][0])
eval[10] = int(row['recommend'][1])
eval[11] = row['inst_sentiment']
eval[12] = row['course_sentiment']
eval[13] = row['readings_score_col']
if type(row['good_instructor']) == list:
eval[14] = int(row['good_instructor'][0])
eval[15] = int(row['good_instructor'][1])
eval[16] = row['would_recommend_inst']
eval[17] = row['would_recommend']
evals.append(eval)
evals = pd.DataFrame(evals)
evals = evals.rename(columns = {0 : 'course_id', 1 : 'prof_score', 2 : 'ass_score', \
3 : 'over_score' , 4 : 'test_score', 5 : 'num_responses', \
6 : 'low_time', 7 : 'avg_time', 8 : 'high_time', 9 : 'num_recommend', \
10 : 'num_dont_recommend', 11 : 'inst_sentiment', 12 : 'course_sentiment',
13 : 'read_score', 14 : 'good_inst', 15 : 'bad_inst'})
evals.to_sql('evals', con = db, flavor = 'sqlite', index = False)
pass
def gen_text(j, db):
'''
Takes the evaluations pandas dataframe and a database object
and first creates our tentative 'text' table. Then, we join this
tentative 'text' table with our 'profs' table, joining on course_id,
in order to strip professors' first and last names from their evaluation
responses, so as to not have these redundant names in our WordCloud.
Finally, we use this fully cleaned dataframe to create our final 'text'
table
- j is a pandas DataFrame
- db is a sqlite3 database object
Does not return anything, but rather creates the 'text' table
in our SQL database
'''
fulldf = j #we use this later when creating the final version of the table
j = j[['course_responses', 'instructor_responses']]
all_responses = []
for ind, row in j.iterrows():
resps = [ind, None, None]
if type(row['course_responses']) == list and len(row['course_responses']) > 0:
course_resps = ''
for course_resp in row['course_responses']:
course_resps += course_resp + ' '
resps[1] = course_resps
if type(row['instructor_responses']) == list and len(row['instructor_responses']) > 0:
inst_resps = ''
for inst_resp in row['instructor_responses']:
inst_resps += inst_resp + ' '
resps[2] = inst_resps
allresponses.append(resps)
alltext = []
#pre-clean all text responses so we can quickly make a wordcloud later
for [ind, courseresp, instresp] in all_responses:
text = [ind, None, None]
if coureresp != None:
resp = courseresp.lower()
resp = resp.strip('`~!@#$%^&*)(_+=][}{":;><,.?')
resplist = resp.split()
resplist = [x for x in resplist if x not in STOPWORDS]
resp = (' ').join(resplist)
text[1] = resp
if instresp != None:
resp = instresp.lower()
resp = resp.strip('`~!@#$%^&*)(_+=][}{":;><,.?')
resplist = resp.split()
resplist = [x for x in resplist if x not in STOPWORDS]
resp = (' ').join(resplist)
text[2] = resp
alltext.append(text)
alltext = pd.DataFrame(text)
alltext = alltext.rename(columns = {0 : 'course_id', 1 : 'course_resp', 2 : 'inst_resp'})
alltext.to_sql('texttentative', con = db, flavor = 'sqlite', index = False)
#now we begin the process of stripping professor names from the evaluation responses
finalalltext = pd.read_sql_query('SELECT profs.course_id, profs.fn, profs.ln, \
texttentative.course_resp, texttentative.inst_resp FROM profs JOIN texttentative \
ON profs.course_id = texttentative.course_id;', db
for ind, row in toclean.iterrows():
if row['fn'] != None:
row['fn'] = row['fn'].lower()
if row['course_resp'] != None:
row['course_resp'] = row['course_resp'].replace(row['fn'], '')
if row['inst_resp'] != None:
row['inst_resp'] = row['inst_resp'].replace(row['fn'], '')
if row['ln'] != None:
row['ln'] = row['ln'].lower()
if row['course_resp'] != None:
row['course_resp'] = row['course_resp'].replace(row['ln'], '')
if row['inst_resp'] != None:
row['inst_resp'] = row['inst_resp'].replace(row['ln'], '')
finalalltext.tosql('text', con = db, flavor = 'sqlite', index = False)
pd.read_sql_query('DROP TABLE texttentative;', db)
if __name__ == "__main__":
db, j = pre_process(SQL_DB_PATH, EVALS_PART_1, EVALS_PART_2)
gen_courses(j, db)
gen_profs(j, db)
gen_crosslists(j, db)
gen_evals(j, db)
gen_text(j, db)