Ttt37's picture
Upload README.md with huggingface_hub
ad383d7 verified
|
Raw History Blame Contribute Delete
1.36 kB
metadata
language:
  - en
tags:
  - sentiment-analysis
  - nlp
  - scikit-learn
  - random-forest
pipeline_tag: text-classification

Sentiment Analysis (Random Forest + Bag of Words)

This model classifies text into emotional sentiment categories using a RandomForestClassifier and NLTK text lemmatization.

Usage

import joblib
import re
import nltk
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from huggingface_hub import hf_hub_download

# Download dependencies
nltk.download('stopwords')
nltk.download('wordnet')

# Download model artifacts
repo_id = "Ttt37/sentiment-analysis-rf"
model_path = hf_hub_download(repo_id=repo_id, filename="sentiment_model.pkl")
vectorizer_path = hf_hub_download(repo_id=repo_id, filename="vectorizer.pkl")

# Load model and vectorizer
model = joblib.load(model_path)
cv = joblib.load(vectorizer_path)

# Text preprocessor
lemmatizer = WordNetLemmatizer()
stop_words = set(stopwords.words('english'))

def preprocess(text):
    review = re.sub('[^a-zA-Z]', ' ', text).lower().split()
    review = [lemmatizer.lemmatize(w) for w in review if w not in stop_words]
    return ' '.join(review)

# Inference
text = "I am feeling really happy today!"
cleaned = preprocess(text)
features = cv.transform([cleaned])
prediction = model.predict(features)

print("Predicted Sentiment:", prediction[0])