Exemple #1
0
        public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary <string, object> suspensionState)
        {
            _result = parameter as UnconsciousBiasResult;
            //_result.PositivityGraph = new double[] { 0.924, 0.806, 0.976, 0.945, 0.999, 0.681, 0.973, 0.994, 0.958, 0.553};
            this.Message = $"Your overall average positivity towards {_result.Person} is";

            this.Value = $"{_result.Positivity.ToString()}%";
            int i = 1;

            foreach (var result  in _result.PositivityGraph)
            {
                var point = new Datapoint()
                {
                    X = i,
                    Y = result
                };
                i++;

                this.PositivityGraph.Add(point);
            }

            Views.Busy.SetBusy(false);

            await Task.CompletedTask;
        }
        //DelegateCommand _ShowBusyCommand;
        //public DelegateCommand ShowBusyCommand
        //    => _ShowBusyCommand ?? (_ShowBusyCommand = new DelegateCommand(async () =>
        //    {
        //        Views.Busy.SetBusy(true, _BusyText);
        //        await Task.Delay(5000);
        //        Views.Busy.SetBusy(false);
        //    }, () => !string.IsNullOrEmpty(BusyText)));

        public async Task GotoDetailsPage()
        {
            Views.Busy.SetBusy(true, "Getting your score...");
            UnconsciousBiasResult result = null;
            var graphClient = await AuthenticationHelper.GetAuthenticatedClientAsync();

            if (graphClient != null)
            {
                var emails = await graphClient.Me
                             .Messages
                             .Request()
                             .Search($"\"to:{Value}\"")
                             .Select("UniqueBody")
                             .GetAsync();

                StringBuilder sb = new StringBuilder();
                sb.Append("{\"documents\":[");
                int i = 1;
                foreach (var email in emails)
                {
                    Debug.WriteLine(email.UniqueBody);

                    // This is super-hacky processing out the HTML tags from the email.
                    // TODO: replace this with a proper library that will do this better.
                    string body = email.UniqueBody.Content;
                    string bodyWithoutHTMLtags = WebUtility.HtmlDecode(Regex.Replace(body, "<[^>]*(>|$)", string.Empty));
                    string step2 = Regex.Replace(bodyWithoutHTMLtags, @"[\s\r\n]+", " ");
                    sb.Append("{\"id\":\"" + i + "\",\"text\":\"" + step2 + "\"},");
                    i++;
                }

                // Remove the trailing comma to get well-formatted JSON
                string tempString = sb.ToString();
                if (tempString.LastIndexOf(",") == tempString.Length - 1)
                {
                    sb.Remove(tempString.Length - 1, 1);
                }

                // Close JSON message
                sb.Append("]}");

                List <double> sentimentScores = await TextAnalyticsHelper.GetSentiment(sb.ToString());

                // Calculate average sentiment score
                double scoreSum   = 0.0;
                int    scoreCount = 0;
                foreach (double sentimentScore in sentimentScores)
                {
                    scoreSum += sentimentScore;
                    scoreCount++;
                }
                double averageSentimentScore = scoreSum / scoreCount;
                int    sentimentPercentage   = Convert.ToInt32(averageSentimentScore * 100);

                result = new UnconsciousBiasResult()
                {
                    Positivity      = sentimentPercentage,
                    KeyWords        = "TODO",
                    Topics          = "TODO",
                    PositivityGraph = sentimentScores,
                    Person          = Value
                };
            }

            // can pass value to other screen and do fancy display
            NavigationService.Navigate(typeof(Views.DetailPage), result);

            await Task.CompletedTask;
        }