-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextcategorization.py
More file actions
194 lines (181 loc) · 5.88 KB
/
textcategorization.py
File metadata and controls
194 lines (181 loc) · 5.88 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
#! python
import os
import sys
from pathlib import Path
import numpy as np
import pandas as pd
# import kerasprocessing
import mypreprocessing
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 = False
fitCorpus = False
fitTrainModel = False
writeCorpus = False
useScikit = True
useScikitMNB = False
useScikitSVM = False
useScikitMLP = True
useKeras = False
verbose = False
headline = None
content = None
probaResult = True
partialTrain = True
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
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()
if writeCorpus:
corpus = None
else:
try:
corpus = pd.read_csv(path + "dataset_final.csv")
except FileNotFoundError:
corpus = None
if corpus is None or writeCorpus:
writeCorpus = True
fitTrainModel = True
corpus = mypreprocessing.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("Headline: ",headline)
vprint("Content: ",content)
if headline is not None and content is not None:
test_str = headline + " " + content
vprint("Testing Result: ",test_str)
scikitprocessing.prepare(corpus, path, write_corpus=writeCorpus, fit_corpus=fitCorpus,
fit_train_model=fitTrainModel, partial=True,
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 isinstance(result, list):
print(result[0])
if headline is not None and content is not None:
dataset = pd.DataFrame(data={'category': result[1], 'headline': [headline], 'content': [content]})
dataset.to_csv(path + 'dataset-all.csv',mode='a',header=False,index=False)
else:
print(result)
if headline is not None and content is not None:
dataset = pd.DataFrame(data={'category': [result], 'headline': [headline], 'content': [content]})
dataset.to_csv(path + 'dataset-all.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)