-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_functions.py
176 lines (130 loc) · 5.15 KB
/
helper_functions.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
import csv
from numpy import *
from data_cleaning import remove_stopwords, comments_to_words, lemmatization, professor_tags
import pandas as pd
from nltk.util import ngrams
from nltk.sentiment.vader import SentimentIntensityAnalyzer
def best_worst_departments(f):
'''
Return the Best & Worst departments based on ratings
'''
csvreader = csv.reader(f)
final_list = list(csvreader)
res_dict = {}
for i in range(len(final_list)):
if i == 0:
continue
if final_list[i][2] not in res_dict:
res_dict[final_list[i][2]] = [float(final_list[i][6])]
else:
res_dict[final_list[i][2]].append(float(final_list[i][6]))
new_res_dict = {}
for i in res_dict:
if len(res_dict[i]) >= 100:
new_res_dict[i] = mean(res_dict[i])
res_list = []
for i in new_res_dict:
res_list.append(new_res_dict[i])
res_list.sort()
best_department = {}
worst_department = {}
for i in range(5):
temp = [k for k,v in new_res_dict.items() if v == res_list[i]]
worst_department[temp[0][:-11]] = res_list[i]
for i in range(1,6):
temp = [k for k,v in new_res_dict.items() if v == res_list[-i]]
best_department[temp[0][:-11]] = res_list[-i]
return best_department, worst_department
def prof_count(df):
'''
Return a dataframe of unique professors & a count of their reviews
'''
df_profs = df.groupby('professor_name')['professor_name'].count()
return df_profs
def dept_count(df):
'''
Return a dataframe of unique departments & a count of their reviews
'''
df_depts = df.groupby('department_name')['department_name'].count()
return df_depts
def add_clean_reviews(df):
'''
Clean the reviews text and add two new columns to DF ~
1. Cleaned Text (including stop words)
2. Deep Cleaned Text
'''
custom_words = ['professor', 'class', 'teacher', 'question']
sentences = df['comments'].values.tolist()
data_words = list(comments_to_words(sentences))
data_nostop_words = remove_stopwords(data_words, custom_words)
lemmatized_data = lemmatization(data_nostop_words, allowed_word_types = ['NOUN', 'ADJ', 'VERB', 'ADV'])
# Lower case + Tokenization only (for word clouds)
clean_reviews = []
for sen in data_words:
clean_sen = ' '.join(word for word in sen)
clean_reviews.append(clean_sen)
df['clean_reviews'] = clean_reviews
clean_reviews_nostop = [ ' '.join(word for word in sen) for sen in data_nostop_words]
df['clean_reviews_nostop'] = clean_reviews_nostop
return df
def concat_prof_reviews():
'''
Group the data by unique professors & concat all their reviews ~ Useful to create word cloud
'''
def f(x):
d = {}
d['star_rating'] = x['star_rating'].max()
d['clean_reviews_concat'] = ' '.join(x['clean_reviews'])
d['clean_reviews_nostop_concat'] = ' '.join(x['clean_reviews_nostop'])
return pd.Series(d, index=['star_rating', 'clean_reviews_concat', 'clean_reviews_nostop_concat'])
return dff.groupby('professor_name').apply(f)
def concat_prof_reviews(dff):
'''
Group the data by unique professors & concat all their reviews ~ Useful to create word cloud
'''
def f(x):
d = {}
d['star_rating'] = x['star_rating'].max()
d['clean_reviews_concat'] = ' '.join(x['clean_reviews'])
d['clean_reviews_nostop_concat'] = ' '.join(x['clean_reviews_nostop'])
return pd.Series(d, index=['star_rating', 'clean_reviews_concat', 'clean_reviews_nostop_concat'])
return dff.groupby('professor_name').apply(f)
def get_ngrams(text, n=2):
'''
For a given text review, get a list of ngrams
'''
text = str(text)
n_grams = ngrams(text.split(), n)
returnVal = []
try:
for grams in n_grams:
returnVal.append('_'.join(grams))
except(RuntimeError):
pass
return ' '.join(returnVal).strip()
def get_sentiment_scores(df):
'''
Add 4 sentiment scores to dataframe : pos, neg, neu & compound score
'''
sid = SentimentIntensityAnalyzer()
df["sentiments"] = df["comments"].apply(lambda review: sid.polarity_scores(str(review)))
df = pd.concat([df.drop(['sentiments'], axis=1), df['sentiments'].apply(pd.Series)], axis=1)
return df
def get_department_sents(df):
'''
Group data by department, calculate Positive, Negative & Neutral % Sentiment of each department
'''
def func(x):
d = {}
pos = len(x[x['Sentiment']=='Positive'])
neu = len(x[x['Sentiment']=='Neutral'])
neg = len(x[x['Sentiment']=='Negative'])
total = pos+neu+neg
d['#Positive'] = pos/total * 100
d['#Neutral'] = neu/total * 100
d['#Negative'] = neg/total * 100
return pd.Series(d, index=['#Positive', '#Neutral', '#Negative'])
df_dept_sent = df.groupby('department_name').apply(func)
df_depts = dept_count(df)
df_dept_sent2 = df_dept_sent.join(df_depts.to_frame('count')).sort_values(by = 'count', ascending=False)
return df_dept_sent2