Exemplo n.º 1
0
        private async Task MessageReceivedAsync(SocketMessage message)
        {
            if (message.Author.Id == _client.CurrentUser.Id)
                return;

            string messageContent = message.Content;

            messageContent = Utf8Encoder.GetString(Utf8Encoder.GetBytes(messageContent));
            if (message.Author.Id.ToString() == ownerId)
            {
                if (messageContent == "trenuj")
                {
                    await message.Channel.SendMessageAsync("Trenuje...");
                    AdvertDetector.Learn();
                    await message.Channel.SendMessageAsync("Wytrenowany");
                    AdvertDetector.Load();
                    return;
                }
            }

            if (message.Channel.Id.ToString() == channelId)
            {
                if (!string.IsNullOrWhiteSpace(messageContent) && messageContent.Length > 3)
                {
                    string msg = messageContent.Replace("\n", " ").Replace("\r", " ");

                    Messages[message.Id] = msg;
                    SentimentPrediction resultprediction = AdvertDetector.Predict(msg);
                    string text = $"{(resultprediction.Probability * 100).ToString("0.00")}%: {StringBuilderStrings(msg.Take(30))}...";
                    await message.Channel.SendMessageAsync(text);
                }
            }
        }
Exemplo n.º 2
0
        private void sent_but_Click(object sender, RoutedEventArgs e)
        {
            SentimentPrediction resultprediction = SentimentAnalyzer.predict(sent_input.Text);
            String txt = $"Text: {sent_input.Text} | Prediction: {(Convert.ToBoolean(resultprediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultprediction.Probability} ";

            sent_output.Text += txt + Environment.NewLine;
        }
Exemplo n.º 3
0
        public String analyzeMsg_withSentiment(string m)
        {
            String result        = "";
            int    target_player = 0;

            if (m.Contains("P1") && !m.Contains("P2"))
            {
                target_player = 1;
            }
            else if (m.Contains("P2") && !m.Contains("P1"))
            {
                target_player = 2;
            }
            if (target_player != 0)
            {
                SentimentPrediction resultprediction = SentimentAnalyzer.predict(m);
                Boolean             neg = Convert.ToBoolean(resultprediction.Prediction);
                if (neg)
                {
                    score_neg[0]++;
                    score_neg[target_player]++;
                }
                else
                {
                    score_pos[0]++;
                    score_pos[target_player]++;
                }
                updateVariables();
                result = $"  P{target_player} {(neg ? "-" : "+")} c={resultprediction.Probability}";
            }
            return(result);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            string           dataPath = "wikipedia-detox-250-line-data.tsv";
            LearningPipeline pipeline = new LearningPipeline();
            TextLoader       testData = new TextLoader(dataPath).CreateFrom <SentimentData>(separator: '\t');

            pipeline.Add(testData);
            pipeline.Add(new TextFeaturizer("Features", "SentimentText"));
            pipeline.Add(new FastTreeBinaryClassifier());
            PredictionModel <SentimentData, SentimentPrediction> model = pipeline.Train <SentimentData, SentimentPrediction>();

            Console.WriteLine(Environment.NewLine + Environment.NewLine);

            while (true)
            {
                Console.WriteLine("Please enter some statement:" + Environment.NewLine);

                // Positive: He is the best, and the article should say that.
                // Negative: Please refrain from adding nonsense to Wikipedia.

                var input = Console.ReadLine();

                SentimentData data = new SentimentData
                {
                    SentimentText = input
                };

                SentimentPrediction prediction = model.Predict(data);
                string text = "Prediction: " + prediction.Sentiment + Environment.NewLine;
                Console.WriteLine(text);
            }
        }
Exemplo n.º 5
0
        private bool OnGetPredictSentiment(string SentimentText)
        {
            SentimentData sampleData = new SentimentData()
            {
                SentimentText = SentimentText
            };
            SentimentPrediction prediction = _modelEngine.Predict(sampleData);

            return(prediction.Prediction);
        }
