static void Main(string[] args)
        {
            /* Get event hub name and service bus connection string from user input. Basic use is to ask user:
             * Which Event Hub are we listening for a message from? (where is the AverageSentiment number coming from?)
            */

            //string ehName = "";
            //string connection = "";

            //Console.WriteLine("Enter Event Hub Name: ");
            //ehName = Console.ReadLine();
            //Console.WriteLine("Enter Service Bus Connection String: ");
            //connection = Console.ReadLine() + ";TransportType=Amqp";

            //Console.WriteLine("START\n");

            //Comment out these two string if using the user input code above
            string ehName = "EVENT HUB NAME";
            string connection = "Endpoint=sb://SERVICEBUSNAMESPACE.servicebus.windows.net/;SharedAccessKeyName=ACCESSKEYNAME;SharedAccessKey=ACCESSKEY;TransportType=Amqp";

            MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connection);
            EventHubClient ehub = factory.CreateEventHubClient(ehName);
            EventHubConsumerGroup group = ehub.GetDefaultConsumerGroup();
            EventHubReceiver reciever = group.CreateReceiver("0");

            while (true)
            {
                EventData data = reciever.Receive();
                if (data != null)
                {
                    try
                    {
                        var result = Encoding.UTF8.GetString(data.GetBytes());
                        dynamic resultJson = JObject.Parse(result);
                        var avgScore = resultJson.avgsentiment;

                        Console.WriteLine(result);
                        Console.WriteLine("Average Score: " + avgScore + "\n");

                        //create sentimentdata object
                        var sentimentData = new SentimentData() { AverageSentiment = avgScore, EventHubName = ehName };

                        //post sentimentdata to api
                        using (var client = new HttpClient())
                        {
                            client.BaseAddress = new Uri("CLIENT BASE ADDRESS FOR WEB API FOR SENTIMENT DATA");
                            var response = client.PostAsJsonAsync("/api/sentimentdata", sentimentData).Result;
                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
            }
        }
Exemple #2
0
        private static void UseModelWithSingleItem(MLContext mlContext, ITransformer model)
        {
            PredictionEngine <SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine <SentimentData, SentimentPrediction>(model);
            SentimentData sampleStatement = new SentimentData
            {
                SentimentText = "This was a very bad steak"
            };
            var resultPrediction = predictionFunction.Predict(sampleStatement);

            Console.WriteLine();
            Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");

            Console.WriteLine();
            Console.WriteLine($"Sentiment: {sampleStatement.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultPrediction.Probability} | Score: {resultPrediction.Score}");

            Console.WriteLine("=============== End of Predictions ===============");
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            //1. Build an ML.NET pipeline for training a sentiment analysis model
            Console.WriteLine("Training a model for Sentiment Analysis using ML.NET");
            var pipeline = new LearningPipeline();

            // 1a. Load the training data using a TextLoader.
            pipeline.Add(new TextLoader(@"..\..\..\Data\wikipedia-detox-250-line-data.tsv").CreateFrom <SentimentData>(useHeader: true));

            // 1b. Featurize the text into a numeric vector that can be used by the machine learning algorithm.
            pipeline.Add(new TextFeaturizer("Features", "SentimentText"));

            // 1c. Add AveragedPerceptron (a linear learner) to the pipeline.
            pipeline.Add(new AveragedPerceptronBinaryClassifier()
            {
                NumIterations = 10
            });

            // 1d. Get a model by training the pipeline that was built.
            PredictionModel <SentimentData, SentimentPrediction> model =
                pipeline.Train <SentimentData, SentimentPrediction>();

            // 2. Evaluate the model to see how well it performs on different data (output the percent of examples classified correctly).
            Console.WriteLine("Training of model is complete \nTesting the model with test data");
            var testData  = new TextLoader(@"..\..\..\Data\wikipedia-detox-250-line-test.tsv").CreateFrom <SentimentData>(useHeader: true);
            var evaluator = new BinaryClassificationEvaluator();
            BinaryClassificationMetrics metrics = evaluator.Evaluate(model, testData);

            Console.WriteLine($"Accuracy of trained model for test data is: {metrics.Accuracy:P2}");

            // 3. Save the model to file so it can be used in another app.
            model.WriteAsync("sentiment_model.zip");

            // 4. Use the model for a single prediction.
            SentimentData testInput = new SentimentData {
                SentimentText = "ML.NET is fun, more samples at https://github.com/dotnet/machinelearning-samples"
            };
            var sentiment = (model.Predict(testInput).Sentiment == true) ? "Positive" : "Negative";

            /* This template uses a minimal dataset to build a sentiment analysis model which leads to relatively low accuracy.
             * In order to build a sentiment analysis model with higher accuracy please follow the walkthrough at https://aka.ms/mlnetsentimentanalysis*/
            Console.WriteLine("Predicted sentiment for \"" + testInput.SentimentText + "\" is:" + sentiment);
            Console.ReadKey();
        }
Exemple #4
0
        public List <SentimentData> GetAllTweetData()
        {
            var client      = new RestClient("https://api.twitter.com/2/users/22853963/mentions?&expansions=geo.place_id&tweet.fields=geo&user.fields=location&place.fields=contained_within,country,country_code,full_name,geo,id,name,place_type");
            var bearerToken = GetBearerToken();
            var request     = new RestRequest(Method.GET);

            var token = JsonConvert.DeserializeObject <Token>(bearerToken.Content);

            request.AddHeader("Authorization", "Bearer " + token.Access_Token);
            request.AddParameter("max_results", 10);

            var response  = client.Execute(request);
            var content   = JsonConvert.DeserializeObject <Root>(response.Content);
            var nextToken = content.meta.next_token;

            request.AddParameter("pagination_token", nextToken);
            List <Datum> tweetList  = content.data;
            int          retryCount = 5;

            for (int i = 0; i < retryCount; i++)
            {
                request.Parameters[2].Value = nextToken;
                var nextResponse = client.Execute(request);
                var nextContent  = JsonConvert.DeserializeObject <Root>(nextResponse.Content);

                tweetList.AddRange(nextContent.data);
                nextToken = nextContent.meta.next_token;
            }

            var dataModelList = new List <SentimentData>();

            foreach (var tweet in tweetList)
            {
                var tempModel = new SentimentData
                {
                    SentimentText = tweet.text,
                    Location      = locationList[GetLocation()],
                    Sentiment     = Convert.ToBoolean(GetRandom())
                };

                dataModelList.Add(tempModel);
            }
            return(dataModelList);
        }
Exemple #5
0
        public StringBuilder UseModelWithSingleItem(MLContext mlContext, ITransformer model)
        {
            StringBuilder builder = new StringBuilder();

            PredictionEngine <SentimentData, SentimentPrediction> predictionFunction = mlContext.Model.CreatePredictionEngine <SentimentData, SentimentPrediction>(model);

            //The PredictionEngine allows you to perform a prediction on a single instance of data

            SentimentData sampleStatement = new SentimentData
            {
                SentimentText = "This place is very good"
            };
            var resultPrediction = predictionFunction.Predict(sampleStatement);

            builder.AppendLine("=============== Prediction Test of model with a single sample and test dataset ===============");
            builder.AppendLine($"Sentiment: {resultPrediction.SentimentText} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative")} | Probability: {resultPrediction.Probability} ");
            builder.AppendLine("=============== End of Predictions ===============");
            return(builder);
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            //Parse HTTP Request Body
            string        requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            SentimentData data        = JsonConvert.DeserializeObject <SentimentData>(requestBody);

            //Make Prediction
            SentimentPrediction prediction = _predictionEnginePool.Predict(data);

            //Convert prediction to string
            int sentiment = Convert.ToBoolean(prediction.Prediction) ? 1 : 0;

            //Return Prediction
            return((ActionResult) new OkObjectResult(sentiment));
        }
Exemple #7
0
        private static void Predict(MLContext mlContext, ITransformer model)
        {
            var predictionFunction = model.MakePredictionFunction <SentimentData, SentimentPrediction>(mlContext);

            SentimentData sampleStatement = new SentimentData
            {
                SentimentText = "This is a very rude movie"
            };

            var resultprediction = predictionFunction.Predict(sampleStatement);

            Console.WriteLine();
            Console.WriteLine("=============== Prediction Test of model with a single sample and test dataset ===============");

            Console.WriteLine();
            Console.WriteLine($"Sentiment: {sampleStatement.SentimentText} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Not Toxic")} | Probability: {resultprediction.Probability} ");
            Console.WriteLine("=============== End of Predictions ===============");
            Console.WriteLine();
        }
        public static SentimentPrediction Predict(SentimentData input)
        {
            // Create new MLContext
            MLContext mlContext = new MLContext();

            // Load model & create prediction engine

            ITransformer mlModel = mlContext.Model.Load(
                MLModelBuilder.GetAbsolutePath(Constants.ModelFilePath),
                out var modelInputSchema);

            System.Console.WriteLine(modelInputSchema);

            var predEngine = mlContext.Model
                             .CreatePredictionEngine <SentimentData, SentimentPrediction>(mlModel);

            // Use model to make prediction on input data
            SentimentPrediction result = predEngine.Predict(input);

            return(result);
        }
        public void Post_negativeText_False()
        {
            //Arrange
            MLContext      mlContext = new MLContext();
            DataViewSchema predictionPipelineSchema;
            ITransformer   predictionPipeline = mlContext
                                                .Model.Load("C:/Users/iRebeca/facultate/.NET/.NetProject/TSense/Tests/MlModels/sentiment_model.zip", out predictionPipelineSchema);
            PredictionEngine <SentimentData, SentimentPrediction> predictionEngine = mlContext
                                                                                     .Model.CreatePredictionEngine <SentimentData, SentimentPrediction>(predictionPipeline);
            SentimentController sentiment     = new SentimentController(predictionEngine);
            SentimentData       sentimentData = new SentimentData();

            sentimentData.SentimentText = "It was bad";

            //Act
            var result     = (OkObjectResult)sentiment.Post(sentimentData).Result;
            var prediction = (SentimentPrediction)result.Value;

            //Assert
            Assert.IsInstanceOfType(result.Value, typeof(SentimentPrediction));
            Assert.AreEqual(prediction.Prediction, false);
        }
 private void OnGetTextSentiment(SentimentData sentimentData, string data)
 {
     if (sentimentData != null)
     {
         Log.Debug("ExampleAlchemyLanguage", "status: {0}", sentimentData.status);
         Log.Debug("ExampleAlchemyLanguage", "url: {0}", sentimentData.url);
         Log.Debug("ExampleAlchemyLanguage", "language: {0}", sentimentData.language);
         Log.Debug("ExampleAlchemyLanguage", "text: {0}", sentimentData.text);
         if (sentimentData.docSentiment == null)
         {
             Log.Debug("ExampleAlchemyLanguage", "No sentiment found!");
         }
         else
         if (sentimentData.docSentiment != null && !string.IsNullOrEmpty(sentimentData.docSentiment.type))
         {
             Log.Debug("ExampleAlchemyLanguage", "Sentiment: {0}, Score: {1}", sentimentData.docSentiment.type, sentimentData.docSentiment.score);
         }
     }
     else
     {
         Log.Debug("ExampleAlchemyLanguage", "Failed to find Relations!");
     }
 }
 private void OnGetTextSentimentUrl(SentimentData sentimentData, string data)
 {
     Log.Debug("ExampleAlchemyLanguage", "Alchemy Language - Get text sentiment response url: {0}", data);
     _getTextSentimentURLTested = true;
 }
 private void OnGetTextSentimentText(SentimentData sentimentData, Dictionary <string, object> customData)
 {
     Log.Debug("ExampleAlchemyLanguage.OnGetTextSentimentText()", "Alchemy Language - Get text sentiment response text: {0}", customData["json"].ToString());
     _getTextSentimentTextTested = true;
 }
