Exemplo n.º 1
0
        public static SentimentResponse GetSentimentResponse(List <NewsResult> newsResults)
        {
            var sentimentResponse = new SentimentResponse();
            var sentimentRequest  = new SentimentRequest();

            sentimentRequest.Documents = new List <Document>();
            foreach (var result in newsResults)
            {
                sentimentRequest.Documents.Add(new Document {
                    Id = result.Headline, Text = result.Summary
                });
            }

            var webClient = new WebClient();

            //webClient.Headers.Add("Ocp-Apim-Subscription-Key", "0f2573c2204648789be5302eee10aa37"); //for bing search 7.0
            webClient.Headers.Add("Ocp-Apim-Subscription-Key", "fe924fc1af744771b2a49487124cb777");
            webClient.Headers.Add(HttpRequestHeader.Accept, "application/json");
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
            string requestJson = JsonConvert.SerializeObject(sentimentRequest);

            byte[] requestBytes = Encoding.UTF8.GetBytes(requestJson);
            byte[] response     = webClient.UploadData("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment", requestBytes);
            string sentiments   = Encoding.UTF8.GetString(response);

            sentimentResponse = JsonConvert.DeserializeObject <SentimentResponse>(sentiments);
            return(sentimentResponse);
        }
Exemplo n.º 2
0
        private static void Main(string[] args)
        {
            string        currentDirectory = Directory.GetCurrentDirectory();
            DirectoryInfo directory        = new DirectoryInfo(currentDirectory);
            var           fileName         = Path.Combine(directory.FullName, "SoccerGameResults.csv");
            var           fileContents     = ReadSoccerResults(fileName);

            fileName = Path.Combine(directory.FullName, "players.json");
            var players       = DeserializePlayers(fileName);
            var topTenPlayers = GetTopTenPlayers(players);

            foreach (var player in topTenPlayers)
            {
                List <NewsResult> newsResults       = GetNewsForPlayer(string.Format("{0} {1}", player.FirstName, player.LastName));
                SentimentResponse sentimentResponse = GetSentimentResponse(newsResults);
                foreach (var sentiment in sentimentResponse.Sentiments)
                {
                    foreach (var newsResult in newsResults)
                    {
                        if (newsResult.Headline == sentiment.Id)
                        {
                            double score;
                            if (double.TryParse(sentiment.Score, out score))
                            {
                                newsResult.SentimentScore = score;
                            }

                            break;
                        }
                    }
                }
                foreach (var result in newsResults)
                {
                    Console.WriteLine(string.Format("Sentiment Score: {0:p}, Date: {1:f}, Headline: {2}, Summary: {3}\r\n", result.SentimentScore, result.DatePublished, result.Headline, result.Summary));
                }

                Console.ReadKey();
            }

            fileName = Path.Combine(directory.FullName, "topten.json");
            SerializePlayersToFile(topTenPlayers, fileName);
        }