public static List <SentenceSummary> AssignGeneralQuestionAnswer(SentenceSummary question, List <SentenceSummary> summaries)
        {
            var answer      = summaries[question.Position + 1];
            var answerIsYes = answer.Phrase.Any(x => Constants.AffirmativeAnswerWords.CaseInsensitiveContains(TextAnalysisUtils.GetTokenText(x)));
            var answerIsNo  = answer.Phrase.Any(x => Constants.NegativeAnswerWords.CaseInsensitiveContains(TextAnalysisUtils.GetTokenText(x)));
            var extras      = new List <SentenceSummary>();

            if (answerIsYes || (!answerIsYes && !answerIsNo))
            {
                var phrases = summaries
                              .Skip(question.Position + 1)
                              .TakeWhile(x => !Constants.InitialWordsToIgnore.CaseInsensitiveContains(TextAnalysisUtils.GetTokenText(x.Phrase.First())) &&
                                         x.Answer != null);

                var fullAnswer = new StringBuilder();
                foreach (var summary in phrases)
                {
                    fullAnswer.Append(summary.Phrase.SkipWhile(x =>
                                                               Constants.AffirmativeAnswerWords.CaseInsensitiveContains(TextAnalysisUtils.GetTokenText(x)) ||
                                                               TextAnalysisUtils.GetTokenText(x).CaseInsensitiveEquals(" ") ||
                                                               TextAnalysisUtils.GetTokenText(x).CaseInsensitiveEquals(",") ||
                                                               TextAnalysisUtils.GetTokenText(x).CaseInsensitiveEquals("."))
                                      .GetPhraseText()
                                      );
                }
                fullAnswer.Replace("was n't", "wasn't").Replace("do n't", "don't");

                var keywords = question.KeyWords;
                if (!answerIsYes && !answerIsNo)
                {
                    keywords.Add("indecision");
                }

                if (fullAnswer.Length > 0 && (answerIsYes || !keywords.CaseInsensitiveContains("indecision")))
                {
                    extras.Add(new SentenceSummary()
                    {
                        KeyWords = keywords, CombinedAnswerText = fullAnswer.ToString(), Position = question.Position
                    });
                }
            }

            question.CombinedAnswerText = answer.Phrase.GetPhraseText();
            return(extras);
        }
        public static SentenceSummary SummarizeAnswer(List <ReversedToken> reversedTokens)
        {
            var summary = new SentenceSummary()
            {
                Phrase   = reversedTokens,
                Names    = TextAnalysisUtils.FindNames(reversedTokens),
                Type     = SentenceType.Answer,
                KeyWords = new List <string>()
                           .AddIfNotNull(reversedTokens.Where(x => x.OriginalToken.PartOfSpeech.Tag == PartOfSpeech.Types.Tag.X).ToList()),
                Answer = new Answer()
                {
                    HasAffirmation = reversedTokens.Any(x => Constants.AffirmativeAnswerWords.CaseInsensitiveContains(TextAnalysisUtils.GetTokenText(x))),
                    Numbers        = TextAnalysisUtils.FindNumbers(reversedTokens),
                }
            };

            return(summary);
        }
        public static List <SentenceSummary> AssignCityAnswer(SentenceSummary question, List <SentenceSummary> summaries)
        {
            var answer = summaries
                         .Skip(question.Position + 1)
                         .TakeWhile(x => !Constants.InitialWordsToIgnore.CaseInsensitiveContains(TextAnalysisUtils.GetTokenText(x.Phrase.First())) &&
                                    x.Answer != null);
            var extras = new List <SentenceSummary>();

            var fullAnswerText   = answer.First().Phrase.GetPhraseText();
            var lastCharPosition = fullAnswerText.Length - 1;
            var firstComma       = fullAnswerText.IndexOf(',');

            var cityPart = answer.First().Phrase.TakeWhile(x => !TextAnalysisUtils.GetTokenText(x).CaseInsensitiveEquals(","));
            var city     = TextAnalysisUtils.FindNames(cityPart.ToList()).First();
            var state    = null as string;

            if (firstComma != -1)
            {
                state = fullAnswerText.Substring(firstComma + 1, lastCharPosition - firstComma - 1);
            }

            if (state != null)
            {
                extras.Add(new SentenceSummary()
                {
                    KeyWords = new List <string>()
                    {
                        "state"
                    }, CombinedAnswerText = state, Position = question.Position
                });
            }

            question.CombinedAnswerText = city;
            if (!question.KeyWords.CaseInsensitiveContains("city"))
            {
                question.KeyWords.Add("city");
            }

            return(extras);
        }
        public static SentenceSummary SummarizeFacts(List <ReversedToken> reverseTokens, int index, List <SentenceSummary> summaries)
        {
            SentenceSummary summary = null;
            var             phrase  = reverseTokens.GetPhraseText();

            if (Constants.AnyQuestionWords.CaseInsensitiveContains(TextAnalysisUtils.GetTokenText(reverseTokens.First())))
            {
                if (Constants.FollowUpQuestionWords.Any(x => phrase.StartsWith(x, StringComparison.InvariantCultureIgnoreCase)))
                {
                    if (summaries.GetLastQuestion()?.GeneralQuestion != null)
                    {
                        summary = SummarizeGeneralQuestion(reverseTokens);
                    }
                    else if (summaries.GetLastQuestion()?.SpecialQuestion != null)
                    {
                        summary = SummarizeSpecialQuestion(reverseTokens);
                    }
                }
                else if (Constants.SpecialQuestionWords.Contains(reverseTokens.First().OriginalToken.Text.Content))
                {
                    summary = SummarizeSpecialQuestion(reverseTokens);
                }
                else if (Constants.GeneralQuestionWords.Contains(reverseTokens.First().OriginalToken.Text.Content))
                {
                    summary = SummarizeGeneralQuestion(reverseTokens);
                }
            }
            else
            {
                summary = SummarizeAnswer(reverseTokens);
            }

            if (summary != null)
            {
                summary.Position = index;
            }

            return(summary);
        }
        public static SentenceSummary SummarizeSpecialQuestion(List <ReversedToken> reverseTokens)
        {
            var root            = reverseTokens.FirstOrDefault(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Root);
            var rootRedirection = reverseTokens.FirstOrDefault(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Prep && root.Relations.Any(rel => rel == x.OriginalToken));
            var type            = root.Relations.FirstOrDefault(x => x.PartOfSpeech.Tag == PartOfSpeech.Types.Tag.Verb || x.PartOfSpeech.Tag == PartOfSpeech.Types.Tag.Adv || x.PartOfSpeech.Tag == PartOfSpeech.Types.Tag.Det);
            var subject         = reverseTokens.FirstOrDefault(x => x.OriginalToken.PartOfSpeech.Tag == PartOfSpeech.Types.Tag.Pron && x.Relations.Any(rel => rel == root.OriginalToken));

            if (subject == null)
            {
                subject = reverseTokens.FirstOrDefault(x => x.OriginalToken.PartOfSpeech.Tag == PartOfSpeech.Types.Tag.Noun && root.Relations.Any(rel => rel == x.OriginalToken));
                if (subject == null)
                {
                    subject = root;
                }
            }
            else if (rootRedirection != null && subject.OriginalToken.PartOfSpeech.Tag == PartOfSpeech.Types.Tag.Pron)
            {
                subject = reverseTokens.FirstOrDefault(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Pobj && rootRedirection.Relations.Any(rel => rel == x.OriginalToken));
            }
            var           subjectSpecificationPointer = reverseTokens.FirstOrDefault(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Prep && subject.Relations.Any(rel => rel == x.OriginalToken));
            ReversedToken subjectSpecification        = null;

            if (subjectSpecificationPointer != null)
            {
                subjectSpecification = reverseTokens.FirstOrDefault(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Pobj && subjectSpecificationPointer.Relations.Any(rel => rel == x.OriginalToken));
            }
            else
            {
                subjectSpecification = reverseTokens.FirstOrDefault(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Nn && subject.Relations.Any(rel => rel == x.OriginalToken));
            }
            if (subjectSpecification == null)
            {
                subjectSpecification = reverseTokens.FirstOrDefault(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Amod && subject.Relations.Any(rel => rel == x.OriginalToken));
            }

            var           subjectOwner = reverseTokens.FirstOrDefault(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Poss && subject.Relations.Any(rel => rel == x.OriginalToken));
            ReversedToken subjectSpecificationOwner = null;

            if (subjectSpecification != null)
            {
                subjectSpecificationOwner = reverseTokens.FirstOrDefault(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Nn && subjectSpecification.Relations.Any(rel => rel == x.OriginalToken));
            }
            var ownerAlternatives = new List <ReversedToken>()
            {
                subjectOwner, subjectSpecificationOwner
            };
            var ownerCanBeEitherAlternative = false;

            if (subjectOwner != null)
            {
                ownerAlternatives.AddRange(reverseTokens.Where(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Conj && subjectOwner.Relations.Any(rel => rel == x.OriginalToken)));
                if (ownerAlternatives.Any())
                {
                    ownerCanBeEitherAlternative = reverseTokens.Any(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Cc && x.OriginalToken.Text.Content.Equals("or", StringComparison.InvariantCultureIgnoreCase) && subjectOwner.Relations.Any(rel => rel == x.OriginalToken));
                }
            }
            if (subjectSpecificationOwner != null)
            {
                ownerAlternatives.AddRange(reverseTokens.Where(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Conj && subjectSpecificationOwner.Relations.Any(rel => rel == x.OriginalToken)));
                if (ownerAlternatives.Any())
                {
                    ownerCanBeEitherAlternative = reverseTokens.Any(x => x.OriginalToken.DependencyEdge.Label == DependencyEdge.Types.Label.Cc && x.OriginalToken.Text.Content.Equals("or", StringComparison.InvariantCultureIgnoreCase) && subjectSpecificationOwner.Relations.Any(rel => rel == x.OriginalToken));
                }
            }
            var alternativeModifierText = ownerAlternatives.Count > 1 ? (ownerCanBeEitherAlternative ? " any" : " all") : "";

            var specialQuestion = new SpecialQuestion
            {
                Subject = subject,
                SubjectSpecification = subjectSpecification,
                ResponseOwnershipRestrictionAlternatives   = ownerAlternatives,
                ResponseOwnershipRestrictionAnyAlternative = ownerCanBeEitherAlternative,
            };

            return(new SentenceSummary
            {
                KeyWords = new List <string>()
                           .AddIfNotNull(root)
                           .AddIfNotNull(subject)
                           .AddIfNotNull(subjectSpecification)
                           .AddIfNotNull(ownerAlternatives),
                Phrase = reverseTokens,
                Names = TextAnalysisUtils.FindNames(reverseTokens),
                Type = SentenceType.SpecialQuestion,
                SpecialQuestion = specialQuestion
            });
        }
        public static List <SentenceSummary> AssignEmergencyContactInfoAnswer(SentenceSummary question, List <SentenceSummary> summaries)
        {
            var name    = summaries[question.Position + 1].Names.FirstOrDefault();
            var phrases = summaries
                          .Skip(question.Position + 1)
                          .TakeWhile(x => !Constants.InitialWordsToIgnore.CaseInsensitiveContains(TextAnalysisUtils.GetTokenText(x.Phrase.First())) &&
                                     x.Answer != null);

            var extras = new List <SentenceSummary>();

            var relationshipPhrase = phrases.FirstOrDefault(x => Constants.RelationshipTypes.CaseInsensitiveContains(x.Phrase.Select(x => x.OriginalToken.Text.Content)));

            if (relationshipPhrase != null)
            {
                var fullPhrase = relationshipPhrase.Phrase.GetPhraseText();
                foreach (var relationship in Constants.RelationshipTypes)
                {
                    if (fullPhrase.CaseInsensitiveContains(relationship))
                    {
                        extras.Add(new SentenceSummary()
                        {
                            KeyWords = new List <string>()
                            {
                                "relationship"
                            }, CombinedAnswerText = relationship, Position = question.Position
                        });
                    }
                }
            }

            question.CombinedAnswerText = name ?? "Not disclosed";
            return(extras);
        }
        public static List <SentenceSummary> AssignAddressAnswer(SentenceSummary question, List <SentenceSummary> summaries)
        {
            var answer = summaries
                         .Skip(question.Position + 1)
                         .TakeWhile(x => !Constants.InitialWordsToIgnore.CaseInsensitiveContains(TextAnalysisUtils.GetTokenText(x.Phrase.First())) &&
                                    x.Answer != null);
            var extras = new List <SentenceSummary>();

            var fullAnswerText   = answer.First().Phrase.GetPhraseText();
            var lastCharPosition = fullAnswerText.Length - 1;
            var firstComma       = fullAnswerText.IndexOf(',');
            var secondComma      = firstComma == -1 ? -1 : fullAnswerText.IndexOf(',', firstComma);

            var address = fullAnswerText.Substring(0, firstComma == -1 ? lastCharPosition : firstComma);
            var city    = null as string;
            var state   = null as string;

            if (firstComma != -1)
            {
                city = fullAnswerText.Substring(firstComma + 1, secondComma == -1 ? lastCharPosition - firstComma : secondComma);
            }
            if (secondComma != -1)
            {
                state = fullAnswerText.Substring(secondComma + 1, lastCharPosition - secondComma);
            }

            if (city != null)
            {
                extras.Add(new SentenceSummary()
                {
                    KeyWords = new List <string>()
                    {
                        "city"
                    }, CombinedAnswerText = city, Position = question.Position
                });
            }
            if (state != null)
            {
                extras.Add(new SentenceSummary()
                {
                    KeyWords = new List <string>()
                    {
                        "state"
                    }, CombinedAnswerText = state, Position = question.Position
                });
            }

            question.CombinedAnswerText = address;

            return(extras);
        }
        public static void AssignFreeformAnswer(SentenceSummary question, List <SentenceSummary> summaries)
        {
            var answer = summaries
                         .Skip(question.Position + 1)
                         .TakeWhile(x => !Constants.InitialWordsToIgnore.CaseInsensitiveContains(TextAnalysisUtils.GetTokenText(x.Phrase.First())) &&
                                    x.Answer != null);

            var fullAnswer = new StringBuilder();

            foreach (var summary in answer)
            {
                fullAnswer.Append(summary.Phrase.GetPhraseText());
            }

            question.CombinedAnswerText = fullAnswer.ToString();
        }