Sentiment analysis is a classic NLP task that involves classifying the emotional tone of a piece of text (e.g., positive, negative, or neutral). This is incredibly useful for businesses to gauge customer feedback, analyze social media trends, and more.
In this tutorial, we'll go through the entire process: from training a machine learning model to deploying it as a usable web service.
Part 1: The Machine Learning Model
Our model needs to learn from examples. We'll use a simple dataset of text reviews and their corresponding labels (positive/negative).
Step 1: Vectorization (Turning Text into Numbers)
A machine learning model can't understand words; it only understands numbers. The process of converting text into a numerical representation is called vectorization.
A classic and effective method is TF-IDF (Term Frequency-Inverse Document Frequency). It creates a vector for each document where each number represents the importance of a word relative to the entire collection of documents. It gives higher weight to words that are frequent in a specific document but rare across all other documents, making them good indicators of the topic.
Step 2: Training a Classifier
Once our text is vectorized, we can train a standard classifier. We'll use Python's scikit-learn library, which makes it easy to build a pipeline that combines the vectorizer and the classifier into a single object.
Code Snippet: Training and Saving the Model
Python
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import SGDClassifier
from sklearn.pipeline import Pipeline
from sklearn.metrics import accuracy_score
import joblib # For saving the model
# --- 1. Load and prepare sample data ---
# In a real project, this would be a larger CSV file.
data = {
'text': [
'This movie was absolutely fantastic!',
'I hated every second of it.',
'The acting was superb and the plot was thrilling.',
'A complete waste of time and money.',
'A brilliant and heartwarming story.',
'Terrible, just terrible.'
],
'sentiment': ['positive', 'negative', 'positive', 'negative', 'positive', 'negative']
}
df = pd.DataFrame(data)
X_train, X_test, y_train, y_test = train_test_split(df['text'], df['sentiment'], test_size=0.2, random_state=42)
# --- 2. Create and train the Scikit-learn pipeline ---
# The pipeline will first apply TF-IDF and then train the classifier.
sentiment_pipeline = Pipeline([
('tfidf', TfidfVectorizer()),
('clf', SGDClassifier(loss='hinge', penalty='l2', random_state=42)),
])
print("Training model...")
sentiment_pipeline.fit(X_train, y_train)
# --- 3. Evaluate the model ---
predictions = sentiment_pipeline.predict(X_test)
print(f"Model Accuracy: {accuracy_score(y_test, predictions):.2f}")
# --- 4. Save the trained pipeline to a file ---
model_filename = 'sentiment_model.joblib'
joblib.dump(sentiment_pipeline, model_filename)
print(f"Model saved to {model_filename}")
Part 2: The Web API
A saved model file is useless on its own. We need to create an interface so other applications can send it new text and get predictions back. A REST API is the perfect tool for this. We'll use Flask, a lightweight Python web framework.
Step 3: Building the API with Flask
Our Flask app will do three things:
- Load our saved sentiment_model.joblib file when it starts.
- Define an endpoint (e.g., /predict) that listens for POST requests.
- Take the JSON text from the request, use the model to make a prediction, and return the result as JSON.
Code Snippet: The Flask API (app.py)
Python
from flask import Flask, request, jsonify
import joblib
# --- 1. Initialize the Flask app ---
app = Flask(__name__)
# --- 2. Load the trained model ---
try:
model = joblib.load('sentiment_model.joblib')
print("Model loaded successfully.")
except FileNotFoundError:
print("Model file not found. Please train and save the model first.")
model = None
# --- 3. Define the prediction endpoint ---
@app.route('/predict', methods=['POST'])
def predict():
if not model:
return jsonify({'error': 'Model is not available'}), 500
# Get the JSON data from the request
data = request.get_json()
if not data or 'text' not in data:
return jsonify({'error': 'Invalid input: "text" field is required.'}), 400
text_to_predict = data['text']
# Use the model to make a prediction
try:
prediction = model.predict([text_to_predict])[0]
return jsonify({'sentiment': prediction})
except Exception as e:
return jsonify({'error': str(e)}), 500
# --- Run the app ---
if __name__ == '__main__':
app.run(debug=True, port=5000)
To run this, you would save the code as app.py and run python app.py in your terminal. You could then test it with a tool like curl: curl -X POST -H "Content-Type: application/json" -d '{"text": "this was a wonderful experience"}' http://127.0.0.1:5000/predict