Ejemplo n.º 1
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) {
                List<NewsResult> newsResults = GetNewsForPlayer(string.Format("{0} {1}", player.FirstName, player.SecondName));
                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}, Date: {1:f}, Headline: {2}, Sumamry: {3} \r\n", result.SentimentScore, result.DatePublished, result.Headline, result.Summary));
                Console.ReadKey();
                }
            }

            fileName = Path.Combine(directory.FullName, "topten.json");
            SerializePlayersToFile(topTenPlayers, fileName);
		}
        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", "a26df349753c400882fa9e068d0d74dd");
            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://eastus.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.º 3
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", "value");
            //webClient.Headers.Add("Ocp-Apim-Subscription-Key", "b8e29bbd1df24254a8595a8a2d4b7632");
            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://westcentralus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment", requestBytes);
            //byte[] response = webClient.UploadData(string.Format("https://westcentralus.api.cognitive.microsoft.com/text/analytics/v2.0", requestBytes));
            string sentiments = Encoding.UTF8.GetString(response);

            sentimentResponse = JsonConvert.DeserializeObject <SentimentResponse>(sentiments);

            return(sentimentResponse);
        }
Ejemplo n.º 4
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
                });
            }

            using (var webClient = new WebClient())
            {
                //string queryString = null;
                var uri = "https://australiaeast.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment";
                webClient.Headers.Add("Ocp-Apim-Subscription-Key", Constants.TextAnalyticAccessKey);
                webClient.Headers.Add(HttpRequestHeader.Accept, "application/json");
                webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                string requestJson = JsonConvert.SerializeObject(sentimentRequest);
                byte[] requestByte = Encoding.UTF8.GetBytes(requestJson);
                byte[] response    = webClient.UploadData(uri, requestByte);

                string sentiments = Encoding.UTF8.GetString(response);
                sentimentResponse = JsonConvert.DeserializeObject <SentimentResponse>(sentiments);
            }
            return(sentimentResponse);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            string        currentDirectory = Directory.GetCurrentDirectory();
            DirectoryInfo directory        = new DirectoryInfo(currentDirectory);
            var           fileName         = Path.Combine(directory.FullName, "SoccerGameResults.csv");

            var fileContent = ReadSoccerResults(fileName);

            Console.WriteLine("Printing from Local File");
            foreach (var content in fileContent)
            {
                Console.WriteLine("{0} - Game Played: {1} - Team: {2}", content.GameDate, content.HomeORAway, content.TeamName);
                Console.WriteLine();
            }

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

            Console.WriteLine("Printing from Bing's News API");
            foreach (var player in toptenplayers)
            {
                List <NewsResults> newsResults       = GetNewsForPlayer(string.Format("{0} {1}", player.FirstName, player.LastName));
                SentimentResponse  sentimentResponse = GetSentimentResponse(newsResults);

                foreach (var sentiment in sentimentResponse.Sentiments)
                {
                    foreach (var result in newsResults)
                    {
                        if (result.Headline == sentiment.Id)
                        {
                            double score;
                            if (double.TryParse(sentiment.Score, out score))
                            {
                                result.SentimentScore = score;
                            }
                            break;
                        }
                        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));
                    }
                }
            }
            fileName = Path.Combine(directory.FullName, "topten.json");
            SerializePlayersToFile(toptenplayers, fileName);
        }
Ejemplo n.º 6
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 {
                    IDisposable = result.Headline, TextReader = result.Summary
                })
            }

            string url       = "google.com";
            var    webClient = new WebClient();

            webClient.Headers.Add(uHttpRequestHeader.HttpRequestHeader.Accept, "application/json");
            byte[] searchResults = webClient.UploadData(string.Format(url, playerName));
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            //Set current directory path
            string        currentDirectory = Directory.GetCurrentDirectory();
            DirectoryInfo directory        = new DirectoryInfo(currentDirectory);

            //Read game results from csv file
            var fileName     = Path.Combine(directory.FullName, "SoccerGameResults.csv");
            var fileContents = ReadSoccerResults(fileName);

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

            //Call BingNews Api for each player's recent news results; call TextAnalysis for sentiment
            foreach (var player in GetTopTenPlayers(players))
            {
                List <NewsResult> newsResults       = GetNewsForPlayer(string.Format("{0} {1}", player.FirstName, player.SecondName));
                SentimentResponse sentimentResponse = GetSentimentResponse(newsResults);
                foreach (var response in sentimentResponse.Responses)
                {
                    foreach (var newsResult in newsResults)
                    {
                        if (newsResult.SentimentId == response.Id)
                        {
                            newsResult.SentimentScore = response.MainSentiment;
                            break;
                        }
                    }
                }
                //Display player info and news results one at a time
                Console.WriteLine(string.Format("Player: {0} {1}", player.FirstName, player.SecondName));
                foreach (var result in newsResults)
                {
                    Console.WriteLine(string.Format("Date: {0:f}, Headline: {1}, Summary: {2}, Sentiment Score: {3}\r\n", result.DatePublished, result.Headline, result.Summary, result.SentimentScore));
                    Console.ReadKey();
                }
            }
            //Serialize top ten results to json file
            fileName = Path.Combine(directory.FullName, "topten.json");
            SerializePlayersToFile(GetTopTenPlayers(players), fileName);
        }
