示例#1
0
        /*
         * (non-Javadoc)
         * We want a very strict grammar structure like the following:
         * InitialNode ----> KW1 ---> KW2 .... ---> KWn ---> FinalNode
         *   ↑________________________________________________|
         */
        protected override GrammarNode createGrammar()
        {
            string silence = Constants.SILENCE_SPELLING;

            initialNode = createGrammarNode(silence);
            finalNode   = createGrammarNode(silence);
            GrammarNode lastNode = createGrammarNode(silence);

            initialNode.add(lastNode, LogMath.getLogOne());
            lastNode.add(initialNode, LogMath.getLogOne());
            GrammarNode lastWordGrammarNode = initialNode;

            /*Iterator<string> iter = tokens.iterator();
             *      while(iter.hasNext()){
             *              GrammarNode currNode = createGrammarNode(iter.next());
             *              lastWordGrammarNode.add(currNode, logMath.getLogOne());
             *              lastWordGrammarNode = currNode;
             *
             *              // Parallel keyword topology
             *              //initialNode.add(currNode, logMath.getLogOne());
             *
             *              //currNode.add(finalNode, logMath.getLogOne());
             *      }*/
            foreach (var token in tokens)
            {
                GrammarNode currNode = createGrammarNode(token);
                lastWordGrammarNode.add(currNode, LogMath.getLogOne());
                lastWordGrammarNode = currNode;
            }
            lastWordGrammarNode.add(lastNode, LogMath.getLogOne());
            lastNode.add(finalNode, logMath.linearToLog(0.0001));
            finalNode.setFinalNode(true);
            return(initialNode);
        }
示例#2
0
 public virtual void commitChanges()
 {
     try
     {
         if (this.loadGrammar)
         {
             if (this.manager == null)
             {
                 this.getGrammarManager();
             }
             this.ruleGrammar = this.loadNamedGrammar(this.grammarName);
             this.loadImports(this.ruleGrammar);
             this.loadGrammar = false;
         }
         this.manager.linkGrammars();
         this.__ruleStack = new JSGFGrammar.RuleStack(this);
         this.newGrammar();
         this.firstNode = this.createGrammarNode("<sil>");
         GrammarNode grammarNode = this.createGrammarNode("<sil>");
         grammarNode.setFinalNode(true);
         Iterator iterator = this.ruleGrammar.getRuleNames().iterator();
         while (iterator.hasNext())
         {
             string text = (string)iterator.next();
             if (this.ruleGrammar.isRulePublic(text))
             {
                 string fullRuleName = this.getFullRuleName(text);
                 JSGFGrammar.GrammarGraph grammarGraph = new JSGFGrammar.GrammarGraph(this);
                 this.__ruleStack.push(fullRuleName, grammarGraph);
                 JSGFRule rule = this.ruleGrammar.getRule(text);
                 JSGFGrammar.GrammarGraph grammarGraph2 = this.processRule(rule);
                 this.__ruleStack.pop();
                 this.firstNode.add(grammarGraph.getStartNode(), 0f);
                 grammarGraph.getEndNode().add(grammarNode, 0f);
                 grammarGraph.getStartNode().add(grammarGraph2.getStartNode(), 0f);
                 grammarGraph2.getEndNode().add(grammarGraph.getEndNode(), 0f);
             }
         }
         this.postProcessGrammar();
         if (this.logger.isLoggable(Level.FINEST))
         {
             this.dumpGrammar();
         }
     }
     catch (MalformedURLException ex)
     {
         throw new IOException(new StringBuilder().append("bad base grammar URL ").append(this.baseURL).append(' ').append(ex).toString(), ex);
     }
 }
示例#3
0
        //@Override
        protected override GrammarNode createGrammar()
        {
            //logger.info("Creating Grammar");
            initialNode = createGrammarNode(Constants.SILENCE_SPELLING);
            finalNode   = createGrammarNode(Constants.SILENCE_SPELLING);
            finalNode.setFinalNode(true);
            GrammarNode branchNode = createGrammarNode(false);

            List <GrammarNode> wordGrammarNodes = new List <GrammarNode>();
            int end = tokens.Count;

            //logger.info("Creating Grammar nodes");
            foreach (var word in tokens.Take(end))
            {
                GrammarNode wordNode = createGrammarNode(word.toLowerCase());
                wordGrammarNodes.Add(wordNode);
            }
            //logger.info("Done creating grammar node");

            // now connect all the GrammarNodes together
            initialNode.add(branchNode, LogMath.getLogOne());

            createBaseGrammar(wordGrammarNodes, branchNode, finalNode);

            if (modelDeletions)
            {
                addForwardJumps(wordGrammarNodes, branchNode, finalNode);
            }
            if (modelBackwardJumps)
            {
                addBackwardJumps(wordGrammarNodes, branchNode, finalNode);
            }
            if (modelRepetitions)
            {
                addSelfLoops(wordGrammarNodes);
            }
            //logger.info("Done making Grammar");
            //initialNode.dumpDot("./graph.dot");
            return(initialNode);
        }
 public override void commitChanges()
 {
     try
     {
         if (this.loadGrammar)
         {
             if (this.manager == null)
             {
                 this.getGrammarManager();
             }
             this.loadXML();
             this.loadGrammar = false;
         }
         this.__ruleStack = new JSGFGrammar.RuleStack(this);
         this.newGrammar();
         this.firstNode = this.createGrammarNode("<sil>");
         GrammarNode grammarNode = this.createGrammarNode("<sil>");
         grammarNode.setFinalNode(true);
         Iterator iterator = this.rules.entrySet().iterator();
         while (iterator.hasNext())
         {
             Map.Entry entry = (Map.Entry)iterator.next();
             JSGFGrammar.GrammarGraph grammarGraph = new JSGFGrammar.GrammarGraph(this);
             this.__ruleStack.push((string)entry.getKey(), grammarGraph);
             JSGFGrammar.GrammarGraph grammarGraph2 = this.processRule((JSGFRule)entry.getValue());
             this.__ruleStack.pop();
             this.firstNode.add(grammarGraph.getStartNode(), 0f);
             grammarGraph.getEndNode().add(grammarNode, 0f);
             grammarGraph.getStartNode().add(grammarGraph2.getStartNode(), 0f);
             grammarGraph2.getEndNode().add(grammarGraph.getEndNode(), 0f);
         }
         this.postProcessGrammar();
     }
     catch (MalformedURLException ex)
     {
         throw new IOException(new StringBuilder().append("bad base grammar URL ").append(this.baseURL).append(' ').append(ex).toString());
     }
 }