-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsentiment_analyzer.py
More file actions
135 lines (105 loc) · 3.97 KB
/
sentiment_analyzer.py
File metadata and controls
135 lines (105 loc) · 3.97 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import pandas as pd
import numpy as np
import re
import pickle
import os
import warnings
warnings.filterwarnings("ignore")
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report
import nltk
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
# Download required NLTK data
try:
nltk.data.find("corpora/stopwords")
except LookupError:
nltk.download("stopwords", quiet=True)
class SentimentAnalyzer:
def __init__(self):
self.vectorizer = TfidfVectorizer(max_features=5000, ngram_range=(1, 2))
self.model = LogisticRegression(
max_iter=1000, random_state=42, class_weight="balanced"
)
self.stemmer = PorterStemmer()
self.stop_words = set(stopwords.words("english"))
def preprocess_text(self, text):
text = str(text).lower()
text = re.sub(r"http\S+|www\S+|https\S+", "", text)
text = re.sub(r"@\w+|#\w+", "", text)
text = re.sub(r"[^a-zA-Z\s]", "", text)
text = re.sub(r"\s+", " ", text).strip()
words = text.split()
words = [
self.stemmer.stem(word)
for word in words
if word not in self.stop_words and len(word) > 2
]
return " ".join(words)
def train(self, data_path):
df = pd.read_csv(data_path, encoding="latin-1")
if "rating" in df.columns:
df["sentiment"] = df["rating"].apply(lambda x: 1 if x >= 4 else 0)
df["text"] = df["verified_reviews"]
else:
df.columns = ["id", "sentiment", "text"]
df["cleaned_text"] = df["text"].apply(self.preprocess_text)
df = df[df["cleaned_text"].str.len() > 0]
X_train, X_test, y_train, y_test = train_test_split(
df["cleaned_text"],
df["sentiment"],
test_size=0.2,
random_state=42,
)
X_train_vec = self.vectorizer.fit_transform(X_train)
X_test_vec = self.vectorizer.transform(X_test)
self.model.fit(X_train_vec, y_train)
y_pred = self.model.predict(X_test_vec)
acc = accuracy_score(y_test, y_pred)
print(f"Model trained with accuracy: {acc:.4f}")
return acc
def predict(self, text):
cleaned = self.preprocess_text(text)
if not cleaned:
return 0, [0.5, 0.5]
vec = self.vectorizer.transform([cleaned])
pred = self.model.predict(vec)[0]
prob = self.model.predict_proba(vec)[0]
return pred, prob
def save_model(self, vectorizer_path, model_path):
os.makedirs("models", exist_ok=True)
with open(vectorizer_path, "wb") as f:
pickle.dump(self.vectorizer, f)
with open(model_path, "wb") as f:
pickle.dump(self.model, f)
def load_model(self, vectorizer_path, model_path):
with open(vectorizer_path, "rb") as f:
self.vectorizer = pickle.load(f)
with open(model_path, "rb") as f:
self.model = pickle.load(f)
# STREAMLIT-SAFE LOADER
def get_analyzer():
analyzer = SentimentAnalyzer()
vectorizer_path = "models/vectorizer.pkl"
model_path = "models/model.pkl"
if not os.path.exists(vectorizer_path) or not os.path.exists(model_path):
print("Model not found. Training...")
analyzer.train("data/amazon_reviews.csv")
analyzer.save_model(vectorizer_path, model_path)
else:
analyzer.load_model(vectorizer_path, model_path)
return analyzer
if __name__ == "__main__":
analyzer = get_analyzer()
samples = [
"I love this product",
"Worst purchase ever",
"Not bad, could be better",
]
for text in samples:
pred, prob = analyzer.predict(text)
label = "Positive" if pred == 1 else "Negative"
confidence = prob[pred] * 100
print(f"{text} → {label} ({confidence:.2f}%)")