-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
31 lines (23 loc) · 987 Bytes
/
predict.py
File metadata and controls
31 lines (23 loc) · 987 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
27
28
29
30
import numpy as np
from models.logistic_regression import LogisticRegression
from libs.sense_proc import TextPreprocessor
# Step 1: Load saved model and vocab
model = LogisticRegression()
model.load_model("model.pkl")
vocab = np.load("vocab.npy", allow_pickle=True).tolist()
idf_vector = np.load("idf.npy")
# Step 2: Prepare text preprocessor
preprocessor = TextPreprocessor()
preprocessor.vocab = vocab
preprocessor.idf_vector = idf_vector
# Step 3: Get input
input_text = input("Enter text to analyze sentiment: ")
# Step 4: Preprocess and convert to vector
cleaned = preprocessor.preprocess(input_text)
tfidf_vector = preprocessor.tf(cleaned) * preprocessor.idf_vector
tfidf_vector = tfidf_vector.reshape(1, -1) # Reshape to (1, D)
# Step 5: Predict
prob = model.predict_prob(tfidf_vector)[0]
label = model.predict(tfidf_vector)[0]
print(f"[INFO] Sentiment Probability (Positive): {prob:.4f}")
print(f"[INFO] Predicted Sentiment: {'Positive' if label == 1 else 'Negative'}")