Exemplo n.º 6
0
                    /// <summary>
                    /// This function predicts wheter a given string is positive or negative.
                    /// </summary>
                    /// <param name="sentence">The string to predict from.</param>
                    /// <returns>True if positive, flase if negative.</returns>
                    public (bool sentiment, double probablility, double score) CheckSingleSentiment(string sentence)
                    {
                        PredictionEngine <T1, SentimentPrediction> predictionFunction = mlEnv.Model.CreatePredictionEngine <T1, SentimentPrediction>(mlModel);
                        T1 sampleStatement = new T1 {
                            SentimentText = sentence
                        };
                        SentimentPrediction resultPrediction = predictionFunction.Predict(sampleStatement);

                        return(resultPrediction.Prediction, resultPrediction.Probability, resultPrediction.Score);
                    }
Exemplo n.º 7
0
        public ActionResult <SentimentPrediction> Post([FromBody] SentimentData data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            SentimentPrediction predictedValue = PredictionEngine.Predict(data);

            return(Ok(predictedValue));
        }
Exemplo n.º 8
0
        public static string Clasify(string _text)
        {
            mlContext = new MLContext();
            TrainTestData splitDataView = LoadData();

            model = BuildAndTrainModel(splitDataView.TrainSet);
            text  = _text;
            SentimentPrediction resultPrediction = UseModelWithSingleItem();

            return(Convert.ToBoolean(resultPrediction.Prediction) ? "Positive" : "Negative");
        }
Exemplo n.º 9
0
        public ActionResult <SentimentPrediction> Post([FromBody] SentimentData data)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            SentimentPrediction predictedValue = PredictionEnginePool.Predict(modelName: ConfigurationManager.AppSettings.Get("modelName"), example: data);

            return(Ok(predictedValue));
        }
Exemplo n.º 10
0
        public ActionResult <string> Post([FromBody] SentimentData input)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            SentimentPrediction prediction = _predictionEnginePool.Predict(input);

            string sentiment = Convert.ToBoolean(prediction.Prediction) ? "Positive" : "Negative";

            return(Ok(sentiment));
        }
Exemplo n.º 11
0
        public ActionResult <string> PredictSentiment([FromQuery] string sentimentText)
        {
            SentimentData sampleData = new SentimentData()
            {
                SentimentText = sentimentText
            };

            //Predict sentiment
            SentimentPrediction prediction = _predictionEnginePool.Predict(sampleData);

            bool   isPositive  = prediction.Sentiment;
            float  probability = CalculatePercentage(prediction.Score);
            string retVal      = $"Prediction: Postive?: '{isPositive.ToString()}' with {probability.ToString()}% probability of toxicity for the text '{sentimentText}'";

            return(retVal);
        }
Exemplo n.º 12
0
        public static void TestSinglePrediction(MLContext mlContext)
        {
            SentimentIssue sampleStatement = new SentimentIssue {
                Text = "This is a very rude movie"
            };

            ITransformer trainedModel = mlContext.Model.Load(ModelPath, out DataViewSchema modelInputSchema);

            // Create prediction engine related to the loaded trained model
            PredictionEngine <SentimentIssue, SentimentPrediction> predictionEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(trainedModel);

            //Score
            SentimentPrediction resultPrediction = predictionEngine.Predict(sampleStatement);

            Console.WriteLine($"=============== Single Prediction  ===============");
            Console.WriteLine($"Text: {sampleStatement.Text} | Prediction: {(Convert.ToBoolean(resultPrediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {resultPrediction.Probability} ");
            Console.WriteLine($"==================================================");
        }
Exemplo n.º 13
0
        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));
        }
Exemplo n.º 14
0
        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 Track(SentimentPrediction output, SentimentIssue input, ILogger logger)
        {
            try
            {
                string sentimentText = input.SentimentText;

                var props = new Dictionary <string, string>
                {
                    { "model_uri", _modelUri?.ToString() },
                    { "prediction_response", output.Prediction ? "Positive" : "Negative" },
                    { "prediction_text", sentimentText },
                };

                _telemetryClient.TrackMetric("Prediction.Probability", output.Probability, props);

                _telemetryClient.TrackMetric("Prediction.Score", output.Score, props);
            }
            catch (Exception ex)
            {
                // avoid fail prediction due to telemetry record saving issues
                logger?.LogError(ex, nameof(Track));
            }
        }
