예제 #1
0
        /// <summary>
        /// Tries to get the adtree element which is on the specified path.
        /// </summary>
        /// <param name="rootAdTree"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool TryGetAdTree(this IAdTree rootAdTree, AttachingPosition[] path, out IAdTree result)
        {
            result = rootAdTree;
            foreach (AttachingPosition value in path)
            {
                if (value == AttachingPosition.ChildOnLeft)
                {
                    if (result.Left == null)
                    {
                        return(false);
                    }

                    result = result.Left;
                }
                else if (value == AttachingPosition.ChildOnRight)
                {
                    if (result.Right == null)
                    {
                        return(false);
                    }

                    result = result.Right;
                }
                else
                {
                    throw new InvalidOperationException($"Failed to get the adTree element because the path ontained the incorrect value {value}. Expected is 1 or 2.");
                }
            }

            return(true);
        }
예제 #2
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);
        }
예제 #3
0
        private static bool IsAttachingOrderCorrect(IAdTree adTree, AttachingPosition position)
        {
            bool result = false;

            // If the attaching position is on the left.
            if (position == AttachingPosition.ChildOnLeft)
            {
                // If left is before the right
                if (adTree.Pattern.IsLeftFirst ||
                    // or right is already set
                    adTree.Right != null ||
                    // or right is not supposed to be set.
                    adTree.Pattern.RightRule.Equals(MorphemeRule.Nothing))
                {
                    result = true;
                }
            }
            else
            {
                if (!adTree.Pattern.IsLeftFirst ||
                    adTree.Left != null ||
                    adTree.Pattern.LeftRule.Equals(MorphemeRule.Nothing))
                {
                    result = true;
                }
            }

            return(result);
        }
예제 #4
0
        public void GetFirstAdPositionOnLeft()
        {
            AdTree adTree = new AdTree(new Morpheme(myAttributesModel, "", 0), new Pattern())
            {
                Right = new AdTree(new Morpheme(myAttributesModel, "", 0), new Pattern())
                {
                    Right = new AdTree(new Morpheme(myAttributesModel, "read", 0), new Pattern()),
                    Left  = new AdTree(new Morpheme(myAttributesModel, "I", 0), new Pattern())
                },
                Left = new AdTree(new Morpheme(myAttributesModel, "", 0), new Pattern())
                {
                    Right = new AdTree(new Morpheme(myAttributesModel, "book", 0), new Pattern()),
                    Left  = new AdTree(new Morpheme(myAttributesModel, "the", 0), new Pattern())
                }
            };

            IAdTree result = adTree.Left.Right.GetFirstAdPositionOnLeft();

            Assert.IsTrue(adTree.Left == result);

            // It is already on left.
            result = adTree.Right.Left.GetFirstAdPositionOnLeft();
            Assert.IsTrue(adTree.Right.Left == result);

            // Root.
            result = adTree.GetFirstAdPositionOnLeft();
            Assert.IsNull(result);
        }
예제 #5
0
        /// <summary>
        /// Returns the path to the adTree element. Empty array if it is the root.
        /// </summary>
        /// <param name="adTree"></param>
        /// <returns></returns>
        public static AttachingPosition[] GetPath(this IAdTree adTree)
        {
            IEnumerable <IAdTree> adTreesOnPath = adTree.GetSequenceToRoot().Where(x => x.AdPosition != null);

            AttachingPosition[] result = adTreesOnPath.Select(x => x.IsOnLeft ? AttachingPosition.ChildOnLeft : AttachingPosition.ChildOnRight).Reverse().ToArray();
            return(result);
        }
예제 #6
0
 public LinguisticStructureBase(IAdTree adTree, IAttributesModel attributesModel, ILinguisticStructureFactory factory, BigInteger attributes)
 {
     AdTree          = adTree;
     AttributesModel = attributesModel;
     Factory         = factory;
     Attributes      = attributes;
 }
예제 #7
0
 private bool CanAppendIndirectly(IAdTree current)
 {
     using (Trace.Entering())
     {
         IEnumerable <IAdTree> incompleteAdTrees = current.GetSequenceToRoot().Where(x => !x.IsComplete()).Take(5);
         int count = incompleteAdTrees.Count();
         return(count < 5);
     }
 }
예제 #8
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);
         }
     }
 }
