-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment_classifier.py
More file actions
26 lines (22 loc) · 936 Bytes
/
sentiment_classifier.py
File metadata and controls
26 lines (22 loc) · 936 Bytes
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
from sklearn.externals import joblib
class SentimentClassifier(object):
def __init__(self):
self.model = joblib.load("class.pickle")
self.vectorizer = joblib.load("vectorizer.pickle")
self.classes_dict = {0: "негативный", 1: "позитивный", -1: "prediction error"}
def predict_text(self, text):
try:
text = text.replace('\n', ' ')
text = text.replace('\r', ' ')
text = text.replace(',', '')
text = text.replace('.', '')
text = text.replace('!', '')
vectorized = self.vectorizer.transform([text])
return self.model.predict(vectorized)
except:
print("prediction error")
return -1
def get_prediction_message(self, text):
prediction = self.predict_text(text)
class_prediction = prediction[0]
return self.classes_dict[class_prediction]