Esempio n. 1
0
        public static bool IsClause(IAdTree adTree, IAttributesModel attributesModel)
        {
            var found  = adTree?.GetRightSequence().FirstOrDefault(x => attributesModel.IsVerb(x.Morpheme.Attributes) || attributesModel.IsU(x.Morpheme.Attributes));
            var result = found != null && attributesModel.IsVerb(found.Morpheme.Attributes);

            return(result);
        }
 public LinguisticStructureBase(IAdTree adTree, IAttributesModel attributesModel, ILinguisticStructureFactory factory, BigInteger attributes)
 {
     AdTree          = adTree;
     AttributesModel = attributesModel;
     Factory         = factory;
     Attributes      = attributes;
 }
Esempio n. 3
0
 /// <summary>
 /// Returns all adtrees which do not match the pattern.
 /// </summary>
 /// <param name="adTree"></param>
 /// <returns></returns>
 public static IEnumerable <IAdTree> GetNonconformities(this IAdTree adTree, IAttributesModel attributesModel)
 {
     foreach (IAdTree item in adTree)
     {
         if (!item.IsCorrect(attributesModel))
         {
             yield return(item);
         }
     }
 }
        public ConstructiveDictionary(IAttributesModel attributesModel, IEnumerable <Morpheme> morphemes, IEnumerable <Pattern> patterns)
        {
            using (Trace.Entering())
            {
                AttributesModel = attributesModel;
                morphemes       = morphemes ?? Enumerable.Empty <Morpheme>();
                Patterns        = patterns ?? Enumerable.Empty <Pattern>();

                InitializeMorphemes(morphemes);
                InitializePatternGraph();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Evaluates if the adtree matches its pattern.
        /// </summary>
        /// <param name="adTree"></param>
        /// <returns></returns>
        public static bool IsCorrect(this IAdTree adTree, IAttributesModel attributesModel)
        {
            // Check the morpheme belonging to the adtree.
            if (!adTree.Pattern.UpRule.Evaluate(adTree.Morpheme))
            {
                if (!adTree.Pattern.UpRule.Equals(MorphemeRule.Nothing) ||
                    (adTree.Pattern.UpRule.Equals(MorphemeRule.Nothing) &&
                     !string.IsNullOrEmpty(adTree.Morpheme.Morph)))
                {
                    return(false);
                }
            }

            // Left
            if (adTree.Left != null)
            {
                if (!adTree.CanAttachToLeft(adTree.Left, attributesModel))
                {
                    return(false);
                }
            }
            else if (!adTree.Pattern.LeftRule.Equals(MorphemeRule.Nothing) &&
                     !adTree.Pattern.LeftRule.Equals(MorphemeRule.Anything))
            {
                return(false);
            }


            // Right
            if (adTree.Right != null)
            {
                if (!adTree.CanAttachToRight(adTree.Right, attributesModel))
                {
                    return(false);
                }
            }
            else if (!adTree.Pattern.RightRule.Equals(MorphemeRule.Nothing) &&
                     !adTree.Pattern.RightRule.Equals(MorphemeRule.Anything))
            {
                return(false);
            }

            return(true);
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
 /// <summary>
 /// Derivation transference.
 /// </summary>
 /// <param name="morphTransformation"></param>
 /// <param name="attributesTransformation"></param>
 /// <returns></returns>
 public static DerivationMorphemeTransference Derivation(IAttributesModel attributesModel, ITransformation <string> morphTransformation, ITransformation <BigInteger> attributesTransformation)
 => new DerivationMorphemeTransference(attributesModel, morphTransformation, attributesTransformation);
Esempio n. 8
0
 /// <summary>
 /// Derivation transference of the morph. Attributes are not changed.
 /// </summary>
 /// <param name="morphTransformation"></param>
 /// <returns></returns>
 public static DerivationMorphemeTransference MorphDerivation(IAttributesModel attributesModel, ITransformation <string> morphTransformation)
 => Derivation(attributesModel, morphTransformation, Trans.NothingToDo <BigInteger>());
Esempio n. 9
0
 /// <summary>
 /// Derivation transference of attributes including grammar character. The morph is not changed.
 /// </summary>
 /// <param name="attributesTransformation"></param>
 /// <returns></returns>
 public static DerivationMorphemeTransference AttributesDerivation(IAttributesModel attributesModel, ITransformation <BigInteger> attributesTransformation)
 => Derivation(attributesModel, Trans.NothingToDo <string>(), attributesTransformation);
 public LinguisticStructureFactory(IAttributesModel attributesModel)
 {
     myAttributesModel = attributesModel;
 }
Esempio n. 11
0
 public Word(IAdTree wordAdTree, IAttributesModel attributesModel, ILinguisticStructureFactory factory, BigInteger attributes)
     : base(wordAdTree, attributesModel, factory, attributes)
 {
 }
 public DerivationMorphemeTransference(IAttributesModel attributesModel, ITransformation <string> morphTransformation, ITransformation <BigInteger> attributeTransformation)
 {
     myAttributesModel         = attributesModel;
     myMorphTransformation     = morphTransformation;
     myAttributeTransformation = attributeTransformation;
 }
Esempio n. 13
0
 public Clause(IAdTree clauseAdTree, IAttributesModel attributesModel, ILinguisticStructureFactory factory, BigInteger attributes)
     : base(clauseAdTree, attributesModel, factory, attributes)
 {
 }
Esempio n. 14
0
        /// <summary>
        /// Returns true if the adTreeElement can be attached to the left branch of the adTree.
        /// </summary>
        /// <param name="adTree"></param>
        /// <param name="adTreeToAttach"></param>
        /// <param name="attributesModel"></param>
        /// <returns></returns>
        public static bool CanAttachToLeft(this IAdTree adTree, IAdTree adTreeToAttach, IAttributesModel attributesModel)
        {
            bool result = adTree.CanAttachViaRule(adTreeToAttach, AttachingPosition.ChildOnLeft, attributesModel);

            return(result);
        }
Esempio n. 15
0
 public Morpheme(IAttributesModel attributesModel, string morph, BigInteger attributes)
 {
     Morph           = morph;
     Attributes      = attributes;
     AttributesModel = attributesModel;
 }
Esempio n. 16
0
 /// <summary>
 /// Indicates there is no morpheme.
 /// </summary>
 /// <remarks>
 /// This is used by zero-marked adpositions.
 /// </remarks>
 public static Morpheme Epsilon(IAttributesModel attributesModel) => new Morpheme(attributesModel, "", attributesModel.Epsilon);
Esempio n. 17
0
 public Term(IAdTree termAdTree, IAttributesModel attributesModel, ILinguisticStructureFactory factory, BigInteger attributes)
     : base(termAdTree, attributesModel, factory, attributes)
 {
 }
Esempio n. 18
0
 public Sentence(IAdTree SentenceAdTree, IAttributesModel attributesModel, ILinguisticStructureFactory factory, BigInteger attributes)
     : base(SentenceAdTree, attributesModel, factory, attributes)
 {
 }