Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

C#advancedaiAI generated

Sentiment Analysis Console App with ML.NET

Build a console app in C# that learns to tell positive from negative text, using ML.NET, Microsoft's machine learning library for .NET. You'll train a classifier on a labeled dataset of reviews, turning text into numbers with ML.NET's text featurization, evaluate it honestly on data it has never seen (accuracy, F1 score and AUC), save the trained model to a file, and then type sentences to get instant predictions with a confidence score. You'll also compare a few settings and look at the mistakes it makes. You'll learn the machine learning workflow in .NET (loading data, pipelines, training, evaluation and prediction), text features, reading classification metrics, and saving and reusing models.

Estimate
~9.5h
Steps
7
Completed by
0
Proposed by
codeseed.app

ML.NET

Project roadmap

  1. 01

    Load and split the data

    ~1.5h

    Create a console project and add the Microsoft.ML package. Download a labeled sentiment dataset (for example the UCI sentiment labelled sentences, or a movie reviews dataset) and define an input class with [LoadColumn] attributes for the text and label. Load it into an IDataView with LoadFromTextFile, check the class balance, and split it into 80% training and 20% test data with a fixed seed. Done when: the data loads, and the sizes and class balance are printed.

  2. 02

    Turn text into features

    ~1.5h

    Build the pipeline's first part with FeaturizeText, which normalizes the text, splits it into words and character n-grams, and turns it into a numeric vector. Preview a few transformed rows to see what the model receives. Done when: the featurized data can be previewed with its feature vector length.

  3. 03

    Train the classifier

    ~1.5h

    Add a binary classification trainer, such as SdcaLogisticRegression, to the pipeline and train it on the training data, timing how long it takes. Done when: a trained model is produced without errors.

  4. 04

    Evaluate the model

    ~1.5h

    Evaluate on the test data with BinaryClassification.Evaluate, and print accuracy, precision, recall, F1 score, AUC and the confusion matrix, with a sentence explaining each metric in plain words. Compare at least one other trainer (such as LbfgsLogisticRegression or FastTree) and one featurization setting, and record the results in README.md. Look at 10 misclassified sentences and describe the patterns (such as sarcasm or negation). Done when: README.md compares the models and describes typical mistakes.

  5. 05

    Save and reload the model

    ~1h

    Save the best model with mlContext.Model.Save to a .zip file together with its input schema, and write a separate code path that loads it and creates a PredictionEngine, so predictions don't need retraining. Handle a missing or corrupt model file with a clear message and an offer to train. Done when: the app predicts using the saved model after restarting.

  6. 06

    Build the interactive console

    ~1.5h

    Add a loop where the user types a sentence and sees the prediction (positive or negative) with its probability, a note when the probability is close to 50% (the model is unsure), and commands to retrain and to show the metrics. Done when: typed sentences get instant predictions with confidence scores.

  7. 07

    Test it

    ~1h

    Write xUnit tests that train on a tiny dataset (checking the pipeline runs end to end), check clearly positive and negative sentences get the expected labels from the saved model, and check saving and loading give the same predictions. Done when: dotnet test passes.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~9.5h · 7 steps

Tech stack

ML.NET