Example #1
0
 public static Pattern MorphematicAdPosition(string patternName, MorphemeRule morphemeRule, MorphemeRule leftRule, MorphemeRule rightRule)
 => new Pattern(patternName)
 {
     UpRule    = morphemeRule,
     LeftRule  = leftRule,
     RightRule = rightRule,
 };
Example #2
0
 public static Pattern EpsilonAdPosition(string patternName, MorphemeRule leftRule, MorphemeRule rightRule)
 => new Pattern(patternName)
 {
     UpRule    = MorphemeRule.Epsilon,
     LeftRule  = leftRule,
     RightRule = rightRule,
 };
Example #3
0
 public Pattern(Pattern pattern)
     : this(pattern.Name, pattern.Attributes)
 {
     Description     = pattern.Description;
     UpRule          = new MorphemeRule(pattern.UpRule);
     UpAttributes    = pattern.UpAttributes;
     LeftRule        = new MorphemeRule(pattern.LeftRule);
     LeftAttributes  = pattern.LeftAttributes;
     RightRule       = new MorphemeRule(pattern.RightRule);
     RightAttributes = pattern.RightAttributes;
     ValencyPosition = pattern.ValencyPosition;
     IsLeftFirst     = pattern.IsLeftFirst;
 }
Example #4
0
        public void CopyConstructor()
        {
            var morphemeRule = new MorphemeRule(GrammarCharacter.I, RuleMaker.Anything <string>(), MaskRule.Is(EnglishAttributes.I.Lexeme.Interjection));

            morphemeRule.SetSubstitution(SubstitutionRules.Epsilon_U_E);

            var copy = new MorphemeRule(morphemeRule);

            Assert.AreEqual(morphemeRule.GrammarCharacter, copy.GrammarCharacter);
            Assert.IsTrue(morphemeRule.MorphRule.Equals(copy.MorphRule));
            Assert.IsTrue(morphemeRule.AttributesRule.Equals(copy.AttributesRule));
            Assert.IsTrue(morphemeRule.SubstitutionRule.Equals(copy.SubstitutionRule));

            Assert.IsTrue(morphemeRule.Equals(copy));
        }