Exemplo n.º 16
0
        public ActionResult <float> PostMultiple([FromBody] ICollection <SentimentData> texts)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            float probabilityPositive = 0;
            float probabilityNegative = 0;
            int   countPositive       = 0;
            int   countNegative       = 0;

            foreach (SentimentData data in texts)
            {
                SentimentPrediction predictedValue = PredictionEnginePool.Predict(modelName: ConfigurationManager.AppSettings.Get("modelName"), example: data);
                if (predictedValue.Sentiment == true)
                {
                    probabilityPositive += predictedValue.Probability;
                    countPositive++;
                }
                else
                {
                    probabilityNegative += predictedValue.Probability;
                    countNegative++;
                }
            }

            if (probabilityNegative < probabilityPositive)
            {
                return(Ok(new Sentiment(true, probabilityPositive / countPositive)));
            }
            else
            {
                return(Ok(new Sentiment(true, probabilityNegative / countNegative)));
            }
        }
Exemplo n.º 17
0
        public ActionResult <float> PostMultiple([FromBody] ICollection <SentimentData> texts)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            float probabilityPositive = 0;
            float probabilityNegative = 0;
            int   countPositive       = 0;
            int   countNegative       = 0;

            foreach (SentimentData data in texts)
            {
                SentimentPrediction predictedValue = PredictionEngine.Predict(data);
                if (predictedValue.Prediction == true)
                {
                    probabilityPositive += predictedValue.Probability;
                    countPositive++;
                }
                else
                {
                    probabilityNegative += predictedValue.Probability;
                    countNegative++;
                }
            }

            if (probabilityPositive >= probabilityNegative)
            {
                return(Ok(new Sentiment(true, probabilityPositive / countPositive)));
            }
            else
            {
                return(Ok(new Sentiment(false, probabilityNegative / countNegative)));
            }
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
0
        static void Main(string[] args)
        {
            try
            {
                // Create MLContext
                MLContext mlContext = new MLContext();

                // Load Trained Model
                DataViewSchema predictionPipelineSchema;
                ITransformer   predictionPipeline = mlContext.Model.Load("../../../MLModels/SentimentModel.zip", out predictionPipelineSchema);

                // Create PredictionEngines
                PredictionEngine <SentimentIssue, SentimentPrediction> predictionEngine = mlContext.Model.CreatePredictionEngine <SentimentIssue, SentimentPrediction>(predictionPipeline);

                // Input Data (single)
                SentimentIssue inputData = new SentimentIssue
                {
                    Text = "I love this movie!"
                };

                // Get Prediction
                SentimentPrediction prediction = predictionEngine.Predict(inputData);

                Console.WriteLine($"=============== Single Prediction  ===============");
                Console.WriteLine($"Text: {inputData.Text} | Prediction: {(Convert.ToBoolean(prediction.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {prediction.Probability} ");
                Console.WriteLine($"================End of Process.Hit any key to exit==================================");

                // input data multiple
                SentimentIssue[] inputArray = new SentimentIssue[]
                {
                    new SentimentIssue
                    {
                        Text = "I love this movie!"
                    },
                    new SentimentIssue
                    {
                        Text = "F*****g good!"
                    },
                };

                //Load Data
                IDataView data = mlContext.Data.LoadFromEnumerable <SentimentIssue>(inputArray);

                // Predicted Data
                IDataView predictions = predictionPipeline.Transform(data);

                // Create an IEnumerable of prediction objects from IDataView
                IEnumerable <SentimentPrediction> dataEnumerable =
                    mlContext.Data.CreateEnumerable <SentimentPrediction>(predictions, reuseRowObject: true);

                // Iterate over each row
                for (int i = 0; i < inputArray.Length; i++)
                {
                    string input             = inputArray[i].Text;
                    SentimentPrediction item = dataEnumerable.ElementAt(i);
                    // display
                    Console.WriteLine($"Text: {input} | Prediction: {(Convert.ToBoolean(item.Prediction) ? "Toxic" : "Non Toxic")} sentiment | Probability of being toxic: {item.Probability} ");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.ReadLine();
        }
Exemplo n.º 20
0
 private (string text, bool sentiment) BuildPredictionResult(SentimentPrediction prediction, ISentimentRequest request)
 {
     return(text : request.SentimentText, sentiment : prediction.Sentiment);
 }
Exemplo n.º 21
0
 public JsonResult BuildJsonPredictionResult(SentimentPrediction prediction, ISentimentRequest request)
 {
     return(new JsonResult(BuildPredictionResult(prediction, request)));
 }