-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcutextcategorization.py
More file actions
238 lines (220 loc) · 7.41 KB
/
cutextcategorization.py
File metadata and controls
238 lines (220 loc) · 7.41 KB
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
#! python
import os
import sys
from pathlib import Path
import numpy as np
import pandas as pd
import pymysql.cursors
import cupreprocessing
import kerasprocessing
import scikitprocessing
path = str(Path(sys.argv[0]).parent) + str(os.sep)
MNB_FILENAME = path + 'mnb_classifier.pkl'
SVM_FILENAME = path + 'svm_classifier.pkl'
MLP_FILENAME = path + 'mlp_classifier.pkl'
CORPUS_VECTOR = path + 'tfidf_vector.pkl'
trainMode = True
fitCorpus = True
fitTrainModel = True
writeCorpus = True
useScikit = True
useScikitMNB = False
useScikitSVM = False
useScikitMLP = True
useCluster = False
useKeras = False
verbose = True
headline = None
content = None
f_pin = None
probaResult = True
partialTrain = True
fetchSQL = False
def vprint(*data):
if verbose:
print(*data)
skipArg = False
for i,s in enumerate(sys.argv[1:]):
if(skipArg):
skipArg = False
continue
if s[:2] == '--':
arg = s[2:]
if arg == 'train':
trainMode = True
elif arg == 'test':
trainMode = False
elif arg == 'scikit':
useScikit = True
elif arg == 'no-scikit':
useScikit = False
elif arg == 'keras':
useKeras = True
elif arg == 'no-keras':
useKeras = False
elif arg == 'proba':
probaResult = True
elif arg == 'no-proba':
probaResult = False
elif arg == 'no-partial':
partialTrain = False
elif arg == 'headline':
headline = sys.argv[i+2]
skipArg = True
elif arg == 'content':
content = sys.argv[i+2]
skipArg = True
elif arg == 'f_pin':
f_pin = sys.argv[i+2]
skipArg = True
if useScikit:
if arg == 'mlp':
useScikitMLP = True
elif arg == 'no-mlp':
useScikitMLP = False
elif arg == 'svm':
useScikitSVM = True
elif arg == 'no-svm':
useScikitSVM = False
elif arg == 'mnb':
useScikitMNB = True
elif arg == 'no-mnb':
useScikitMNB = False
if trainMode:
if arg == 'fit-model':
fitTrainModel = True
elif arg == 'no-fit-model':
fitTrainModel = False
elif arg == 'write-corpus':
writeCorpus = True
elif arg == 'no-write-corpus':
writeCorpus = False
elif arg == 'fit-corpus':
fitCorpus = True
elif arg == 'no-fit-corpus':
fitCorpus = False
elif s[0] == '-':
for arg in s[1:]:
if 't' == arg:
trainMode = True
elif 'T' == arg:
trainMode = False
elif 'C' == arg:
useScikit = True
elif 'c' == arg:
useScikit = False
elif 'K' == arg:
useKeras = True
elif 'k' == arg:
useKeras = False
elif 'v' == arg:
verbose = True
elif 'q' == arg:
verbose = False
if useScikit:
if 'N' == arg:
useScikitMLP = True
elif 'n' == arg:
useScikitMLP = False
if 'S' == arg:
useScikitSVM = True
elif 's' == arg:
useScikitSVM = False
if 'B' == arg:
useScikitMNB = True
elif 'b' == arg:
useScikitMNB = False
if trainMode:
if 'M' == arg:
fitTrainModel = True
elif 'm' == arg:
fitTrainModel = False
elif 'C' == arg:
writeCorpus = True
elif 'c' == arg:
writeCorpus = False
elif 'F' == arg:
fitCorpus = True
elif 'f' == arg:
fitCorpus = False
if not trainMode:
fitCorpus = False
fitTrainModel = False
writeCorpus = False
np.random.seed()
def fSQL():
try:
with open("database.txt") as f:
props = [line.rstrip() for line in f]
# Connect to the database
connection = pymysql.connect(host=props[0],
user=props[1],
password=props[2],
db=props[3])
query = props[4]
data = pd.read_sql(query, connection)
data.rename(columns={'POST_ID':'story_id','F_PIN':'f_pin','TITLE':'title','DESCRIPTION':'description'},inplace=True)
connection.close()
return data
except IndexError as e:
vprint(e)
return None
except:
return None
if writeCorpus:
corpus = None
else:
try:
corpus = pd.read_csv(path+"dataset_final_cu-preprocessing.csv")
except FileNotFoundError:
corpus = None
if corpus is None or writeCorpus:
writeCorpus = True
fitTrainModel = True
partialTrain = False
if fetchSQL:
corpus_raw = fSQL()
corpus = cupreprocessing.write_corpus(path,corpus_raw=corpus_raw)
else:
corpus = cupreprocessing.write_corpus(path, fix_contractions=False)
if (not useScikit) and (not useKeras):
useScikit = True
if (not useScikitMNB) and (not useScikitSVM) and (not useScikitMLP):
useScikitMLP = True
if useScikit:
test_str = None
vprint("Title: ",headline)
vprint("Description: ",content)
vprint("F_Pin ID: ",f_pin)
if f_pin is not None and headline is not None and content is not None:
test_str = f_pin+" "+headline + " " + content
vprint("Testing Result: ",test_str)
if useCluster:
scikitprocessing.prepare_cluster(corpus, corpus_raw, path, write_corpus=writeCorpus, fit_corpus=fitCorpus,
fit_train_model=fitTrainModel, verbose=verbose, new_data=test_str)
else:
scikitprocessing.prepare(corpus, path, write_corpus=writeCorpus, fit_corpus=fitCorpus,
fit_train_model=fitTrainModel, partial=partialTrain,
proba=probaResult, verbose=verbose, new_data=test_str)
if useScikitMNB:
result = scikitprocessing.test_mnb()
if useScikitSVM:
result = scikitprocessing.test_svm()
if useScikitMLP:
result = scikitprocessing.test_mlp()
if useCluster:
result = scikitprocessing.cluster()
if result is not None:
if isinstance(result, list):
print(result[0])
if f_pin is not None and headline is not None and content is not None:
dataset = pd.DataFrame(data={'story_id': result[1],'f_pin':[f_pin], 'title': [headline], 'description': [content]})
dataset.to_csv(path + 'dataset-all-cu.csv',mode='a',header=False,index=False)
else:
print(result)
if f_pin is not None and headline is not None and content is not None:
dataset = pd.DataFrame(data={'story_id': [result],'f_pin':[f_pin], 'title': [headline], 'description': [content]})
dataset.to_csv(path + 'dataset-all-cu.csv',mode='a',header=False,index=False)
if useKeras:
kerasprocessing.exec(corpus, path, write_corpus=writeCorpus, fit_corpus=fitCorpus, fit_train_model=fitTrainModel,
verbose=verbose, new_data=test_str)