-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFBClassifier.py
More file actions
58 lines (43 loc) · 1.79 KB
/
Copy pathFBClassifier.py
File metadata and controls
58 lines (43 loc) · 1.79 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
import pickle
import re
import string
import numpy as np
from sklearn.externals import joblib
from sklearn.feature_extraction.text import TfidfVectorizer
re_tok = re.compile(f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])')
FILES_LOCATION = "model/"
def tokenize(s): return re_tok.sub(r' \1 ', s).split()
label_cols = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']
def save_obj(obj, name):
with open(FILES_LOCATION + name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name):
with open(FILES_LOCATION + name + '.pkl', 'rb') as f:
return pickle.load(f)
def load_model(path=FILES_LOCATION):
r = [0] * 6
m = [0] * 6
for i in range(6):
r[i] = np.mat(np.load(path + "r" + str(i) + ".npy"))
m[i] = joblib.load(path + "m" + str(i) + ".sav")
# TF-IDF vectorizer
vec = TfidfVectorizer(ngram_range=(1, 2), tokenizer=tokenize,
min_df=3, max_df=0.9, strip_accents='unicode', use_idf=1,
smooth_idf=1, sublinear_tf=1)
vec._tfidf._idf_diag = load_obj("idf_diag") # sp.spdiags(idfs, diags = 0, m = len(idfs), n = len(idfs))
vec.vocabulary_ = load_obj("vocabulary")
return vec, m, r
vec, m, r = load_model()
def predict(sentence, col=None):
# print(sentence)
if col != None:
if col not in label_cols: raise ValueError("column requested is not in label columns")
i = label_cols.index(col)
pred = vec.transform([sentence])
return (m[i].predict_proba(pred.multiply(r[i]))[0, 1])
else:
result = [0] * 6
pred = vec.transform([sentence])
for i, j in enumerate(label_cols):
result[i] = (j, m[i].predict_proba(pred.multiply(r[i]))[0, 1])
return result