예제 #9
0
 /// <summary>
 /// Appends the adtree to the adtree.
 /// </summary>
 /// <param name="adTree">The adtree into which it shall be attached.</param>
 /// <param name="toAttach">The adtree which shall be appended.</param>
 /// <param name="appendPosition">The position how it shall be appended.</param>
 public static void Attach(this IAdTree adTree, IAdTree toAttach, AttachingPosition appendPosition)
 {
     if (appendPosition == AttachingPosition.ChildOnLeft)
     {
         adTree.Left = toAttach;
     }
     else if (appendPosition == AttachingPosition.ChildOnRight)
     {
         adTree.Right = toAttach;
     }
 }
예제 #10
0
 /// <summary>
 /// Removes the adtree from the adtree structure.
 /// </summary>
 /// <param name="adTree">The adtree which shall be removed.</param>
 public static void Detach(this IAdTree adTree)
 {
     if (adTree.IsOnLeft)
     {
         adTree.AdPosition.Left = null;
     }
     else if (adTree.IsOnRight)
     {
         adTree.AdPosition.Right = null;
     }
 }
        public string GetAdTreeVisualization(IAdTree adTree)
        {
            StringBuilder result = new StringBuilder();

            var rows = GetRows(adTree).ToList();

            foreach (var row in rows)
            {
                result.AppendLine(row);
            }

            return(result.ToString());
        }
예제 #12
0
        /// <summary>
        /// Replaces the adtree with the 'replace' adtree.
        /// </summary>
        /// <param name="adTree">AdTree which shall be replaced.</param>
        /// <param name="replace">AdTree which shall be used insted of the original one.</param>
        public static void Replace(this IAdTree adTree, IAdTree replace)
        {
            if (adTree.IsOnLeft)
            {
                adTree.AdPosition.Left = replace;
            }
            else if (adTree.IsOnRight)
            {
                adTree.AdPosition.Right = replace;
            }

            replace.Left  = adTree.Left;
            replace.Right = adTree.Right;
        }
예제 #13
0
        /// <summary>
        /// Returns the signature (sequence of grammar characters) for the adtree.
        /// </summary>
        /// <param name="adTreeSequence"></param>
        /// <returns></returns>
        public static string GetSignature(this IAdTree adTree)
        {
            var result = new StringBuilder();

            foreach (var item in adTree)
            {
                if (item.Morpheme.GrammarCharacter != GrammarCharacter.e && !string.IsNullOrEmpty(item.Morpheme.Morph))
                {
                    result.Append(item.Morpheme.GrammarCharacter);
                }
            }

            return(result.ToString());
        }
예제 #14
0
        public void MakeShallowCopy()
        {
            // The phrase: I read the book.
            IAdTree adTree = new AdTree(new Morpheme(myAttributesModel, "", 0), new Pattern())
            {
                Right = new AdTree(new Morpheme(myAttributesModel, "", 0), new Pattern())
                {
                    Right = new AdTree(new Morpheme(myAttributesModel, "read", EnglishAttributes.I.Lexeme.Verb), new Pattern()),
                    Left  = new AdTree(new Morpheme(myAttributesModel, "I", EnglishAttributes.O.Lexeme), new Pattern())
                },
                Left = new AdTree(new Morpheme(myAttributesModel, "", EnglishAttributes.U), new Pattern())
                {
                    Right = new AdTree(new Morpheme(myAttributesModel, "book", EnglishAttributes.O.Lexeme), new Pattern()),
                    Left  = new AdTree(new Morpheme(myAttributesModel, "the", EnglishAttributes.A.Lexeme), new Pattern())
                }
            };

            // Note: the copy does not start from the root but the whole tree will be copied.
            IAdTree copy = adTree.Right.MakeShallowCopy();

            // The copy must be set to the same position as the original.
            Assert.IsTrue(adTree.Right.Morpheme == copy.Morpheme);
            Assert.IsTrue(adTree.Right.Pattern == copy.Pattern);

            // Get the root - for easier testing.
            adTree = adTree.Root;
            copy   = copy.Root;

            Assert.IsTrue(adTree.Morpheme == copy.Morpheme);
            Assert.IsTrue(adTree.Pattern == copy.Pattern);

            Assert.IsTrue(adTree.Right.Morpheme == copy.Right.Morpheme);
            Assert.IsTrue(adTree.Right.Pattern == copy.Right.Pattern);

            Assert.IsTrue(adTree.Right.Right.Morpheme == copy.Right.Right.Morpheme);
            Assert.IsTrue(adTree.Right.Right.Pattern == copy.Right.Right.Pattern);

            Assert.IsTrue(adTree.Right.Left.Morpheme == copy.Right.Left.Morpheme);
            Assert.IsTrue(adTree.Right.Left.Pattern == copy.Right.Left.Pattern);

            Assert.IsTrue(adTree.Left.Morpheme == copy.Left.Morpheme);
            Assert.IsTrue(adTree.Left.Pattern == copy.Left.Pattern);

            Assert.IsTrue(adTree.Left.Right.Morpheme == copy.Left.Right.Morpheme);
            Assert.IsTrue(adTree.Left.Right.Pattern == copy.Left.Right.Pattern);

            Assert.IsTrue(adTree.Left.Left.Morpheme == copy.Left.Left.Morpheme);
            Assert.IsTrue(adTree.Left.Left.Pattern == copy.Left.Left.Pattern);
        }
