public virtual void TestBasicOperations()
        {
            TreeShapedStack <string> tss = new TreeShapedStack <string>();

            NUnit.Framework.Assert.AreEqual(tss.size, 0);
            TreeShapedStack <string> tss1 = tss.Push("1");

            NUnit.Framework.Assert.AreEqual(tss1.size, 1);
            NUnit.Framework.Assert.AreEqual(tss1.Peek(), "1");
            TreeShapedStack <string> tss2 = tss1.Push("2");

            NUnit.Framework.Assert.AreEqual(tss2.size, 2);
            NUnit.Framework.Assert.AreEqual(tss2.Peek(), "2");
            NUnit.Framework.Assert.AreEqual(tss2.previous.Peek(), "1");
            TreeShapedStack <string> tss3 = tss2.Push("3");

            NUnit.Framework.Assert.AreEqual(tss3.size, 3);
            NUnit.Framework.Assert.AreEqual(tss3.Peek(), "3");
            NUnit.Framework.Assert.AreEqual(tss3.previous.Peek(), "2");
            tss3 = tss3.Pop();
            NUnit.Framework.Assert.AreEqual(tss3.Peek(), "2");
            NUnit.Framework.Assert.AreEqual(tss3.previous.Peek(), "1");
            NUnit.Framework.Assert.AreEqual(tss3.Peek(), "2");
            TreeShapedStack <string> tss4 = tss3.Push("4");

            NUnit.Framework.Assert.AreEqual(tss4.Peek(), "4");
            NUnit.Framework.Assert.AreEqual(tss4.Peek(), "4");
            NUnit.Framework.Assert.AreEqual(tss4.previous.Peek(), "2");
            tss4 = tss4.Pop();
            NUnit.Framework.Assert.AreEqual(tss4.Peek(), "2");
            tss4 = tss4.Pop();
            NUnit.Framework.Assert.AreEqual(tss4.Peek(), "1");
            tss4 = tss4.Pop();
            NUnit.Framework.Assert.AreEqual(tss4.size, 0);
        }
        /// <summary>Add a binary node to the existing node on top of the stack</summary>
        public virtual State Apply(State state, double scoreDelta)
        {
            TreeShapedStack <Tree> stack = state.stack;
            Tree right = stack.Peek();

            stack = stack.Pop();
            Tree left = stack.Peek();

            stack = stack.Pop();
            Tree head;

            switch (side)
            {
            case BinaryTransition.Side.Left:
            {
                head = left;
                break;
            }

            case BinaryTransition.Side.Right:
            {
                head = right;
                break;
            }

            default:
            {
                throw new ArgumentException("Unknown side " + side);
            }
            }
            if (!(head.Label() is CoreLabel))
            {
                throw new ArgumentException("Stack should have CoreLabel nodes");
            }
            CoreLabel headLabel  = (CoreLabel)head.Label();
            CoreLabel production = new CoreLabel();

            production.SetValue(label);
            production.Set(typeof(TreeCoreAnnotations.HeadWordLabelAnnotation), headLabel.Get(typeof(TreeCoreAnnotations.HeadWordLabelAnnotation)));
            production.Set(typeof(TreeCoreAnnotations.HeadTagLabelAnnotation), headLabel.Get(typeof(TreeCoreAnnotations.HeadTagLabelAnnotation)));
            Tree newTop = new LabeledScoredTreeNode(production);

            newTop.AddChild(left);
            newTop.AddChild(right);
            stack = stack.Push(newTop);
            return(new State(stack, state.transitions.Push(this), state.separators, state.sentence, state.tokenPosition, state.score + scoreDelta, false));
        }
Example #3
0
        public static CoreLabel GetStackLabel(TreeShapedStack <Tree> stack, int nodeNum, params FeatureFactory.Transition[] transitions)
        {
            if (stack.Size() <= nodeNum)
            {
                return(null);
            }
            for (int i = 0; i < nodeNum; ++i)
            {
                stack = stack.Pop();
            }
            Tree node = stack.Peek();

            // TODO: this is nice for code readability, but might be expensive
            foreach (FeatureFactory.Transition t in transitions)
            {
                switch (t)
                {
                case FeatureFactory.Transition.Left:
                {
                    if (node.Children().Length != 2)
                    {
                        return(null);
                    }
                    node = node.Children()[0];
                    break;
                }

                case FeatureFactory.Transition.Right:
                {
                    if (node.Children().Length != 2)
                    {
                        return(null);
                    }
                    node = node.Children()[1];
                    break;
                }

                case FeatureFactory.Transition.Unary:
                {
                    if (node.Children().Length != 1)
                    {
                        return(null);
                    }
                    node = node.Children()[0];
                    break;
                }

                default:
                {
                    throw new ArgumentException("Unknown transition type " + t);
                }
                }
            }
            if (!(node.Label() is CoreLabel))
            {
                throw new ArgumentException("Can only featurize CoreLabel trees");
            }
            return((CoreLabel)node.Label());
        }
