Example #1
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 #2
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 #3
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");
            }
            }
        }