示例#1
0
        public static SentimentResponse GetSentimentResponse(List <NewsResults> 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", "");
            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://(customSubDomain).cognitiveservices.azure.com/text/analytics/v3.0/sentiment", requestBytes);
            string sentiments   = Encoding.UTF8.GetString(response);

            sentimentResponse = JsonConvert.DeserializeObject <SentimentResponse>(sentiments);
            return(sentimentResponse);
        }
示例#2
0
        static void Main(string[] args)
        {
            string        currentDirectory = Directory.GetCurrentDirectory();
            DirectoryInfo directory        = new DirectoryInfo(currentDirectory);
            var           fileName         = Path.Combine(directory.FullName, "people.txt");
            var           fileContents     = ReadAcctLines(fileName);

            var jsonFileName   = Path.Combine(directory.FullName, "users.json");
            var users          = DeserializeUers(jsonFileName);
            var paperlessUsers = getPaperlessUsers(users);

            foreach (var u in paperlessUsers)
            {
                Console.WriteLine($"Electronic billing is enabled for account {u.AccountNumber}");
            }

            var newJsonFileName = Path.Combine(directory.FullName, "paperlessUsers.json");

            SerializeUsersToFile(paperlessUsers, newJsonFileName);



            //Console.WriteLine(GetGoogleHomePage());

            List <NewsResults> newsResults       = GetNews("healthcare");
            SentimentResponse  sentimentResponse = GetSentimentResponse(newsResults);

            foreach (var s in sentimentResponse.SentimentDocument)
            {
                foreach (var r in newsResults)
                {
                    if (r.Headline == s.id)
                    {
                        r.SentimentScore = (double)s.confidenceScores.positive;
                        break;
                    }
                }
            }
            foreach (var result in newsResults)
            {
                Console.WriteLine(string.Format("Sentiment Score: {0:P}, Date: {1:f}, Headline: {2},\r Summary: {3} \r\n", result.SentimentScore, result.DatePublished, result.Headline, result.Summary));
            }

            // --- get file list ---
            //var files = directory.GetFiles("*.txt");
            //foreach (var file in files)
            //{
            //    Console.WriteLine(file.Name);
            //}

            // --- return line1 from file ---
            //var file = new FileInfo(fileName);
            //if (file.Exists)
            //{
            //    using (var reader = new StreamReader(file.FullName))
            //    {
            //        Console.SetIn(reader);
            //        Console.WriteLine(Console.ReadLine());
            //    }
            //}

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