-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
51 lines (42 loc) · 1.6 KB
/
model.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
import tensorflow as tf
from tensorflow.keras.models import load_model
import pickle, re, nltk
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from tensorflow.keras.preprocessing import sequence
import tweepy
import numpy as np
class Model:
def __init__(self):
self.model = load_model('sentimentmodel4.h5')
self.model._make_predict_function()
with open('tokenizer1.pickle', 'rb') as handle:
self.tokenizer = pickle.load(handle)
self.max_sentence_length = self.model.layers[0].input_shape[1]
self.lemmatizer = WordNetLemmatizer()
def process_text(self, texts):
sentences = []
for s in texts:
replaced = re.sub(r'[^a-zA-z0-9\s]','', s.lower())
#tokenize words
words = word_tokenize(replaced)
#lemmatize words
#lexicon = filtered_sentence
lexicon = [self.lemmatizer.lemmatize(i) for i in words]
sentences.append(lexicon)
t = self.tokenizer.texts_to_sequences(sentences)
p = sequence.pad_sequences(t, maxlen=self.max_sentence_length)
return p
def process_prediction(self, prediction):
if prediction <= 1:
return "Very Negative"
elif prediction <= 1.9:
return "Somewhat Negative"
elif 1.9 <= prediction <= 2.1:
return "Neutral"
elif prediction <= 3:
return "Somewhat Positive"
else:
return "Very Positive"
def predict(self, inputs):
return self.model.predict_classes(inputs)