Exemple #13
0
        public static PredictionModel <SentimentData, SentimentPrediction> TrainAndPredict()
        {
            string pathCurrent = System.IO.Directory.GetCurrentDirectory();

            var pipeline = new LearningPipeline();

            pipeline.Add(new TextLoader <SentimentData>(_dataPath, useHeader: false, separator: "tab"));
            pipeline.Add(new TextFeaturizer("Features", "SentimentText"));
            pipeline.Add(new FastTreeBinaryClassifier()
            {
                NumLeaves = 5, NumTrees = 5, MinDocumentsInLeafs = 2
            });

            PredictionModel <SentimentData, SentimentPrediction> model = null;

            try
            {
                model = pipeline.Train <SentimentData, SentimentPrediction>();

                IEnumerable <SentimentData> sentiments = new[]
                {
                    new SentimentData
                    {
                        SentimentText = "Contoso's 11 is a wonderful experience",
                        Sentiment     = 0
                    },
                    new SentimentData
                    {
                        SentimentText = "The acting in this movie is very bad",
                        Sentiment     = 0
                    },
                    new SentimentData
                    {
                        SentimentText = "Joe versus the Volcano Coffee Company is a great film.",
                        Sentiment     = 0
                    },
                    new SentimentData
                    {
                        SentimentText = "horrible.",
                        Sentiment     = 0
                    },
                    new SentimentData
                    {
                        SentimentText = "oh my godness. wonderful.",
                        Sentiment     = 0
                    },
                    new SentimentData
                    {
                        SentimentText = "Contoso's 11 is not an ordinary expirence. just fine.",
                        Sentiment     = 0
                    },
                    new SentimentData
                    {
                        SentimentText = "It's based on an personal condition.",
                        Sentiment     = 0
                    }
                };

                IEnumerable <SentimentPrediction> predictions = model.Predict(sentiments);
                var sentimentsAndPredictions = sentiments.Zip(predictions, (sentiment, prediction) => (sentiment, prediction));

                Console.WriteLine();
                Console.WriteLine("Result Sentiment Predictions >>>>>>>>>>>>>>>>>>");
                Console.WriteLine("---------------------");
                foreach ((SentimentData, SentimentPrediction)item in sentimentsAndPredictions)
                {
                    SentimentData       sentiment  = item.Item1;
                    SentimentPrediction prediction = item.Item2;
                    Console.WriteLine($"Sentiment: {sentiment.SentimentText} | Prediction: {(prediction.Sentiment ? "Positive" : "Negative")}");
                }
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("error:" + e.Message);
            }

            return(model);
        }