Example #4
0
        internal virtual Tree GetStackNode(int depth)
        {
            if (depth >= stack.Size())
            {
                return(null);
            }
            TreeShapedStack <Tree> node = stack;

            for (int i = 0; i < depth; ++i)
            {
                node = node.Pop();
            }
            return(node.Peek());
        }
Example #5
0
        internal virtual State.HeadPosition GetSeparator(int nodeNum)
        {
            if (nodeNum >= stack.Size())
            {
                return(null);
            }
            TreeShapedStack <Tree> stack = this.stack;

            for (int i = 0; i < nodeNum; ++i)
            {
                stack = stack.Pop();
            }
            Tree node = stack.Peek();
            int  head = ShiftReduceUtils.HeadIndex(node);

            if (separators[head] != null)
            {
                return(State.HeadPosition.Head);
            }
            int  left      = ShiftReduceUtils.LeftIndex(node);
            int  nextLeft  = separators.FloorKey(head);
            bool hasLeft   = (nextLeft != null && nextLeft >= left);
            int  right     = ShiftReduceUtils.RightIndex(node);
            int  nextRight = separators.CeilingKey(head);
            bool hasRight  = (nextRight != null && nextRight <= right);

            if (hasLeft && hasRight)
            {
                return(State.HeadPosition.Both);
            }
            else
            {
                if (hasLeft)
                {
                    return(State.HeadPosition.Left);
                }
                else
                {
                    if (hasRight)
                    {
                        return(State.HeadPosition.Right);
                    }
                    else
                    {
                        return(State.HeadPosition.None);
                    }
                }
            }
        }
Example #6
0
        public static CoreLabel GetRecentDependent(TreeShapedStack <Tree> stack, FeatureFactory.Transition transition, int nodeNum)
        {
            if (stack.Size() <= nodeNum)
            {
                return(null);
            }
            for (int i = 0; i < nodeNum; ++i)
            {
                stack = stack.Pop();
            }
            Tree node = stack.Peek();

            if (node == null)
            {
                return(null);
            }
            if (!(node.Label() is CoreLabel))
            {
                throw new ArgumentException("Can only featurize CoreLabel trees");
            }
            CoreLabel head = ((CoreLabel)node.Label()).Get(typeof(TreeCoreAnnotations.HeadWordLabelAnnotation));

            switch (transition)
            {
            case FeatureFactory.Transition.Left:
            {
                while (true)
                {
                    if (node.Children().Length == 0)
                    {
                        return(null);
                    }
                    Tree child = node.Children()[0];
                    if (!(child.Label() is CoreLabel))
                    {
                        throw new ArgumentException("Can only featurize CoreLabel trees");
                    }
                    if (((CoreLabel)child.Label()).Get(typeof(TreeCoreAnnotations.HeadWordLabelAnnotation)) != head)
                    {
                        return((CoreLabel)child.Label());
                    }
                    node = child;
                }
                goto case FeatureFactory.Transition.Right;
            }

            case FeatureFactory.Transition.Right:
            {
                while (true)
                {
                    if (node.Children().Length == 0)
                    {
                        return(null);
                    }
                    if (node.Children().Length == 1)
                    {
                        node = node.Children()[0];
                        continue;
                    }
                    Tree child = node.Children()[1];
                    if (!(child.Label() is CoreLabel))
                    {
                        throw new ArgumentException("Can only featurize CoreLabel trees");
                    }
                    if (((CoreLabel)child.Label()).Get(typeof(TreeCoreAnnotations.HeadWordLabelAnnotation)) != head)
                    {
                        return((CoreLabel)child.Label());
                    }
                    node = child;
                }
                goto default;
            }

            default:
            {
                throw new ArgumentException("Can only get left or right heads");
            }
            }
        }