-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
73 lines (42 loc) · 1.58 KB
/
test.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
import tensorflow as tf
import pickle
import re
# Your input sentence
sentence = ['When stole book in class and the teacher caught me the rest of the class laughed at my attempt ']
print("[INFO]: Loading Classes")
# Load class names
classNames = np.load("./data/class_names.npy")
# Load tokenizer pickle file
print("[INFO]: Loading Tokens")
with open('./data/tokenizer.pickle', 'rb') as handle:
Tokenizer = pickle.load(handle)
# Load model
print("[INFO]: Loading Model")
model = tf.keras.models.load_model("./data/model_final.model")
print("[INFO]: Preprocessing")
# Preprocess Text
MAX_LENGTH = maxlen = 100
def preprocess_text(sen):
# Removing html tags
sentence = remove_tags(sen)
# Remove punctuations and numbers
sentence = re.sub('[^a-zA-Z]', ' ', sentence)
# Single character removal
sentence = re.sub(r"\s+[a-zA-Z]\s+", ' ', sentence)
# Removing multiple spaces
sentence = re.sub(r'\s+', ' ', sentence)
return sentence
TAG_RE = re.compile(r'<[^>]+>')
def remove_tags(text):
return TAG_RE.sub('', text)
# Tokenize and pad sentence
sentence_processed = Tokenizer.texts_to_sequences(sentence)
sentence_processed = np.array(sentence_processed)
sentence_padded = tf.keras.preprocessing.sequence.pad_sequences(sentence_processed, padding='post', maxlen=MAX_LENGTH)
print("""[INFO]: Prediction\n\t{}""".format(sentence[0]))
# Get prediction for sentence
result = model.predict(sentence_padded)
print("-"*20)
# Show prediction
print("[INFO]: Emotion class for given text is: {}".format(classNames[np.argmax(result)]))