How can you perform sentiment analysis using C# and ML.NET?
How can you perform sentiment analysis using C# and ML.NET?
1 Answer
You can perform sentiment analysis in C# using ML.NET by training or consuming a machine learning model that classifies text as positive or negative.
A common workflow is:
- Install ML.NET packages
- Prepare training data
- Build and train a pipeline
- Evaluate the model
- Predict sentiment on new text
Official docs: ML.NET Documentation
1. Create a Console Project
dotnet new console -n SentimentAnalysisApp
cd SentimentAnalysisApp
Install the ML.NET package:
dotnet add package Microsoft.ML
ML.NET NuGet package:
Microsoft.ML NuGet Package
2. Prepare Training Data
Create a file named sentiment-data.tsv.
Example:
Sentiment Text
1 I love this product
1 This is amazing
0 I hate this item
0 This is terrible
1= Positive0= Negative
3. Create Data Models
Create a file called SentimentData.cs.
using Microsoft.ML.Data;
// Input data class
public class SentimentData
{
// First column from dataset
[LoadColumn(0)]
public bool Sentiment { get; set; }
// Second column from dataset
[LoadColumn(1)]
public string Text { get; set; }
}
// Prediction output class
public class SentimentPrediction
{
// Predicted sentiment
[ColumnName("PredictedLabel")]
public bool Prediction { get; set; }
// Confidence score
public float Probability { get; set; }
// Raw model score
public float Score { get; set; }
}
4. Train the Model
Replace Program.cs with:
using Microsoft.ML;
using Microsoft.ML.Data;
// Create ML context
var mlContext = new MLContext();
// Load training data
IDataView dataView = mlContext.Data.LoadFromTextFile<SentimentData>(
path: "sentiment-data.tsv",
hasHeader: true
);
// Split dataset into train/test sets
var splitData = mlContext.Data.TrainTestSplit(
dataView,
testFraction: 0.2
);
// Build ML pipeline
var pipeline = mlContext.Transforms.Text.FeaturizeText(
outputColumnName: "Features",
inputColumnName: nameof(SentimentData.Text)
)
.Append(
mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(
labelColumnName: nameof(SentimentData.Sentiment),
featureColumnName: "Features"
)
);
// Train model
var model = pipeline.Fit(splitData.TrainSet);
// Evaluate model
var predictions = model.Transform(splitData.TestSet);
var metrics = mlContext.BinaryClassification.Evaluate(
predictions,
labelColumnName: nameof(SentimentData.Sentiment)
);
// Print evaluation metrics
Console.WriteLine($"Accuracy: {metrics.Accuracy:P2}");
Console.WriteLine($"F1 Score: {metrics.F1Score:P2}");
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:P2}");
// Create prediction engine
var predictionEngine = mlContext.Model.CreatePredictionEngine
<SentimentData, SentimentPrediction>(model);
// Example prediction
var sample = new SentimentData
{
Text = "This app is fantastic!"
};
// Predict sentiment
var prediction = predictionEngine.Predict(sample);
// Display result
Console.WriteLine($"Text: {sample.Text}");
Console.WriteLine($"Prediction: {(prediction.Prediction ? "Positive" : "Negative")}");
Console.WriteLine($"Probability: {prediction.Probability:P2}");
5. Run the Application
dotnet run
Example output:
Accuracy: 95.00%
F1 Score: 94.00%
AUC: 97.00%
Text: This app is fantastic!
Prediction: Positive
Probability: 98.12%
How ML.NET Processes Text
The important step is:
FeaturizeText()
This converts raw text into numerical vectors using NLP techniques such as:
- Tokenization
- N-grams
- TF-IDF weighting
- Text normalization
The classifier then learns patterns associated with positive or negative sentiment.
Using a Pretrained Model
Instead of training from scratch, you can also use:
- ONNX models
- Azure AI services
Hugging Face transformer models exported to ONNX
ONNX with ML.NET:
ML.NET ONNX Guide
Real-World Use Cases
Sentiment analysis is commonly used for:
- Customer review analysis
- Social media monitoring
- Chat moderation
- Support ticket prioritization
- Product feedback analytics
Improving Accuracy
For production systems:
- Use larger datasets
- Clean noisy text
- Remove spam/duplicates
- Use transformer-based models
- Balance positive/negative samples
- Add multilingual support if needed