Пример #1
0
        public static MoodResponse Sentiment(string text, string messageId, string toLanguage = "en")
        {
            var client      = new HttpClient();
            var queryString = HttpUtility.ParseQueryString(text);

            // Request headers
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "79dd65f1796c482eb3b6062a91906c9a");

            var uri = "https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment?" + queryString;

            HttpResponseMessage response;

            // Request body
            string json = "{\"documents\": [{\"language\": \"en\", \"id\": \"" + messageId + "\",\"text\": \"" + text + "\"";

            byte[] byteData = Encoding.UTF8.GetBytes(json);

            using (var content = new ByteArrayContent(byteData))
            {
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response = client.PostAsync(uri, content).GetAwaiter().GetResult();
            }
            Stream       receiveStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
            StreamReader readStream    = new StreamReader(receiveStream, Encoding.UTF8);

            return(MoodResponse.FromJson(readStream.ReadToEnd()));
        }
Пример #2
0
 public static string ToJson(this MoodResponse self) => JsonConvert.SerializeObject(self, Converter.Settings);