Home > Software design >  How can I alter this ML.NET code to input data through the console instead
How can I alter this ML.NET code to input data through the console instead

Time:01-04

I'm following a tutorial which has given me this sample code. I want to alter it so the user inputs the data through the console rather than in the code but I'm unsure how to, any ideas?

using MyMLApp;
// Add input data

var sampleData = new SentimentModel.ModelInput()
{
    Col0 = "Will never go here again!"
};

// Load model and predict output of sample data
var result = SentimentModel.Predict(sampleData);

// If Prediction is 1, sentiment is "Positive"; otherwise, sentiment is "Negative"
string sentiment = result.Prediction == 1 ? "Positive" : "Negative";
Console.WriteLine($"Text: {sampleData.Col0}\nSentiment: {sentiment}");

CodePudding user response:

Use Console.ReadLine(). Optionally handle empty input in your code.

string? input = Console.ReadLine();
var sampleData = new SentimentModel.ModelInput()
{
    Col0 = input
};
  •  Tags:  
  • Related