Classifying customer opinions as positive or negative using NLP preprocessing and four ML classifiers — with hyperparameter-tuned accuracy up to 81.5%.
This project applies Natural Language Processing to a dataset of 1,000 restaurant reviews, automatically classifying each as positive or negative. The pipeline covers text preprocessing, feature extraction via Bag-of-Words (CountVectorizer), model training, hyperparameter tuning, and live inference on unseen reviews.
| Model | Accuracy (Tuned) | Precision | Recall | |
|---|---|---|---|---|
| Random Forest | 85.5% | 71.0% | Best | |
| Decision Tree | 88.2% | 67.0% | ||
| Multinomial NB | 77.8% | 81.6% | ||
| Logistic Regression | 77.2% | 78.0% |
A reusable function that preprocesses any raw review string and returns a positive/negative prediction using the trained model.
def predict_sentiment(sample_review): # Clean non-alphabetic characters sample_review = re.sub(r'[^a-zA-Z]', ' ', sample_review) sample_review = sample_review.lower() words = sample_review.split() # Remove stopwords + apply stemming words = [w for w in words if w not in set(stopwords.words('english'))] final = ' '.join([ps().stem(w) for w in words]) # Vectorize + predict vec = cv.transform([final]).toarray() return classifier.predict(vec) # 1 = Positive, 0 = Negative