예제 #15
0
        /// <summary>
        /// Inserts the adtree into the adtree.
        /// </summary>
        /// <param name="adTree">The adtree into which it shall be inserted.</param>
        /// <param name="toInsert">The adtree which shall be inserted.</param>
        /// <param name="whereReattach">The adtree which shall reatach the adtree disconnected by the insertion.</param>
        /// <param name="reattachPosition">The position where the adtree shall be reattached after the insertion.</param>
        public static void Insert(this IAdTree adTree, IAdTree toInsert, IAdTree whereReattach, AttachingPosition reattachPosition)
        {
            // Insert the toInsert instead of the adTree.
            if (adTree.IsOnLeft)
            {
                adTree.AdPosition.Left = toInsert;
            }
            else if (adTree.IsOnRight)
            {
                adTree.AdPosition.Right = toInsert;
            }

            // Attach the adtree to the inserted adtree.
            whereReattach.Attach(adTree, reattachPosition);
        }
예제 #16
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);
        }
예제 #17
0
 private IEnumerable <IAdTree> GetGrammarCharacterTransferences(IAdTree adTree)
 {
     using (Trace.Entering())
     {
         IEnumerable <Pattern> patterns = myConstructiveDictionary.FindMonoTransferencePatterns(adTree.Morpheme);
         foreach (Pattern pattern in patterns)
         {
             // Note: in case of a grammar character transference it is expected the morpheme rule always represents
             //       a concrete value.
             if (pattern.UpRule.AttributesRule is IReferenceValueRule <BigInteger> isRule)
             {
                 IAdTree adTreeCopy         = adTree.MakeShallowCopy();
                 IAdTree transferenceAdTree = new AdTree(new Morpheme(myConstructiveDictionary.AttributesModel, "", isRule.ReferenceValue), pattern);
                 transferenceAdTree.Right = adTreeCopy;
                 yield return(transferenceAdTree);
             }
         }
     }
 }
예제 #18
0
        /// <summary>
        /// Returns the morpheme for Morpheme, MonoTransference or PairTransference adtree.
        /// </summary>
        /// <remarks>
        /// Returns null if the
        /// </remarks>
        /// <param name="adTree"></param>
        /// <returns></returns>
        public static Morpheme TryGetTransferenceMorpheme(this IAdTree adTree)
        {
            Morpheme result = null;

            if (adTree.Pattern.IsMorpheme)
            {
                result = adTree.Morpheme;
            }
            else if (adTree.Pattern.IsMonoTransference)
            {
                result = new Morpheme(adTree.Morpheme.AttributesModel, adTree.Right?.Morpheme.Morph, adTree.Morpheme.Attributes);
            }
            else if (adTree.Pattern.IsPairTransference)
            {
                result = new Morpheme(adTree.Morpheme.AttributesModel, adTree.Phrase, adTree.Morpheme.Attributes);
            }

            return(result);
        }
예제 #19
0
        private IAdTree GetPlaceToAppend(IAdTree current)
        {
            using (Trace.Entering())
            {
                IEnumerable <IAdTree> adTrees = current.GetSequenceToRoot();
                foreach (IAdTree adTree in adTrees)
                {
                    // If left or right position can be filled.
                    if (adTree.Left == null && !adTree.Pattern.LeftRule.Equals(MorphemeRule.Nothing) ||
                        adTree.Right == null && !adTree.Pattern.RightRule.Equals(MorphemeRule.Nothing) ||
                        // or an adposition morpheme is still expected.
                        !adTree.Pattern.UpRule.Equals(MorphemeRule.Nothing) &&
                        !adTree.Pattern.UpRule.Evaluate(adTree.Morpheme))
                    {
                        return(adTree);
                    }
                }

                return(current.Root);
            }
        }
