public static string GetPhraseText(this IEnumerable <ReversedToken> list)
        {
            if (list != null)
            {
                var           builder   = new StringBuilder();
                ReversedToken lastToken = null;
                foreach (var token in list)
                {
                    var firstChar = token.OriginalToken.Text.Content[0];
                    switch (firstChar)
                    {
                    case '-':
                    case '!':
                    case '?':
                    case '\'':
                    case '.':
                    case ',':
                        builder.Append(token.OriginalToken.Text.Content);
                        break;

                    default:
                        if (builder.Length > 0 && lastToken != null && lastToken.OriginalToken.Text.Content != "-")
                        {
                            builder.Append(" ");
                        }
                        builder.Append(token.OriginalToken.Text.Content);
                        break;
                    }
                    lastToken = token;
                }
                return(builder.ToString());
            }
            return(null);
        }
 public static List <string> AddIfNotNull(this List <string> list, ReversedToken element)
 {
     if (element != null)
     {
         list.Add(element.OriginalToken.Text.Content);
     }
     return(list);
 }
Beispiel #3
0
 public static string GetTokenText(ReversedToken reversedToken)
 {
     return(reversedToken?.OriginalToken?.Text?.Content ?? "Unknown");
 }
        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
            });
        }