Example #5
0
        private static bool IsSubruleOf(this MorphemeRule morphemeRule, MorphemeRule other)
        {
            if (other.GrammarCharacter == GrammarCharacter.e ||
                other.GrammarCharacter == morphemeRule.GrammarCharacter)
            {
                if (other.MorphRule.Equals(MorphRules.Anything) ||
                    morphemeRule.MorphRule.IsSubruleOf(other.MorphRule))
                {
                    if (other.AttributesRule.Equals(MaskRule.Anything) ||
                        morphemeRule.AttributesRule.IsSubruleOf(other.AttributesRule))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #6
0
        public void IsMatch()
        {
            IAttributesModel attributesModel = new EnglishAttributesModel();

            // Or
            MorphemeRule morphemeRule = new MorphemeRule(GrammarCharacter.I, RuleMaker.Anything <string>(), MaskRule.Is(EnglishAttributes.I.Lexeme.Interjection).Or(MaskRule.Is(EnglishAttributes.I.Lexeme.Verb)));

            Assert.IsTrue(morphemeRule.Evaluate(new Morpheme(attributesModel, "", EnglishAttributes.I.Lexeme.Verb)));
            Assert.IsTrue(morphemeRule.Evaluate(new Morpheme(attributesModel, "", EnglishAttributes.I.Lexeme.Interjection)));
            Assert.IsTrue(morphemeRule.Evaluate(new Morpheme(attributesModel, "", EnglishAttributes.I.Lexeme.Verb | EnglishAttributes.I.Lexeme.Interjection)));
            Assert.IsFalse(morphemeRule.Evaluate(new Morpheme(attributesModel, "", EnglishAttributes.O)));

            // And
            morphemeRule = new MorphemeRule(GrammarCharacter.I, RuleMaker.Anything <string>(), MaskRule.Is(EnglishAttributes.I.Lexeme.Interjection).And(MaskRule.Is(EnglishAttributes.I.Lexeme.Verb)));
            Assert.IsFalse(morphemeRule.Evaluate(new Morpheme(attributesModel, "", EnglishAttributes.I.Lexeme.Verb)));
            Assert.IsFalse(morphemeRule.Evaluate(new Morpheme(attributesModel, "", EnglishAttributes.I.Lexeme.Interjection)));
            Assert.IsTrue(morphemeRule.Evaluate(new Morpheme(attributesModel, "", EnglishAttributes.I.Lexeme.Verb | EnglishAttributes.I.Lexeme.Interjection)));
            Assert.IsFalse(morphemeRule.Evaluate(new Morpheme(attributesModel, "", EnglishAttributes.O)));
        }
Example #7
0
        private static bool CanAttachViaRule(this IAdTree adTree, IAdTree adTreeToAttach, AttachingPosition attachPosition, IAttributesModel attributesModel)
        {
            // Get rule to evalute.
            MorphemeRule rule = attachPosition == AttachingPosition.ChildOnLeft ? adTree.Pattern.LeftRule : adTree.Pattern.RightRule;

            // If the rule allows to attach and the order of attaching is correct.
            if (!rule.Equals(MorphemeRule.Nothing) && IsAttachingOrderCorrect(adTree, attachPosition))
            {
                // If the adtree where to attach is morphematic then it is not possible to attach the second child until the morpheme is set.
                if (adTree.Pattern.IsMorphematicAdPosition() && (adTree.Right != null || adTree.Left != null) && string.IsNullOrEmpty(adTree.Morpheme.Morph))
                {
                    return(false);
                }

                // Note: from the tree to attach we need to get the adtree representing the morpheme which shall be evaluated.
                IAdTree morphemeAdTree = null;

                // If the adTreeToAttach is a morpheme or a transference.
                if (adTreeToAttach.Pattern.IsLikeMorpheme)
                {
                    morphemeAdTree = adTreeToAttach;
                }
                // It is an adposition.
                else if (adTreeToAttach.Pattern.IsEpsilonAdPosition() ||
                         adTreeToAttach.Pattern.IsMorphematicAdPosition())
                {
                    // If a substitution (because adTreeToAttach is adposition) can be attached.
                    if (rule.SubstitutionRule.Evaluate(adTreeToAttach.Morpheme.GrammarCharacter))
                    {
                        // If it shall be attached to the right and
                        // the valency position is specified then check correctness with regard to presence of previously filled valencies.
                        if (attachPosition == AttachingPosition.ChildOnRight)
                        {
                            IAdTree valencyElement = adTree.GetSequenceToRoot()
                                                     .TakeUntil(x => x.IsOnRight)
                                                     .FirstOrDefault(x => x.Pattern.ValencyPosition > 0);

                            if (valencyElement != null)
                            {
                                IAdTree previousValencyElement = adTreeToAttach.GetRightSequence()
                                                                 .FirstOrDefault(x => x.Pattern.ValencyPosition > 0);

                                if (previousValencyElement == null && valencyElement.Pattern.ValencyPosition > 1 ||
                                    previousValencyElement != null && valencyElement.Pattern.ValencyPosition != previousValencyElement.Pattern.ValencyPosition + 1)
                                {
                                    return(false);
                                }
                            }
                        }

                        // Try to get the driving morpheme.
                        morphemeAdTree = adTreeToAttach.RightChildren.FirstOrDefault(x => x.Pattern.IsLikeMorpheme);

                        // If the governor is not attached yet then check only rules.
                        if (morphemeAdTree == null)
                        {
                            IEnumerable <IAdTree> rightSequence = new IAdTree[] { adTreeToAttach }.Concat(adTreeToAttach.RightChildren);
                            if (rightSequence.Any(x => !x.Pattern.RightRule.IsSubruleOf(rule) && !rule.IsSubruleOf(x.Pattern.RightRule)))
                            {
                                return(false);
                            }

                            return(true);
                        }
                    }
                }

                if (morphemeAdTree != null)
                {
                    // If it shall be attached to the right the morpheme is a verb then check the valency.
                    if (attachPosition == AttachingPosition.ChildOnRight && attributesModel.IsVerb(morphemeAdTree.Morpheme.Attributes))
                    {
                        int valency = attributesModel.GetNumberOfValencies(morphemeAdTree.Morpheme.Attributes);

                        if (valency > -1)
                        {
                            // Get already filled valency positions.
                            int[] valencyPositions = morphemeAdTree.GetSequenceToRoot()
                                                     .TakeWhile(x => x != adTree) // This is to not make an assuption if it is already attached or not.
                                                     .TakeUntil(x => x.IsOnRight)
                                                     .Concat(adTree.GetSequenceToRoot().TakeUntil(x => x.IsOnRight))
                                                     .Where(x => x.Pattern.ValencyPosition > 0)
                                                     .Select(x => x.Pattern.ValencyPosition)
                                                     .ToArray();

                            // If such valency is already filled.
                            if (valencyPositions.Length > valency)
                            {
                                return(false);
                            }
                            for (int i = 0; i < valencyPositions.Length; ++i)
                            {
                                if (valencyPositions[i] != i + 1)
                                {
                                    return(false);
                                }
                            }
                        }
                    }

                    Morpheme morphemeToEvaluate = morphemeAdTree.TryGetTransferenceMorpheme();

                    if (string.IsNullOrEmpty(morphemeToEvaluate?.Morph) && morphemeAdTree.Pattern.IsPairTransference)
                    {
                        // The morpheme with the morph is not attached yet so check only attributes.
                        if (rule.AttributesRule.Evaluate(morphemeAdTree.Morpheme.Attributes))
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }

                    // Check if the morpheme passes the rule.
                    bool result = rule.Evaluate(morphemeToEvaluate);
                    return(result);
                }
            }

            return(false);
        }
Example #8
0
 public static Pattern PairTransference(string patternName, string description, BigInteger morphemeAttributes, MorphemeRule leftRule, MorphemeRule rightRule)
 => new Pattern(patternName)
 {
     Description = description,
     UpRule      = EnglishMorphemeRule.Is("", morphemeAttributes),
     LeftRule    = leftRule,
     RightRule   = rightRule,
 };