Exemplo n.º 1
0
        public async Task Location(IDialogContext context, LuisResult result)
        {
            string message = ""; string levelOfGeography = "";
            Tuple <string, string> subjectTuple;
            var      conversation        = context.Activity as Activity;
            Activity replyToConversation = conversation.CreateReply(message);

            replyToConversation.Type = "message";

            var entities    = new List <EntityRecommendation>(result.Entities);
            int resultCount = EntityHandler.GetResultCount(entities);

            subjectTuple = EntityHandler.DetermineSubject(entities);
            string fullSubjectStr = subjectTuple.Item2;

            if (fullSubjectStr == "B")
            {
                replyToConversation.Text = "Sorry, unable to determine what subject you're asking about";
                await context.PostAsync(replyToConversation);

                context.Wait(MessageReceived); return;
            }
            string      queryTopic = DataSubjects.Topics.First(t => t.Item2 == fullSubjectStr.Substring(1, 2)).Item1;
            QueryObject _queryObj  = new QueryObject(fullSubjectStr);

            if (entities.Any((entity) => entity.Type == "level_of_geography"))
            {
                levelOfGeography = entities.Where(e => e.Type == "level_of_geography")
                                   .First().Entity;
                Parser.ParseLevelOfGeography(levelOfGeography, _queryObj, _dataTypes);
                if (!_dataTypes.ResolveSuccess)
                {
                    replyToConversation.Text = $"{_dataTypes.Message}: '{levelOfGeography}'";
                    await context.PostAsync(replyToConversation);

                    context.Wait(MessageReceived);
                    return;
                }
            }
            if (entities.Any((entity) => entity.Type == "location"))
            {
                string locationEntity = entities.Where(e => e.Type == "location")
                                        .First().Entity.ToLower();
                LevelsOfGeography lg = Parser.ParseLocation(locationEntity, _queryObj);
                if (lg.ResolveSuccess)
                {
                    _queryObj.Location = $"{_queryObj.LocationLevel}:{lg.LocationCode}";
                }
                else
                {
                    replyToConversation.Text = $"{lg.Message}: '{locationEntity}'";
                    await context.PostAsync(replyToConversation);

                    context.Wait(MessageReceived);
                    return;
                }
            }

            await context.PostAsync(replyToConversation);

            string apiResponse = await _queryObj.QueryWeb();

            var            resultObjects = Parser.ParseToObjects(apiResponse, _queryObj);
            List <JObject> newObjects    = new List <JObject>();

            try
            {
                if (entities.Any(e => e.Type == "ascending"))
                {
                    newObjects = resultObjects
                                 .OrderBy(x => Convert.ToInt32(x[fullSubjectStr])).Take(resultCount).ToList();
                    for (var i = 0; i < newObjects.Count(); i++)
                    {
                        if (i == 0)
                        {
                            replyToConversation.Text = $"{newObjects[i]["NAME"].ToString().Replace("  ", "%").Split('%')[0]} has the least {subjectTuple.Item1} with {newObjects[i][fullSubjectStr]}.  \r\n";
                        }
                        else if (i == newObjects.Count() - 1)
                        {
                            replyToConversation.Text += $"Finally, {newObjects[i]["NAME"].ToString().Replace("  ","%").Split('%')[0]} has {subjectTuple.Item1} with {newObjects[i][fullSubjectStr]}. ";
                        }
                        else
                        {
                            //randomize conjunctions to make more natural syntax
                            replyToConversation.Text += $"{PartsOfSpeech.GetConjunctiveAdverb()} {newObjects[i]["NAME"].ToString().Replace("  ", "%").Split('%')[0]} with {newObjects[i][fullSubjectStr]}.  \r\n";
                        }
                    }
                }
                else if (entities.Any(e => e.Type == "descending"))
                {
                    newObjects = resultObjects
                                 .OrderByDescending(x => Convert.ToInt32(x[fullSubjectStr])).Take(resultCount).ToList();
                    for (var i = 0; i < newObjects.Count(); i++)
                    {
                        if (i == 0)
                        {
                            replyToConversation.Text = $"{newObjects[i]["NAME"].ToString().Replace("  ", "%").Split('%')[0]} has the most {subjectTuple.Item1} with {newObjects[i][fullSubjectStr]}.  \r\n";
                        }
                        else if (i == newObjects.Count() - 1)
                        {
                            replyToConversation.Text += $"Finally, {newObjects[i]["NAME"].ToString().Replace("  ", "%").Split('%')[0]} has {newObjects[i][fullSubjectStr]}.  ";
                        }
                        else
                        {
                            //randomize conjunctions to make more natural syntax
                            replyToConversation.Text += $"{PartsOfSpeech.GetConjunctiveAdverb()} {newObjects[i]["NAME"].ToString().Replace("  ", "%").Split('%')[0]} with {newObjects[i][fullSubjectStr]}.  \r\n";
                        }
                    }
                }
                var json = JsonConvert.SerializeObject(newObjects);
                //replyToConversation.Text = json;
            }
            catch (Exception e)
            {
                replyToConversation.Text = "Sorry, I ran into an error trying to serialize my response";
                Console.WriteLine(e.Message);
            }

            await context.PostAsync(replyToConversation);

            context.Wait(MessageReceived);
        }