Ejemplo n.º 1
0
        private bool CheckNonPivotValidity(ILexicalSentence first, ILexicalSentence second)
        {
            GrammaticalType[] significantTypes = new GrammaticalType[] { GrammaticalType.Verb, GrammaticalType.ConjugatedVerb, GrammaticalType.Subject, GrammaticalType.DirectObject };
            global::System.Collections.Generic.IEnumerable <Tuple <ISensoryEvent, short> > firstChecks  = first.Predicate.Where(word => word.Item1?.Event != null && word.Item1.Event.Role != SubjectPivot && significantTypes.Contains(word.Item1.Event.Role));
            global::System.Collections.Generic.IEnumerable <Tuple <ISensoryEvent, short> > secondChecks = second.Subject.Where(word => word.Item1?.Event != null && word.Item1.Event.Role != PredicatePivot && significantTypes.Contains(word.Item1.Event.Role));

            global::System.Collections.Generic.IEnumerable <Tuple <ISensoryEvent, short> > firstMatches  = second.Predicate.Where(word => word.Item1?.Event != null && word.Item1.Event.Role != SubjectPivot && significantTypes.Contains(word.Item1.Event.Role));
            global::System.Collections.Generic.IEnumerable <Tuple <ISensoryEvent, short> > secondMatches = first.Subject.Where(word => word.Item1?.Event != null && word.Item1.Event.Role != PredicatePivot && significantTypes.Contains(word.Item1.Event.Role));

            bool returnValue = !PivotMatch;

            if (firstChecks.Any())
            {
                returnValue = returnValue &&
                              !PivotMatch == firstChecks.Any(wordPair => firstMatches.Any(matchPair => matchPair.Item1.Event.Role == wordPair.Item1.Event.Role &&
                                                                                          matchPair.Item1.Event.Phrase.Equals(wordPair.Item1.Event.Phrase, StringComparison.InvariantCultureIgnoreCase)));
            }

            if (secondChecks.Any())
            {
                returnValue = returnValue &&
                              !PivotMatch == secondChecks.Any(wordPair => secondMatches.Any(matchPair => matchPair.Item1.Event.Role == wordPair.Item1.Event.Role &&
                                                                                            matchPair.Item1.Event.Phrase.Equals(wordPair.Item1.Event.Phrase, StringComparison.InvariantCultureIgnoreCase)));
            }

            return(returnValue);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Whether these two sentences match this rule
        /// </summary>
        /// <param name="first">The first sentence (optimally which one we want the subject from)</param>
        /// <param name="second">The second sentence (optimally which one we want the predicate from)</param>
        /// <param name="elegance">The elegance threshold to meet</param>
        /// <returns>-1 = subject/subject match, -2 = predicate/predicate match, 0 = no match, 1 = subject/predicate match, 2 = predicate/subject match</returns>
        public short MatchesRule(ILexicalSentence first, ILexicalSentence second, int elegance)
        {
            if (EleganceThreshold > elegance ||
                (SensoryMatch && first.SensoryType != second.SensoryType) ||
                first.Language != second.Language ||
                first.Type != second.Type
                )
            {
                return(0);
            }

            Tuple <ISensoryEvent, short> firstSubjectPivot    = first.Subject.FirstOrDefault(word => word.Item1.Event.Role == SubjectPivot);
            Tuple <ISensoryEvent, short> firstPredicatePivot  = first.Predicate.FirstOrDefault(word => word.Item1.Event.Role == PredicatePivot);
            Tuple <ISensoryEvent, short> secondSubjectPivot   = second.Subject.FirstOrDefault(word => word.Item1.Event.Role == SubjectPivot);
            Tuple <ISensoryEvent, short> secondPredicatePivot = second.Predicate.FirstOrDefault(word => word.Item1.Event.Role == PredicatePivot);

            if (CheckPivotValidity(firstSubjectPivot?.Item1?.Event, secondSubjectPivot?.Item1?.Event) &&
                CheckNonPivotValidity(first, second))
            {
                return(-1);
            }

            if (CheckPivotValidity(firstPredicatePivot?.Item1?.Event, secondPredicatePivot?.Item1?.Event) &&
                CheckNonPivotValidity(first, second))
            {
                return(-2);
            }

            if (CheckPivotValidity(firstSubjectPivot?.Item1?.Event, secondPredicatePivot?.Item1?.Event) &&
                CheckNonPivotValidity(first, second))
            {
                return(1);
            }

            if (CheckPivotValidity(firstPredicatePivot?.Item1?.Event, secondSubjectPivot?.Item1?.Event) &&
                CheckNonPivotValidity(first, second))
            {
                return(2);
            }

            return(0);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a narrative description from this
        /// </summary>
        /// <returns>A long description</returns>
        public string Describe(LexicalContext overridingContext = null)
        {
            if (!string.IsNullOrWhiteSpace(Override))
            {
                return(Override);
            }

            StringBuilder sb = new StringBuilder();

            if (Sentences.Count == 0 || overridingContext != null)
            {
                Unpack(overridingContext);
            }

            List <int> removedSentences            = new List <int>();
            List <ILexicalSentence> finalSentences = new List <ILexicalSentence>();

            for (int i = 0; i < Sentences.Count(); i++)
            {
                if (removedSentences.Contains(i))
                {
                    continue;
                }

                ILexicalSentence sentence = Sentences[i];
                for (int n = i + 1; n < Sentences.Count(); n++)
                {
                    if (removedSentences.Contains(n))
                    {
                        continue;
                    }

                    ILexicalSentence secondSentence = Sentences[n];
                    foreach (SentenceComplexityRule complexityRule in sentence.Language.ComplexityRules)
                    {
                        short match = complexityRule.MatchesRule(sentence, secondSentence, overridingContext.Elegance);

                        if (match != 0)
                        {
                            finalSentences.Add(CombineSentences(sentence, secondSentence, complexityRule, match));
                            removedSentences.Add(i);
                            removedSentences.Add(n);

                            //Short circut the outer for loop
                            n = Sentences.Count();
                            break;
                        }
                    }
                }

                if (!removedSentences.Contains(i))
                {
                    finalSentences.Add(sentence);
                }
            }

            foreach (ILexicalSentence sentence in finalSentences)
            {
                sb.Append(sentence.Describe() + " ");
            }

            if (sb.Length > 0)
            {
                sb.Length -= 1;
            }

            return(sb.ToString());
        }
Ejemplo n.º 4
0
        private ILexicalSentence CombineSentences(ILexicalSentence first, ILexicalSentence second, SentenceComplexityRule rule, int matchBasis)
        {
            /// -1 = subject/subject match
            /// -2 = predicate/predicate match
            /// 0 = no match
            /// 1 = subject/predicate match
            /// 2 = predicate/subject match</returns>
            switch (matchBasis)
            {
            case 1:
                first.Predicate = second.Predicate;
                break;

            case 2:
                first.Subject = second.Subject;
                break;

            case -1:
                List <Tuple <ISensoryEvent, short> > newSubjects = new List <Tuple <ISensoryEvent, short> >();
                newSubjects.AddRange(first.Subject);
                newSubjects.AddRange(second.Subject.Where(sub => !newSubjects.Any(nSub => nSub.Item1.Event.Phrase.Equals(sub.Item1.Event.Phrase, StringComparison.InvariantCultureIgnoreCase))));

                foreach (ILexica sub in newSubjects.SelectMany(subj => subj.Item1.Event.Modifiers).Where(pre => pre.Role == GrammaticalType.Descriptive))
                {
                    sub.Context.Plural = true;
                }

                foreach (Tuple <ISensoryEvent, short> sub in newSubjects.Where(pre => pre.Item1.Event.Role == GrammaticalType.Verb || pre.Item1.Event.Role == GrammaticalType.ConjugatedVerb))
                {
                    sub.Item1.Event.Context.Plural = true;
                }

                first.Subject = newSubjects;
                break;

            case -2:
                List <Tuple <ISensoryEvent, short> > newPredicates = new List <Tuple <ISensoryEvent, short> >();
                newPredicates.AddRange(first.Predicate);
                newPredicates.AddRange(second.Predicate.Where(sub => !newPredicates.Any(nSub => nSub.Item1.Event.Phrase.Equals(sub.Item1.Event.Phrase, StringComparison.InvariantCultureIgnoreCase))));

                foreach (ILexica pred in newPredicates.SelectMany(subj => subj.Item1.Event.Modifiers).Where(pre => pre.Role == GrammaticalType.Descriptive))
                {
                    pred.Context.Plural = true;
                }

                foreach (Tuple <ISensoryEvent, short> pred in newPredicates.Where(pre => pre.Item1.Event.Role == GrammaticalType.Verb || pre.Item1.Event.Role == GrammaticalType.ConjugatedVerb))
                {
                    pred.Item1.Event.Context.Plural = true;
                }

                first.Predicate = newPredicates;
                break;
            }

            List <Tuple <ISensoryEvent, short> > newModifiers = new List <Tuple <ISensoryEvent, short> >();

            newModifiers.AddRange(first.Modifiers);
            newModifiers.AddRange(second.Modifiers);

            first.Modifiers = newModifiers;

            return(first);
        }