-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model.py
More file actions
47 lines (42 loc) · 1.39 KB
/
test_model.py
File metadata and controls
47 lines (42 loc) · 1.39 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
import pickle
import string
import numpy as np
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from tensorflow.keras.preprocessing.text import one_hot
from tensorflow.keras.preprocessing.sequence import pad_sequences
# Load the model
with open('review.pkl', 'rb') as f:
model = pickle.load(f)
# Settings (must match training)
DIC_SIZE = 10000
MAXLEN = 953
lem = WordNetLemmatizer()
def clean_text(text):
text = text.lower()
text = ''.join([ch for ch in text if ch not in string.punctuation])
words = text.split()
words = [lem.lemmatize(w) for w in words if w not in stopwords.words('english')]
return ' '.join(words)
# Test reviews
test_reviews = [
"I loved this movie! It was amazing.",
"Terrible film, waste of time.",
"Hated every minute of it.",
"Great acting and story.",
"Awful, boring, complete garbage."
]
print("="*60)
print("MODEL TEST")
print("="*60)
for review in test_reviews:
cleaned = clean_text(review)
encoded = [one_hot(cleaned, DIC_SIZE)]
padded = pad_sequences(encoded, maxlen=MAXLEN, padding='post')
pred = model.predict(padded, verbose=0)[0][0]
sentiment = 'positive' if pred > 0.5 else 'negative'
print(f"Review: {review}")
print(f"Cleaned: {cleaned}")
print(f"Prediction score: {pred:.4f}")
print(f"Sentiment: {sentiment}")
print("-"*60)