Ejemplo n.º 1
0
        /// <summary>
        /// Notifies the builder a new choices block is starting.
        /// </summary>
        public void StartNewChoices()
        {
            HierarchicalNode node = new HierarchicalNode("(");

            // If we're nesting this inside another choices node, then it must
            // be a child of that node (or an option). If it's not, then it is
            // a sequence to be added to the current chain.
            if (addToChoiceAsChild)
            {
                choicesNodeStack.Peek().AddChild(node);
            }
            else
            {
                chainNodeStack.Peek().AddNext(node);
            }

            choicesNodeStack.Push(node);
            addToChoiceAsChild = true;

            // It's okay to pop the stack since the one that is on there will
            // not be appended to anymore regardless since either this node
            // goes after it, or this node will appear after it when the EndX()
            // function is called. Therefore the chain will always be able to
            // continue and this node will eventually bubble up to its proper
            // position.
            PopChainStackIfAny();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the root node of what has been added so far.
        /// </summary>
        /// <returns>The root node.</returns>
        public HierarchicalNode GetRootNode()
        {
            if (!removedDummyNode)
            {
                rootNode         = rootNode.Next;
                removedDummyNode = true;
            }

            return(rootNode);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates an empty list that can be added to.
        /// </summary>
        /// <exception cref="ArgumentNullException>">If the rule name is null.
        /// </exception>
        /// <exception cref="ArgumentException">If the rule name is empty.
        /// </exception>
        public HierarchialListBuilder(string ruleIdentifier)
        {
            CheckNotNull(ruleIdentifier);
            CheckArgument(ruleIdentifier.Length > 0);

            RuleIdentifier   = ruleIdentifier;
            choicesNodeStack = new Stack <HierarchicalNode>();
            chainNodeStack   = new Stack <HierarchicalNode>();

            // There needs to be a starting node that we will allow us to
            // extend upon.
            removedDummyNode  = false;
            startingDummyNode = new HierarchicalNode("dummy");
            rootNode          = startingDummyNode;
            chainNodeStack.Push(startingDummyNode);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Compiles all the parsed data into .NET speech recognition objects.
        /// </summary>
        private void DoGrammarCompilation()
        {
            foreach (string key in ModuleMap.Keys)
            {
                foreach (string nodeName in ModuleMap[key].LoadedGrammarNodes.Keys)
                {
                    HierarchicalNode rootNode       = ModuleMap[key].LoadedGrammarNodes[nodeName];
                    GrammarBuilder   grammarBuilder = GrammarCompiler.CompileToGrammarBuilder(rootNode);

                    VCGrammar grammar        = new VCGrammar(grammarBuilder);
                    string    pythonFilePath = ModuleMap[key].PythonFilePath;
                    if (pythonFilePath != null && pythonFilePath.Length > 0)
                    {
                        grammar.SetPythonFilePath(pythonFilePath);
                    }
                    grammar.Name = nodeName;

                    CompiledGrammar.Add(grammar);
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds a new word to the compiled structure.
        /// </summary>
        /// <param name="word">The word to add.</param>
        /// <exception cref="ArgumentNullException">If the argument is null.
        /// </exception>
        /// <exception cref="ArgumentException">If the word is an empty string.
        /// </exception>
        public void AddNewWord(string word)
        {
            CheckNotNull(word);
            CheckArgument(word.Length > 0);

            HierarchicalNode node = new HierarchicalNode(word);

            // It either belongs as a child, or it is to be attached to the
            // next node.
            if (addToChoiceAsChild)
            {
                choicesNodeStack.Peek().AddChild(node);
            }
            else
            {
                chainNodeStack.Peek().AddNext(node);
            }

            PopChainStackIfAny(); // Any node on the stack can't be extended from now.
            chainNodeStack.Push(node);
            addToChoiceAsChild = false;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Compiles the node recursively, meaning its children are compiled
        /// and its next link (if any exists) is also compiled.
        /// </summary>
        /// <param name="node">The node to recursively compile.</param>
        /// <returns>The grammar builder of this object and the children and/or
        /// the next node all recursively.</returns>
        private GrammarBuilder CompileNodeRecursively(HierarchicalNode node)
        {
            Assert(node != null);

            GrammarBuilder grammarBuilderCurrent = null;
            GrammarBuilder grammarBuilderNext    = null;

            // If there's another element after this, compile that recursively first.
            if (node.Next != null)
            {
                grammarBuilderNext = CompileNodeRecursively(node.Next);
            }

            // If there's children, compile that; otherwise compile itself.
            if (node.Children.Count > 0)
            {
                Choices choices = new Choices();
                foreach (HierarchicalNode childNode in node.Children)
                {
                    choices.Add(CompileNodeRecursively(childNode));
                }
                grammarBuilderCurrent = new GrammarBuilder(choices, node.MinRepeat, node.MaxRepeat);
            }
            else
            {
                grammarBuilderCurrent = new GrammarBuilder(node.Value, node.MinRepeat, node.MaxRepeat);
            }

            // If there was a next node, link the next node after this one.
            if (grammarBuilderNext != null)
            {
                grammarBuilderCurrent.Append(grammarBuilderNext);
            }

            return(grammarBuilderCurrent);
        }
Ejemplo n.º 7
0
 public void AddNewVariable(HierarchicalNode rootNode)
 {
     AddNewWord("VARIABLE HERE"); // TODO - To be done later
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Compiles a root node to a .NET grammar object.
 /// </summary>
 /// <param name="rootNode">The node to compile from.</param>
 /// <returns>A .NET Grammar object that can be loaded into the speech
 /// recognition engine.</returns>
 /// <exception cref="ArgumentNullException">If the argument is null.
 /// </exception>
 public static GrammarBuilder CompileToGrammarBuilder(HierarchicalNode rootNode)
 {
     CheckNotNull(rootNode);
     return(new GrammarCompiler(rootNode).grammarBuilder);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Creates a grammar object from a root node.
 /// </summary>
 /// <param name="rootNode">The node to compile from.</param>
 private GrammarCompiler(HierarchicalNode rootNode)
 {
     Assert(rootNode != null);
     grammarBuilder = CompileNodeRecursively(rootNode);
 }
Ejemplo n.º 10
0
        public void AddChild(HierarchicalNode node)
        {
            CheckNotNull(node);

            _Children.Add(node);
        }
Ejemplo n.º 11
0
        public void AddNext(HierarchicalNode node)
        {
            CheckNotNull(node);

            Next = node;
        }