-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopen_deploy_draft.py
247 lines (211 loc) · 8.7 KB
/
open_deploy_draft.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
"""
Code to process a dataframe into X_test for ML deployment
"""
# import packages
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.model_selection import train_test_split, KFold
from nltk.stem.snowball import SnowballStemmer
from scipy.stats import randint
from io import StringIO
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_selection import chi2
# from IPython.display import display
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import LinearSVC
from sklearn.model_selection import cross_val_score
from sklearn.metrics import confusion_matrix
from sklearn import metrics
from sklearn.feature_selection import chi2
from nltk.corpus import stopwords
#
# if nltk does not work, then run the 3 lines below:
### import nltk
### nltk.download("punkt")
### nltk.download("stopwords")
def predict_faculty(df_in, model, tfidf):
"""
:param df: conditions exist
:return:
"""
X = get_X_and_y(df_in)
features_tfidf = tfidf.transform(X['text_info']).toarray() # don't push all feats in, just the text stack
features = pd.concat([pd.DataFrame(features_tfidf), X.reset_index().drop(columns={'text_info',
'text_info_read',
'text_info_export',
'index'})], axis=1)
y_pred = model.predict(features)
return y_pred
# Here are copies of common functions for the sole purpose of easing imports for cross-platform use
#
# let's prepare the preprocessing
def remove_punctuation(text):
'''a function for removing punctuation'''
import string
# replacing the punctuations with no space,
# which in effect deletes the punctuation marks
translator = str.maketrans('', '', string.punctuation)
# return the text stripped of punctuation marks
return text.translate(translator)
# print(remove_punctuation("""123, hi, ./;[][90-] \][0-*( )] hi man how are you""" )) # powerful and fast
def remove_numbers(text):
import string
translator = str.maketrans('', '', '0123456789')
return text.translate(translator)
# print(remove_numbers('8u1981723 asdh 288 hi hi 2 hi ')) # nice
def remove_stopwords_and_lower(text):
'''a function for removing the stopword'''
# extracting the stopwords from nltk library
sw = stopwords.words('english')
# displaying the stopwords
np.array(sw)
# we know more stuff no one needs at all like 'department' but let's keep them for now
# removing the stop words and lowercasing the selected words
text = [word.lower() for word in text.split() if word.lower() not in sw]
# joining the list of words with space separator
return " ".join(text)
def comma_space_fix(text):
return (text
.replace(": ", ":")
.replace(":", ": ")
.replace("! ", "!")
.replace("!", "! ")
.replace("? ", "?")
.replace("?", "? ")
.replace(", ", ",")
.replace(",", ", ")
.replace(". ", ".")
.replace(".", ". ")
.replace("; ", ";")
.replace(";", "; ")) # this makes both ",x" and ", x" ", x"
# hey! notice that you are cutting off the org info after the first comma but lumping it all together now
# for multi-affil, this may not be what you want as it loses ordering
# however it is OK for now
def remove_common_words_and_lower(text):
# we need to remove vu/dept/amsterdam because it messes up the bigrams
remove_words_org = ['vu', 'amsterdam', 'vrije', 'universiteit', 'free', 'university', 'department', 'of', 'the',
'in',
'and', 'a', '@', 'center', 'centre', 'instituut', 'institute', '&', 'for', '(', ')', 'insitute',
'research']
#
# removing institute is perhaps not the best option, try stuff out : )
# removing the stop words and lowercasing the selected words
text = [word.lower() for word in text.split() if word.lower() not in remove_words_org]
# joining the list of words with space separator
return " ".join(text)
# fill empty nans with empty strings,
# this difference avoids errors in type assertions
def fill_empty(row):
if pd.notnull(row):
return row
else:
return ''
# define encoding/enumeration
def encode_fac(row):
if row == 'Faculty of Science':
id = 0
elif row == 'Faculty of Behavioural and Movement Sciences':
id = 1
elif row == 'medical center':
id = 2
elif row == 'Faculty of Social Sciences':
id = 3
elif row == 'School of Business and Economics':
id = 4
elif row == 'Faculty of Law':
id = 5
elif row == 'Faculty of Humanities':
id = 6
elif row == 'Faculty of Religion and Theology':
id = 7
elif row == 'ACTA':
id = 8
else: # rest
id = 9
return id
def get_X_and_y(df):
def add_space(row):
return row + ' '
df['text_info_1'] = (df
.first_VU_author_raw_organization_info
.apply(fill_empty)
.apply(comma_space_fix)
.apply(remove_punctuation)
.apply(remove_numbers)
.apply(remove_stopwords_and_lower)
.apply(remove_common_words_and_lower)
.apply(add_space))
df['text_info_2'] = (df
.title
.apply(fill_empty)
.apply(comma_space_fix)
.apply(remove_punctuation)
.apply(remove_numbers)
.apply(remove_stopwords_and_lower)
.apply(remove_common_words_and_lower)
.apply(add_space))
df['text_info_3'] = (df
.journal_name
.apply(fill_empty)
.apply(comma_space_fix)
.apply(remove_punctuation)
.apply(remove_numbers)
.apply(remove_stopwords_and_lower)
.apply(remove_common_words_and_lower)
.apply(add_space))
df['text_info_4'] = (df
.abstract_text_clean
.apply(fill_empty)
.apply(comma_space_fix)
.apply(remove_punctuation)
.apply(remove_numbers)
.apply(remove_stopwords_and_lower)
.apply(remove_common_words_and_lower)
.apply(add_space))
# define the features matrix
# notice that for this setting we do not add extra cols
# for example, we could add #authors as a column
# and let the machine learning decide how/if to use that
abstract_down_weight = 3 # hinges on space_fix
#
df['text_info'] = (3 * df['text_info_1']
+ ' '
+ 3 * df['text_info_2'] # title
+ ' '
+ 3 * df['text_info_3'] # journal_name
+ ' '
+ df['text_info_4']) # abstract
df['text_info_read'] = (df['text_info_1']
+ ' || '
+ df['text_info_2']
+ ' || '
+ df['text_info_3']
+ ' || '
+ df['text_info_4']
)
df['text_info_export'] = (
' #ORGVU1 ' +
df['text_info_1']
+ ' #TITLE '
+ df['text_info_2']
+ ' #JNAME '
+ df['text_info_3']
+ ' #ABS '
+ df['text_info_4']
)
for id in np.arange(0, 10):
df['fac_' + str(id)] = df['faculty_(matched)'].apply(encode_fac) == id
# extra feature
df['has_DOI'] = df.apply(lambda x: True if pd.notnull(x.DOI) else False, axis=1)
X = df[
['text_info', 'text_info_read', 'text_info_export', 'has_DOI', 'type_contains_book', 'fac_0', 'fac_1',
'fac_2', 'fac_3', 'fac_4', 'fac_5'
, 'fac_6', 'fac_7', 'fac_8', 'fac_9']]
# add contact to train AR on later and then remove it
return X