public static async Task <IList <SentimentResult> > Analyse(string[] sentences)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(BaseUrl);

                // Request headers.
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", AccountKey);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                var input = new SentimentInput();
                for (int i = 0; i < sentences.Count(); i++)
                {
                    input.Documents.Add(new Document()
                    {
                        Id = i + 1, Text = sentences[i]
                    });
                }


                // Request body. Insert your text data here in JSON format.
                byte[] byteData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(input));

                // Detect sentiment:
                var uri      = "text/analytics/v2.0/sentiment";
                var response = await CallEndpoint(client, uri, byteData);

                var output = JsonConvert.DeserializeObject <SentimentOutput>(response);



                return(GenerateResult(input, output));
            }
        }
Ejemplo n.º 2
0
        public Sentiment GetSentiment(string text)
        {
            var input = new SentimentInput
            {
                Text = text
            };

            var prediction = predictionEnginePool.Predict(modelName: "SentimentAnalysisModel", example: input);

            var confidence = prediction.Prediction == "0" ? prediction.Score[0] : prediction.Score[1];

            if (confidence < 0.7)
            {
                return(Sentiment.Neutral);
            }

            return((prediction.Prediction == "1") ? Sentiment.Positive : Sentiment.Negative);
        }
        private static IList <SentimentResult> GenerateResult(SentimentInput inputs, SentimentOutput outputs)
        {
            var ret = new List <SentimentResult>();

            foreach (var input in inputs.Documents)
            {
                foreach (var output in outputs.Documents)
                {
                    if (input.Id == output.Id)
                    {
                        ret.Add(new SentimentResult()
                        {
                            Sentence = input.Text, SentimentScore = output.Score
                        });
                    }
                }
            }

            return(ret);
        }