public void CanAttachToRight_ValencyPosition() { // The phrase: I read AdTree adTree = new AdTree(new Morpheme(myAttributesModel, "", EnglishAttributes.Epsilon), EnglishPattern.O1_I) { Right = new AdTree(new Morpheme(myAttributesModel, "read", EnglishAttributes.I.Lexeme.Verb.Valency.Bivalent), EnglishPattern.Morpheme(EnglishAttributes.I.Lexeme.Verb.Valency.Bivalent)), Left = new AdTree(new Morpheme(myAttributesModel, "I", EnglishAttributes.O.Lexeme), EnglishPattern.Morpheme(EnglishAttributes.O.Lexeme)) }; // Try to connect the second valency position. AdTree valency2 = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.O2_I); Assert.IsTrue(valency2.CanAttachToRight(adTree, myAttributesModel)); // Try to connect the first valency position. AdTree valency1 = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.O1_I); Assert.IsFalse(valency1.CanAttachToRight(adTree, myAttributesModel)); // Try to connect the third valency position. AdTree valency3 = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.O3_I); Assert.IsFalse(valency3.CanAttachToRight(adTree, myAttributesModel)); // Try to connect the verb directly to the second valency. adTree = new AdTree(new Morpheme(myAttributesModel, "read", EnglishAttributes.I.Lexeme.Verb.Valency.Bivalent), EnglishPattern.Morpheme(EnglishAttributes.I.Lexeme.Verb.Valency.Bivalent)); Assert.IsFalse(valency2.CanAttachToRight(adTree, myAttributesModel)); }
public void CanAttachToRight() { IAdTree adTree = new AdTree(Morpheme.Epsilon(myAttributesModel), new Pattern() { RightRule = EnglishMorphemeRule.O_Lexeme, } ); IAdTree adTreeElement = new AdTree(new Morpheme(myAttributesModel, "car", EnglishAttributes.O.Lexeme), new Pattern() { UpRule = EnglishMorphemeRule.O_Lexeme }); Assert.IsTrue(adTree.CanAttachToRight(adTreeElement, myAttributesModel)); // Primitive transference. adTree = new AdTree(Morpheme.Epsilon(myAttributesModel), new Pattern() { RightRule = EnglishMorphemeRule.A_Lexeme, } ); adTreeElement = new AdTree(new Morpheme(myAttributesModel, "", EnglishAttributes.A.Lexeme), EnglishPattern.MonoTransference("O>A", EnglishAttributes.A.Lexeme, EnglishAttributes.O.Lexeme)) { Right = new AdTree(new Morpheme(myAttributesModel, "car", EnglishAttributes.O.Lexeme), new Pattern() { UpRule = EnglishMorphemeRule.O_Lexeme }) }; Assert.IsTrue(adTree.CanAttachToRight(adTreeElement, myAttributesModel)); adTree = new AdTree(Morpheme.Epsilon(myAttributesModel), new Pattern() { RightRule = EnglishMorphemeRule.O_Lexeme, } ); adTreeElement = new AdTree(new Morpheme(myAttributesModel, "green", EnglishAttributes.A.Lexeme), new Pattern() { UpRule = EnglishMorphemeRule.A_Lexeme }); Assert.IsFalse(adTree.CanAttachToRight(adTreeElement, myAttributesModel)); }
public void CanAttachToRight_Substitution() { IAdTree adTree = new AdTree(Morpheme.Epsilon(myAttributesModel), new Pattern() { RightRule = EnglishMorphemeRule.O_Lexeme, } ); IAdTree adTreeElement = new AdTree(Morpheme.Epsilon(myAttributesModel), new Pattern() { UpRule = MorphemeRule.Epsilon, LeftRule = EnglishMorphemeRule.A_Lexeme, RightRule = EnglishMorphemeRule.O_Lexeme }) { Right = new AdTree(new Morpheme(myAttributesModel, "car", EnglishAttributes.O.Lexeme), new Pattern() { UpRule = EnglishMorphemeRule.Is(MorphRules.Something, EnglishAttributes.O.Lexeme) }), }; Assert.IsTrue(adTree.CanAttachToRight(adTreeElement, myAttributesModel)); // Substitution is not allowed. adTree = new AdTree(Morpheme.Epsilon(myAttributesModel), new Pattern() { RightRule = EnglishMorphemeRule.O_Lexeme, }.SetSubstitutionForRight(SubstitutionRules.Nothing) ); adTreeElement = new AdTree(Morpheme.Epsilon(myAttributesModel), new Pattern() { UpRule = MorphemeRule.Epsilon, LeftRule = EnglishMorphemeRule.A_Lexeme, RightRule = EnglishMorphemeRule.O_Lexeme }) { Right = new AdTree(new Morpheme(myAttributesModel, "car", EnglishAttributes.O.Lexeme), new Pattern() { UpRule = EnglishMorphemeRule.Is(MorphRules.Something, EnglishAttributes.O.Lexeme) }), }; Assert.IsFalse(adTree.CanAttachToRight(adTreeElement, myAttributesModel)); }
public void CanAttachToRight_Substitution_Morphemic() { IAdTree adTree = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.EpsilonAdPosition("O-A", "", EnglishAttributes.O.Lexeme, EnglishAttributes.A.Lexeme)) { Left = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.EpsilonAdPosition("A-O", "", EnglishAttributes.A.Lexeme, EnglishAttributes.O.Lexeme)) }; IAdTree adTreeElement = new AdTree(new Morpheme(myAttributesModel, "car", EnglishAttributes.O.Lexeme), EnglishPattern.Morpheme(EnglishAttributes.O.Lexeme)); Assert.IsTrue(adTree.Left.CanAttachToRight(adTreeElement, myAttributesModel)); // The morpheme is not attached to the right yet - so only rules are evaluated. adTreeElement = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.EpsilonAdPosition("A-O", "", EnglishAttributes.A.Lexeme, EnglishAttributes.O.Lexeme)); Assert.IsTrue(adTree.Left.CanAttachToRight(adTreeElement, myAttributesModel)); // The morpheme is not attached to the right yet - so only rules are evaluated => incorrect rules. adTreeElement = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.EpsilonAdPosition("O-A", "", EnglishAttributes.O.Lexeme, EnglishAttributes.A.Lexeme)); Assert.IsFalse(adTree.Left.CanAttachToRight(adTreeElement, myAttributesModel)); adTree = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.EpsilonAdPosition("O-A", "", EnglishAttributes.O.Lexeme, EnglishAttributes.A.Lexeme)) { Left = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.EpsilonAdPosition("A-O", "", EnglishAttributes.A.Lexeme, EnglishAttributes.O.Lexeme)) { Right = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.EpsilonAdPosition("A-O", "", EnglishAttributes.A.Lexeme, EnglishAttributes.O.Lexeme)) } }; // Now try to attach the morpheme. adTreeElement = new AdTree(new Morpheme(myAttributesModel, "car", EnglishAttributes.O.Lexeme), EnglishPattern.Morpheme(EnglishAttributes.O.Lexeme)); Assert.IsTrue(adTree.Left.Right.CanAttachToRight(adTreeElement, myAttributesModel)); // Attach to the right on the root. adTree = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.EpsilonAdPosition("A-O", "", EnglishAttributes.A.Lexeme, EnglishAttributes.O.Lexeme)); adTreeElement = new AdTree(new Morpheme(myAttributesModel, "car", EnglishAttributes.O.Lexeme), EnglishPattern.Morpheme(EnglishAttributes.O.Lexeme)); Assert.IsTrue(adTree.CanAttachToRight(adTreeElement, myAttributesModel)); // Attach to the right on the root - incorrect morpheme. adTree = new AdTree(Morpheme.Epsilon(myAttributesModel), EnglishPattern.EpsilonAdPosition("A-O", "", EnglishAttributes.A.Lexeme, EnglishAttributes.O.Lexeme)); adTreeElement = new AdTree(new Morpheme(myAttributesModel, "green", EnglishAttributes.A.Lexeme), EnglishPattern.Morpheme(EnglishAttributes.A.Lexeme)); Assert.IsFalse(adTree.CanAttachToRight(adTreeElement, myAttributesModel)); }
// Note: indirect append means that adtress (with unfilled values) will be inserted inbetween start and end adtree. // Unfilled values are expected to be filled later otherwise the adtree will be not valid. private void TryToAppendIndirectly(AttachingPosition appendingPositionOfStartElement, IAdTree start, IAdTree end, string expectedSignature, List <IAdTree> results) { using (Trace.Entering()) { GrammarCharacter startGrammarCharacter = GrammarCharacter.e; GrammarCharacter endGrammarCharacter = GrammarCharacter.e; // If start adTree connects the bridge on its left. if (appendingPositionOfStartElement == AttachingPosition.ParrentForLeft) { startGrammarCharacter = start.Pattern.LeftRule.GrammarCharacter; endGrammarCharacter = end.RulingGrammarCharacter; } // If start adTree connects the bridge on its right. else if (appendingPositionOfStartElement == AttachingPosition.ParrentForRight) { startGrammarCharacter = start.Pattern.RightRule.GrammarCharacter; endGrammarCharacter = end.RulingGrammarCharacter; } // If the bridge connects the start adtree on its left. else if (appendingPositionOfStartElement == AttachingPosition.ChildOnLeft) { startGrammarCharacter = start.RulingGrammarCharacter; endGrammarCharacter = end.RulingGrammarCharacter; } // If the bridge connects the start adtree on its right. else if (appendingPositionOfStartElement == AttachingPosition.ChildOnRight) { startGrammarCharacter = start.RulingGrammarCharacter; endGrammarCharacter = end.RulingGrammarCharacter; } // Get all possibile ways how to get from start to end grammar character and path is not greater than 4. IEnumerable <IReadOnlyList <DirectedEdge <GrammarCharacter, Pattern> > > connectionPaths = myConstructiveDictionary.PatternGraph .FindAllEdges(startGrammarCharacter, endGrammarCharacter).ToList(); // Go via all possible ways. foreach (IReadOnlyList <DirectedEdge <GrammarCharacter, Pattern> > path in connectionPaths) { IAdTree startCopy = start.MakeShallowCopy(); IAdTree endCopy = end.MakeShallowCopy(); IAdTree previousBridge = null; // Go via the path. for (int i = 0; i < path.Count; ++i) { DirectedEdge <GrammarCharacter, Pattern> edge = path[i]; BigInteger attributes = edge.Value.UpRule.AttributesRule is IReferenceValueRule <BigInteger> referenceAttributes ? referenceAttributes.ReferenceValue : myConstructiveDictionary.AttributesModel.GetAttributes(edge.Value.UpRule.GrammarCharacter); IAdTree bridge = new AdTree(new Morpheme(myConstructiveDictionary.AttributesModel, "", attributes), edge.Value); // If it is the first item on the path. if (i == 0) { if (appendingPositionOfStartElement == AttachingPosition.ParrentForLeft) { if (startCopy.Left == null && startCopy.CanAttachToLeft(bridge, myConstructiveDictionary.AttributesModel)) { startCopy.Left = bridge; } else { break; } } else if (appendingPositionOfStartElement == AttachingPosition.ParrentForRight) { if (startCopy.Right == null && startCopy.CanAttachToRight(bridge, myConstructiveDictionary.AttributesModel)) { startCopy.Right = bridge; } else { break; } } else if (appendingPositionOfStartElement == AttachingPosition.ChildOnLeft) { if (bridge.Left == null && bridge.CanAttachToLeft(startCopy, myConstructiveDictionary.AttributesModel)) { bridge.Left = startCopy; } else { break; } } else if (appendingPositionOfStartElement == AttachingPosition.ChildOnRight) { if (bridge.Right == null && bridge.CanAttachToRight(startCopy, myConstructiveDictionary.AttributesModel)) { bridge.Right = startCopy; } else { break; } } } else { if (previousBridge.Left == null && previousBridge.CanAttachToLeft(bridge, myConstructiveDictionary.AttributesModel)) { previousBridge.Left = bridge; } else if (previousBridge.Right == null && previousBridge.CanAttachToRight(bridge, myConstructiveDictionary.AttributesModel)) { previousBridge.Right = bridge; } else { break; } } // If the signature does not match then break it. var currentSignature = bridge.Root.GetSignature(); if (!expectedSignature.StartsWith(currentSignature)) { break; } // If it is the last item in the path. if (i == path.Count - 1) { if (bridge.Left == null && bridge.CanAttachToLeft(endCopy, myConstructiveDictionary.AttributesModel)) { bridge.Left = endCopy; currentSignature = endCopy.Root.GetSignature(); if (expectedSignature.StartsWith(currentSignature)) { results.Add(endCopy); } } else if (bridge.Right == null && bridge.CanAttachToRight(endCopy, myConstructiveDictionary.AttributesModel)) { bridge.Right = endCopy; currentSignature = endCopy.Root.GetSignature(); if (expectedSignature.StartsWith(currentSignature)) { results.Add(endCopy); } } else { break; } } previousBridge = bridge; } } } }