Ejemplo n.º 8
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.SentimentId, Text = result.Summary
                });
            }
            var webClient = new WebClient();

            webClient.Headers.Add("Ocp-Apim-Subscription-Key", "364c7692b83247139c69060b3e40d396");
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
            string requestJson = JsonConvert.SerializeObject(sentimentRequest);

            byte[] requestBytes = Encoding.UTF8.GetBytes(requestJson);
            string endpoint     = "cyadehn-th-textanalytics.cognitiveservices.azure.com";

            byte[] response;
            try
            {
                response = webClient.UploadData(string.Format("https://{0}/text/analytics/v3.0/sentiment", endpoint), requestBytes);
                ITraceWriter traceWriter = new MemoryTraceWriter();
                string       sentiments  = Encoding.UTF8.GetString(response);
                sentimentResponse = JsonConvert.DeserializeObject <SentimentResponse>(sentiments, new JsonSerializerSettings {
                    TraceWriter = traceWriter
                });
                Console.Write(traceWriter);
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(sentimentResponse);
        }
        static void Main(string[] args)
        {
            string currentDirectory = Directory.GetCurrentDirectory();

            DirectoryInfo directory = new DirectoryInfo(currentDirectory);
            var           fileName  = Path.Combine(directory.FullName, "SoccerGameResults.csv");
            // Console.WriteLine(fileName);

            // var file = new FileInfo(fileName);
            // Console.WriteLine(file);
            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 + " PPG: " + player.PointsPerGame);
                List <NewsResult> newsResults       = GetNews(string.Format("{0} {1}", player.FirstName, player.SecondName));
                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: {3:p}, Date: {0}, Headline {1}, Summary {2} \r\n", result.DatePublished, result.Headline, result.Summary, result.SentimentScore));
                }
                Console.ReadKey();
            }
            fileName = Path.Combine(directory.FullName, "topten.json");
            // Console.WriteLine(GetGoogleHomePage("Diego Valeri"));
            SerializePlayerToFile(topTenPlayers, fileName);
            // Console.WriteLine(fileContents);
            // string[] fil	eLines = fileContents.Split(new char[]{'\r','\n'}, StringSplitOptions.RemoveEmptyEntries);
            // foreach(var line in fileLines)
            // {
            //  Console.WriteLine(line);

            // }
            // if(file.Exists)
            // {

            //  using(var reader = new StreamReader(fileName))
            //  {
            //      Console.SetIn(reader);
            //      Console.WriteLine(Console.ReadLine());
            //  }
            // }



            // var mysteryMessage = new byte[] { 89, 0, 97, 0, 121, 0, 33, 0 };
            // var messageContents = UnicodeEncoding.Unicode.GetString(mysteryMessage);
            // Console.WriteLine(messageContents);
        }
Ejemplo n.º 10
0
        public static void Main(string[] args)
        {
            string        currentDirectory = Directory.GetCurrentDirectory();
            DirectoryInfo directory        = new DirectoryInfo(currentDirectory);
            //var files = directory.GetFiles("*.txt");
            //foreach (var file in files)
            //{
            //    Console.WriteLine(file.Name);
            //}

            var fileName     = Path.Combine(directory.FullName, "SoccerGameResults.csv");
            var fileContents = ReadSoccerResults(fileName);

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

            //foreach (var player in players)
            //{
            //    Console.WriteLine(player.FirstName);
            //}

            //var topTenPlayers = GetTopTenPlayers(players);
            //foreach (var player in topTenPlayers)
            //{
            //    Console.WriteLine("Name: " + player.FirstName + " PPG: " + player.PointsPerGame);
            //}

            var topTenPlayers = GetTopTenPlayers(players);

            foreach (var player in topTenPlayers)
            {
                List <NewsResult> newsResults       = GetNewsForPlayer(string.Format("{0} {1}", player.FirstName, player.SecondName));
                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;
                        }
                    }
                    //Console.WriteLine(string.Format("Date: {0:f}, Headline: {1}, Summary: {2} \r\n", result.DatePublished, result.Headline, result.Summary));
                    //Console.ReadKey();
                }

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

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

            //var fileContents = ReadFile(fileName);
            //string[] fileLines = fileContents.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            //foreach (var line in fileLines)
            //{
            //    Console.WriteLine(line);
            //}

            //Console.WriteLine(fileContents);
            //var file = new FileInfo(filename);
            //if (file.Exists)
            //{
            //    //File will be closed when this reaches the end
            //    using (var reader = new StreamReader(file.FullName))
            //    {
            //        Console.SetIn(reader);
            //        Console.WriteLine(Console.ReadLine());
            //    }
            //    Console.ReadLine();
            //}

            //Console.WriteLine(GetGoogleHomePage());
            //Console.WriteLine(GetNewsForPlayer("Dieog Valeri"));
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            string        currentDircetory = Directory.GetCurrentDirectory();
            DirectoryInfo directory        = new DirectoryInfo(currentDircetory);
            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 players)
            {
                Console.WriteLine(
                    String.Format(
                        "Team: {0}, ID: {1}, First name: {2}, Last name: {3}, Points per game: {4}",

                        player.TeamName,
                        player.Id,
                        player.FirstName,
                        player.LastName,
                        player.PointsPerGame
                        )
                    );
            }

            Console.WriteLine("\n The top ten players of the season are: \n");

            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}, \n Date: {1:f},\n Headline: {2}, \n Summary: \n\n {3} \r\n",
                            result.SentimentScore,
                            result.DatePublished,
                            result.HeadLine,
                            result.Summary
                            ));
                    Console.ReadKey();
                }
            }
            //fileName = Path.Combine(directory.FullName, "topTenPlayers.json");
            //SerialzePlayersToFile(topTenPlayers, fileName);
        }