Ejemplo 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", "e48136dd75ed4e45ba7879cd9737dd92");
            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);
        }
Ejemplo n.º 2
0
        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)
            //{
            //    Console.WriteLine("Name: " + player.FirstName + " " + player.LastName + " PPG: " + player.pointsPerGame);
            //}
            fileName = Path.Combine(directory.FullName, "topten.json");
            SerializePlayersToFile(topTenPlayers, fileName);
            // Console.WriteLine(GetGoogleHomePage());
            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("Date: {0:f} \r\nHeadline: {1} \r\nSummary: {2} \r\nSentiment Score: {3:p} \r\n", result.DatePublished, result.HeadLine, result.Summary, result.SentimentScore));
                    Console.ReadKey();
                }
            }
        }