Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

Problem

 
This problem is centred around developing a .NET Core Winforms application that will detect whether a message is spam or not, based on message content given by the user. To solve this problem, we will build an ML model that takes an input of one column Message,  the 'Label' column which is what you want to predict, and in this case, is named 'Label', which will tell us the predicted result. To train our model, we will use the SMS Spam Collection Dataset downloaded from UCI ML Repository.
 
As we are classifying the text messages into one of two categories, we will use binary classification for this problem.
 

Solution

 
To solve this problem, first, we will build an ML model that we want to use. Then we will train this model on existing data, and lastly, we'll consume the model in our .NET Core Winforms Application to predict whether a few example messages are spam or not.
 
Prerequisites
Let's start.
 
Before going to build our Machine Learning model, we need to set up our .NET Core Winforms application.
 
First, we will create a Winforms application and set up the basic data structures that will be used later for Machine Learning model.
  • Open Visual Studio and select Windows Forms App (.NET Core).

    Spam Detection For Text Messages In ASP.NET Core Using ML.NET

  • Please enter your project name. I'm going to enter Spam Detection Winforms and click on the "Create" button.

    Spam Detection For Text Messages In ASP.NET Core Using ML.NET

  • Now you can see our project in solution explorer, it has been created successfully.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • We have created our .NET Winforms desktop application successfully. Now, first of all, we will download the SMS Spam Collection Data Set from UCI ML Repository and transform the data structure according to our requirement.  

  • After downloading the data, extract the "zip" file.

  • There are two columns inside the dataset file - Label and Message. In the original dataset, the Label column is having two values - spam and ham. We will replace spam and ham with True and False respectively. Our data will look like this (Top 5 records from dataset just for view).
    1. false Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...
    2. false Ok lar... Joking wif u oni...
    3. true Free entry in 2 a wkly comp to win FA Cup final tkts 21st May 2005. Text FA to 87121 to receive entry question(std txt rate)T&C's apply 08452810075over18's
    4. false U dun say so early hor... U c already then say...
    5. false Nah I don't think he goes to usf, he lives around here though
  • Now, we will build and train our ML.NET Model using ML.NET Model Builder, a Visual Studio Extension. I've already installed ML.NET Model builder on my machine If you don't have you can download it from here.

  • Now, right-click on your WinForms project and select add button and then click on the Machine Learning option

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • ML.NET Model builder wizard has been opened. Now, please select the scenario for which you want to build a machine learning model. For instance, I want to build a spam detection machine learning model which isn't here, so I'll select the custom scenario.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • Now select the dataset file, select label column and input columns for our machine learning model. After selecting all options please click on the train button.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • Select the machine learning problem for which you want to build the model -  like I want to detect spam detection whether it's spam or not so I'll select a binary classification problem. Set the training time for while you want to train the machine learning model.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • After clicking on the train button, you will notice that the training has started.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • Once the model training has been completed, you can evaluate the machine learning model performance.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • Our machine learning model has been built and trained successfully, now click on add projects button to add this machine learning model into our existing project so that we can consume this model into our .NET Core Winforms application.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • Now you can see that our new machine learning model projects have been added to our existing project solution. You can explore them using the Solution Explorer window.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • Now we will consume this machine learning model into our .NET Core Winforms application. So, first of all, we will create a new Form into our application.

  • To create a new form, Right-click on WinForms project and then select Add>Form (Windows Form) option, a new window will be opened.

  • Please enter your form name and click on Create button.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • After the form has been created, double click on the newly created form from solution explorer and your form will be opened in Winforms designer.

  • Now open ToolBox and add a RichText to get a message from the user which we want to detect whether it's spam or not.

  • Add a new Button from ToolBox and set the title to predict. You can see the designed form, as it's given below.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

  • Now double click on Predict button, and Visual Studio will automatically generate an event handler for this button.

  • Inside your button event handler, we will write the code, we will get the message from the user and then will consume our machine learning model to make a prediction and after that, we will show the prediction/result into a MessageBox.
    1. //Get message from text box  
    2. var msg = txtMessage.Text;  
    3. //Make prediction  
    4. var prediction=ConsumeModel.Predict(new ModelInput  
    5. {  
    6.    Message=msg,  
    7. });  
    8. //Show result  
    9. MessageBox.Show($"Prediction: {(prediction.Prediction?"Spam":"Not Spam")} Score: {prediction.Score}");  
  • Here we can explore what's inside our consume class. Open Spam Detection WinformsML.Model project and click on ConsumeModel class.

  • Inside Predict function, we are creating an instance of MLContext and after that, we are loading our machine learning model .zip file that we have build using ML.NET Model builder.

  • After loading model, we are creating a ML.NET prediction engine to make our prediction.
    1. public class ConsumeModel  
    2.    {  
    3.        // For more info on consuming ML.NET models, visit https://aka.ms/model-builder-consume  
    4.        // Method for consuming model in your app  
    5.        public static ModelOutput Predict(ModelInput input)  
    6.        {  
    7.   
    8.            // Create new MLContext  
    9.            MLContext mlContext = new MLContext();  
    10.   
    11.            // Load model & create prediction engine  
    12.            string modelPath = @"C:\Users\mhabi\source\repos\Spam Detection Winforms\Spam Detection WinformsML.Model\MLModel.zip";  
    13.            ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);  
    14.            var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);  
    15.   
    16.            // Use model to make prediction on input data  
    17.            ModelOutput result = predEngine.Predict(input);  
    18.            return result;  
    19.        }  
    20.    }   
  • This is a beginner article so that's why I'm not going into deep how the machine learning model is built and designed behind ML.NET Model Builder.

  • Now click on debug button to run your .NET Core Winforms application.

    Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning

Conclusion


So we have built the solution to our problem.
  1. We have created a .NET WinFormsApplication template.
  2. We have downloaded and SMS Spam Collection data set files for training our model.
  3. We have built and trained our machine learning model using ML.NET Model Builder.
  4. We have Consumed our ML.NET Model for SMS Spam Detection problem.
  5. At last, we built a user interface that would allow the user to enter their message to predict whether the message is spam or not.
Demo
 
Spam Detection For Text Messages In .NET Core Winform Using ML.NET Machine Learning
 
Note
In this article, we learned how to develop a .NET Core WinForms Application for Spam Detection for Text Messages and how to build, train, and consume Spam Detection ML.NET Machine Learning model in .NET Core WinForms.
 
For more information about dataset attributes, please check out UCI ML Repository.
 
You can download the demo project from my GitHub repository.