예제 #20
0
        /// <summary>
        /// Returns the shallow copy (Morphemes and Patterns are not duplicated) of the adtree. The returned copy is on the same path.
        /// </summary>
        /// <param name="adTree"></param>
        /// <returns></returns>
        public static IAdTree MakeShallowCopy(this IAdTree adTree)
        {
            // Store the current position in the tree.
            AttachingPosition[] path = adTree.GetPath();

            IAdTree root = adTree.Root;

            // <original, copy>
            Stack <Tuple <IAdTree, IAdTree> > stack = new Stack <Tuple <IAdTree, IAdTree> >();
            IAdTree rootCopy = new AdTree(root.Morpheme, root.Pattern);

            stack.Push(Tuple.Create(root, rootCopy));

            while (stack.Count > 0)
            {
                Tuple <IAdTree, IAdTree> aThis = stack.Pop();

                if (aThis.Item1.Left != null)
                {
                    IAdTree leftCopy = new AdTree(aThis.Item1.Left.Morpheme, aThis.Item1.Left.Pattern);
                    aThis.Item2.Left = leftCopy;
                    stack.Push(Tuple.Create(aThis.Item1.Left, leftCopy));
                }

                if (aThis.Item1.Right != null)
                {
                    IAdTree rightCopy = new AdTree(aThis.Item1.Right.Morpheme, aThis.Item1.Right.Pattern);
                    aThis.Item2.Right = rightCopy;
                    stack.Push(Tuple.Create(aThis.Item1.Right, rightCopy));
                }
            }

            // Return the tree element in the copy which is on the same path as the input parameter.
            if (rootCopy.TryGetAdTree(path, out IAdTree result))
            {
                return(result);
            }

            throw new InvalidOperationException("Failed to properly copy the adtree.");
        }
예제 #21
0
        /// <summary>
        /// Returns true if the adtree has filled all required data.
        /// </summary>
        /// <remarks>
        /// It does not evaluate rules. It just checks if required data are present.
        /// </remarks>
        /// <param name="adtree"></param>
        /// <returns></returns>
        public static bool IsComplete(this IAdTree adTree)
        {
            // Check if the morph is set.
            if (string.IsNullOrEmpty(adTree.Morpheme.Morph) &&
                !adTree.Pattern.UpRule.MorphRule.Equals(MorphRules.Nothing) &&
                !adTree.Pattern.UpRule.MorphRule.Evaluate(adTree.Morpheme.Morph))
            {
                return(false);
            }

            // Check if the attribute is set.
            if (adTree.Morpheme.Attributes == 0 &&
                !adTree.Pattern.UpRule.AttributesRule.Equals(MaskRule.Nothing) &&
                !adTree.Pattern.UpRule.AttributesRule.Equals(MaskRule.Anything))
            {
                return(false);
            }


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


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

            return(true);
        }
예제 #22
0
        /// <summary>
        /// Returns the deep copy of the sub-adTree. (adTree will be the root of the returned adTree)
        /// </summary>
        /// <param name="adTree"></param>
        /// <returns></returns>
        public static IAdTree MakeDeepCopy(this IAdTree adTree)
        {
            var root = adTree;

            // <original, copy>
            Stack <Tuple <IAdTree, IAdTree> > stack = new Stack <Tuple <IAdTree, IAdTree> >();

            var     rootMorpheme = new Morpheme(root.Morpheme);
            var     rootPattern  = new Pattern(root.Pattern);
            IAdTree rootCopy     = new AdTree(rootMorpheme, rootPattern);

            stack.Push(Tuple.Create(root, rootCopy));

            while (stack.Count > 0)
            {
                Tuple <IAdTree, IAdTree> aThis = stack.Pop();

                if (aThis.Item1.Left != null)
                {
                    var     morpheme = new Morpheme(aThis.Item1.Left.Morpheme);
                    var     pattern  = new Pattern(aThis.Item1.Left.Pattern);
                    IAdTree leftCopy = new AdTree(morpheme, pattern);
                    aThis.Item2.Left = leftCopy;
                    stack.Push(Tuple.Create(aThis.Item1.Left, leftCopy));
                }

                if (aThis.Item1.Right != null)
                {
                    var     morpheme  = new Morpheme(aThis.Item1.Right.Morpheme);
                    var     pattern   = new Pattern(aThis.Item1.Right.Pattern);
                    IAdTree rightCopy = new AdTree(morpheme, pattern);
                    aThis.Item2.Right = rightCopy;
                    stack.Push(Tuple.Create(aThis.Item1.Right, rightCopy));
                }
            }

            return(rootCopy);
        }
