Problem
This problem is centered around predicting if a customer's review has a positive or negative sentiment. We will use small Wikipedia-detox-datasets (one dataset for training and a second dataset for model's accuracy evaluation) that were processed by humans and each comment has been assigned a sentiment label:
- 0 - nice/positive
- 1 - toxic/negative
Using those datasets we will build a model that when predicting, will analyze a string and predict a sentiment value of 0 or 1.
Prerequisites
- Visual Studio 2019 or 2017
- ML.NET Model Builder Extension for Visual Studio
Solution
To solve this problem, first, we will build an ML model. Then we will train the model on existing data, evaluate how good it is, and lastly, we'll consume the model and integrate it into our Asp.Net Core application to predict sentiment for new comments or reviews.
Let's start,
- Download and Install ML.NET Model Builder tool from Visual Studio Marketplace. Click here.
- After ML.NET Model Builder installation open your Visual Studio (in my case I'm using VS2019)
- After Project has been selected, enter your Project Name.
- Select Asp.Net Core template which you want to use, I'm using Web Application MVC.
- After the project has been created, we will start to build our model. Right-click on Project > Add > Machine Learning,
- ML.NET Model Builder tool GUI has been opened. Select Sentiment Analysis.
- After scenario selection, we will select the data set that will be used to train our model. You add data from the file as well as using SQL Server Database. I will use wikiDetoxAnnotated40kRows data to train my model. You can download it from here. After data source selection click on Train link
- After data source selection, enter the time for which you want to train the model and click on Start training button, and the model training will be started for the entered time.
- After the model has been successfully trained click on the evaluation button.
- You can see the new build machine learning model evaluation. Click on code button to consume the model into your project.
- As you can see in solution explorer, the ML.NET Machine Learning model has been created.
- Now, the machine learning model for semantic analysis has been created. Now, first, we need to add the reference Semantic AnalysisML.Model project into our Semantic Analysis Web project and also add ML.NET package from NuGet.
Create a new empty controller named Semantic and copy the snippet code.
- using System;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.ML;
- using SentimentAnalysisML.Model.DataModels;
-
- namespace Sentiment_Analysis.Controllers
- {
- public class SemanticController : Controller
- {
- [HttpGet]
- public IActionResult Analysis()
- {
- return View();
- }
- [HttpPost]
- public IActionResult Analysis(ModelInput input)
- {
-
- MLContext mlContext = new MLContext();
- ITransformer mlModel = mlContext.Model.Load(@"..\Sentiment AnalysisML.Model\MLModel.zip", out var modelInputSchema);
- // Create predection engine related to the loaded train model
- var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);
-
- input.Year = DateTime.Now.Year;
-
- ModelOutput result = predEngine.Predict(input);
- // Store result into ViewBag
- ViewBag.Result = result;
- return View();
- }
- }
- }
Add a new view named Analysis in Semantic Folder and copy the snippet code.
- @model SentimentAnalysisML.Model.DataModels.ModelInput
-
- @{
- ViewData["Title"] = "Semantic Analysis";
- }
- <h2>Semantic Analysis</h2>
- <hr />
- @{
- var result = ViewBag.Result;
- }
- <div class="row">
- <div class="col-md-4">
- <form asp-action="Analysis">
- <div asp-validation-summary="ModelOnly" class="text-danger"></div>
- <div class="form-group">
- <label asp-for="Comment" class="control-label"></label>
- <input asp-for="Comment" class="form-control" />
- <span asp-validation-for="Comment" class="text-danger"></span>
- </div>
- <div class="form-group">
- <input type="submit" value="Check" class="btn btn-default" />
- </div>
- </form>
- </div>
- <div class="col-md-offset-4 col-md-4">
- @if (result != null)
- {
- <h2>Result</h2>
- <h3>Predection:@result.Prediction</h3>
- <h3>Score:@result.Score</h3>
- }
- </div>
- </div>
-
- @section Scripts {
- @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
- }
Demo
- false - nice/positive
- true - toxic/negative
Source Code
You can also access the source code from
GitHub