Exemple #14
0
        static void Main(string[] args)
        {
            /* Get event hub name and service bus connection string from user input. Basic use is to ask user:
             * Which Event Hub are we listening for a message from? (where is the AverageSentiment number coming from?)
             */

            //string ehName = "";
            //string connection = "";

            //Console.WriteLine("Enter Event Hub Name: ");
            //ehName = Console.ReadLine();
            //Console.WriteLine("Enter Service Bus Connection String: ");
            //connection = Console.ReadLine() + ";TransportType=Amqp";

            //Console.WriteLine("START\n");


            //Comment out these two string if using the user input code above
            string ehName     = "EVENT HUB NAME";
            string connection = "Endpoint=sb://SERVICEBUSNAMESPACE.servicebus.windows.net/;SharedAccessKeyName=ACCESSKEYNAME;SharedAccessKey=ACCESSKEY;TransportType=Amqp";

            MessagingFactory      factory  = MessagingFactory.CreateFromConnectionString(connection);
            EventHubClient        ehub     = factory.CreateEventHubClient(ehName);
            EventHubConsumerGroup group    = ehub.GetDefaultConsumerGroup();
            EventHubReceiver      reciever = group.CreateReceiver("0");


            while (true)
            {
                EventData data = reciever.Receive();
                if (data != null)
                {
                    try
                    {
                        var     result     = Encoding.UTF8.GetString(data.GetBytes());
                        dynamic resultJson = JObject.Parse(result);
                        var     avgScore   = resultJson.avgsentiment;

                        Console.WriteLine(result);
                        Console.WriteLine("Average Score: " + avgScore + "\n");

                        //create sentimentdata object
                        var sentimentData = new SentimentData()
                        {
                            AverageSentiment = avgScore, EventHubName = ehName
                        };

                        //post sentimentdata to api
                        using (var client = new HttpClient())
                        {
                            client.BaseAddress = new Uri("CLIENT BASE ADDRESS FOR WEB API FOR SENTIMENT DATA");
                            var response = client.PostAsJsonAsync("/api/sentimentdata", sentimentData).Result;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
            }
        }
Exemple #15
0
 public static SentimentPrediction getPrediction(SentimentData data, PredictionEnginePool <SentimentData, SentimentPrediction> PredictionEnginePool)
 {
     return(PredictionEnginePool.Predict(modelName: ConfigurationManager.AppSettings.Get("modelName"), example: data));
 }
Exemple #16
0
        static void Main(string[] args)
        {
            var defaultColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Cyan;
            //1. Create our ML Context
            var          mlContext = new MLContext();
            ITransformer model     = null;

            //2. Load training data
            IDataView trainingDataview = mlContext.Data.LoadFromTextFile <SentimentData>(TrainingDataPath, hasHeader: true);


            if (File.Exists(ModelPath))
            {
                model = mlContext.Model.Load(ModelPath, out var modelInputSchema);
                Console.WriteLine($">>> Loaded model");
            }
            else
            {
                //3. Create and build the pipeline to prepare your data
                var pipeline = mlContext.Transforms.Text.FeaturizeText(outputColumnName: DefaultOutputColumnName, inputColumnName: nameof(SentimentData.Text)).
                               Append(mlContext.BinaryClassification.Trainers.SdcaLogisticRegression());

                //4. Get the model by training the pipeline that was build
                Console.WriteLine($">>> Creating and training the model");
                model = pipeline.Fit(trainingDataview);

                //5. Evaluate the model
                Console.WriteLine($">>> Training completed");

                Console.WriteLine($">>> Evaluating the model with test data");
                IDataView testDataview = mlContext.Data.LoadFromTextFile <SentimentData>(TestDataPath, hasHeader: true);
                var       predictions  = model.Transform(testDataview);
                var       results      = mlContext.BinaryClassification.Evaluate(predictions);
                Console.WriteLine($">>> Model accuracy {results.Accuracy:P2}");
            }

            //6. Use Model - Create prediction engine related to the loaded trained model

            var predictionEngine = mlContext.Model.CreatePredictionEngine <SentimentData, SentimentPrediction>(model);

            Console.WriteLine($">>> Created prediction engine based on model");

            string[] textInputs = File.ReadAllLines(NewInputsPath);

            Console.WriteLine($">>> Make predictions for new inputs");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("================START PREDICTIONS================");
            foreach (var input in textInputs)
            {
                var testInput = new SentimentData {
                    Text = input
                };
                var predictionResult = predictionEngine.Predict(testInput);
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("Prediction for ");
                Console.ForegroundColor = defaultColor;
                Console.Write($"\"{testInput.Text}\"");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write(" is: ");

                if (Convert.ToBoolean(predictionResult.Prediction))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Toxic");
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Non Toxic");
                }
            }

            //7. Save Model
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("================END PREDICTIONS==================");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine($">>> Saved model");
            Console.ForegroundColor = defaultColor;
            mlContext.Model.Save(model, trainingDataview.Schema, ModelPath);
        }
 private void OnGetTextSentimentText(SentimentData sentimentData, string data)
 {
     Log.Debug("ExampleAlchemyLanguage", "Alchemy Language - Get text sentiment response text: {0}", data);
     Test(sentimentData != null);
     _getTextSentimentTextTested = true;
 }