예제 #23
0
        public void Insert()
        {
            AdTree adTree = new AdTree(new Morpheme(myAttributesModel, "A1", 0), new Pattern())
            {
                Right = new AdTree(new Morpheme(myAttributesModel, "A11", 0), new Pattern()),
            };
            AdTree toInsert = new AdTree(new Morpheme(myAttributesModel, "hello", 0), new Pattern());

            adTree.Right.Insert(toInsert, toInsert, AttachingPosition.ChildOnLeft);

            Assert.AreEqual("hello", adTree.Right.Morpheme.Morph);
            Assert.AreEqual("A11", adTree.Right.Left.Morpheme.Morph);


            adTree = new AdTree(new Morpheme(myAttributesModel, "A1", 0), new Pattern())
            {
                Right = new AdTree(new Morpheme(myAttributesModel, "A11", 0), new Pattern()),
            };
            toInsert = new AdTree(new Morpheme(myAttributesModel, "hello", 0), new Pattern());
            adTree.Right.Insert(toInsert, toInsert, AttachingPosition.ChildOnRight);

            Assert.AreEqual("hello", adTree.Right.Morpheme.Morph);
            Assert.AreEqual("A11", adTree.Right.Right.Morpheme.Morph);


            // Inserting to the root.
            adTree = new AdTree(new Morpheme(myAttributesModel, "A1", 0), new Pattern())
            {
                Right = new AdTree(new Morpheme(myAttributesModel, "A11", 0), new Pattern()),
            };
            toInsert = new AdTree(new Morpheme(myAttributesModel, "hello", 0), new Pattern());
            adTree.Insert(toInsert, toInsert, AttachingPosition.ChildOnRight);

            IAdTree root = adTree.Root;

            Assert.AreEqual("hello", root.Morpheme.Morph);
            Assert.AreEqual("A1", root.Right.Morpheme.Morph);
        }
        private string GetAdTreeItemVisualization(IAdTree adTree)
        {
            var grammarCharacter = adTree.Morpheme != null ? $"[{adTree.Morpheme.GrammarCharacter}]" : "";
            var pattern          = !adTree.Pattern.IsMorpheme ? $"{{{adTree.Pattern.Name}}}" : "";
            var morpheme         = !string.IsNullOrEmpty(adTree.Morpheme?.Morph) ? $"{adTree.Morpheme?.Morph}" : "";

            var result = new StringBuilder();

            if (pattern != "")
            {
                result.Append(pattern);
            }
            if (grammarCharacter != "")
            {
                result.Append(grammarCharacter);
            }
            if (morpheme != "")
            {
                result.Append(morpheme);
            }

            return(result.ToString());
        }
 public ISentence CreateSentence(IAdTree sentenceAdTree, BigInteger attributes) => new Sentence(sentenceAdTree, myAttributesModel, this, attributes);
 public IClause CreateClause(IAdTree clauseAdTree, BigInteger attributes) => new Clause(clauseAdTree, myAttributesModel, this, attributes);
 public ITerm CreateTerm(IAdTree termAdTree, BigInteger attributes) => new Term(termAdTree, myAttributesModel, this, attributes);
 public IWord CreateWord(IAdTree wordAdTree, BigInteger attributes) => new Word(wordAdTree, myAttributesModel, this, attributes);
예제 #29
0
 public Word(IAdTree wordAdTree, IAttributesModel attributesModel, ILinguisticStructureFactory factory, BigInteger attributes)
     : base(wordAdTree, attributesModel, factory, attributes)
 {
 }
예제 #30
0
        /// <summary>
        /// Returns the sequence from the specified adtree through right children.
        /// </summary>
        /// <param name="adTree"></param>
        /// <returns></returns>
        public static IEnumerable <IAdTree> GetRightSequence(this IAdTree adTree) => new IAdTree[]
        {
            adTree
        }

        .Concat(adTree.RightChildren);