private bool LoadXML(SpeechGrammar grammar, string xml)
        {
            // Check Language
            var language = ConfigManager.GetInstance().Find("bot.language", "fr-FR");

            if (!Regex.IsMatch(xml, "xml:lang=\"" + language + "\"", RegexOptions.IgnoreCase))
            {
                Log("Ignoring : " + grammar.Name + " (" + language + ")");
                return(false);
            }

            // Clean XML
            var bot = ConfigManager.GetInstance().Find("bot.name", "SARAH").ToUpper();

            // Replace SaRaH by bot name
            xml = Regex.Replace(xml, "([^/])SARAH", "$1" + bot, RegexOptions.IgnoreCase);

            // Add optional SARAH
            var item = "<item>\\s*" + bot + "\\s*</item>";

            if (Regex.IsMatch(xml, item, RegexOptions.IgnoreCase))
            {
                xml             = Regex.Replace(xml, item, "<item repeat=\"0-1\">" + bot + "</item>", RegexOptions.IgnoreCase);
                grammar.HasName = true;
            }

            // Set XML
            grammar.SetXML(xml);
            return(true);
        }
        public void LoadDynamicGrammar(String[] sentences, String[] tags, String callbackUrl)
        {
            if (sentences == null || tags == null)
            {
                return;
            }
            if (sentences.Length != tags.Length)
            {
                return;
            }

            // Create once
            if (DynGrammar == null)
            {
                DynGrammar = new SpeechGrammar("Dyn");
                DynGrammar.LastModified = DateTime.Now;
                DynGrammar.Enabled      = false;
            }

            // Build XML
            bool garbage = false;
            var  xml     = "\n<one-of>";

            for (var i = 0; i < sentences.Length; i++)
            {
                Log("Add to DynGrammar: " + sentences[i] + " => " + tags[i]);
                if (sentences[i].IndexOf('*') >= 0)
                {
                    garbage = true;
                }
                if (sentences[i].Equals("*"))
                {
                    if (callbackUrl == null)
                    {
                        callbackUrl = "http://127.0.0.1:8080/askme";
                    }
                    continue;
                }
                xml += "\n<item>" + sentences[i].Replace("*", "<ruleref special=\"GARBAGE\" />") + "<tag>out.action.tag=\"" + tags[i] + "\"</tag></item>";
            }
            xml += "\n</one-of>";
            xml += "\n<tag>out.action._attributes.uri=\"" + callbackUrl + "\";</tag>";
            if (garbage)
            {
                xml += "\n<tag>out.action._attributes.dictation=\"true\";</tag>";
            }

            // Fix callback
            DynGrammar.CallbackURL = callbackUrl;

            // Update the XML
            GrammarManager.GetInstance().UpdateXML(DynGrammar, xml);
        }
    public void LoadDynamicGrammar(String[] sentences, String[] tags, String callbackUrl) {

      if (sentences == null || tags == null) { return; }
      if (sentences.Length  != tags.Length) { return; }

      // Create once
      if (DynGrammar == null) {
        DynGrammar = new SpeechGrammar("Dyn");
        DynGrammar.LastModified = DateTime.Now;
        DynGrammar.Enabled = false;
      }

      // Build XML
      bool garbage = false;
      var xml = "\n<one-of>";
      for (var i = 0; i < sentences.Length; i++) {
        Log("Add to DynGrammar: " + sentences[i] + " => " + tags[i]);
        if (sentences[i].IndexOf('*') >= 0) { garbage = true; }
        if (sentences[i].Equals("*")) {
          if (callbackUrl == null) { callbackUrl = "http://127.0.0.1:8080/askme"; }
          continue; 
        }
        xml += "\n<item>" + sentences[i].Replace("*", "<ruleref special=\"GARBAGE\" />") + "<tag>out.action.tag=\"" + tags[i] + "\"</tag></item>";
      }
      xml += "\n</one-of>";
      xml += "\n<tag>out.action._attributes.uri=\"" + callbackUrl + "\";</tag>";
      if (garbage) {
        xml += "\n<tag>out.action._attributes.dictation=\"true\";</tag>";
      }

      // Fix callback
      DynGrammar.CallbackURL = callbackUrl;

      // Update the XML
      GrammarManager.GetInstance().UpdateXML(DynGrammar, xml);
    }
        public SpeechGrammar UpdateXML(SpeechGrammar grammar, String bodyXML)
        {
            if (grammar == null)
            {
                return(null);
            }

            // Include BODY to XML
            var name = grammar.Name;
            var rule = "rule" + Char.ToUpper(name[0]) + name.Substring(1);
            var xml  = "\n<grammar version=\"1.0\" xml:lang=\"" + ConfigManager.GetInstance().Find("bot.language", "fr-FR") + "\" mode=\"voice\"  root=\"" + name + "\" xmlns=\"http://www.w3.org/2001/06/grammar\" tag-format=\"semantics/1.0\">";

            xml += "\n<rule id=\"" + name + "\" scope=\"public\">";
            xml += "\n<tag>out.action=new Object(); </tag>";
            xml += bodyXML;
            xml += "\n</rule>";
            xml += "\n</grammar>";

            // Load Grammar
            LoadXML(grammar, xml);
            grammar.LastModified = DateTime.Now;

            // Add to cache
            if (!Cache.ContainsKey(name))
            {
                Cache[name] = grammar;
            }

            // Reload the XML of the Grammar
            foreach (SpeechEngine engine in SpeechManager.GetInstance().GetEngines())
            {
                engine.Load(name, grammar.Build());
            }

            return(grammar);
        }
        public void LoadFile(string path)
        {
            try {
                var name = Path.GetFileNameWithoutExtension(path);

                // Check Grammar cache
                SpeechGrammar grammar = FindGrammar(name);
                if (null != grammar)
                {
                    if (grammar.LastModified == File.GetLastWriteTime(path))
                    {
                        Log("Ignoring: " + name + " (no changes)"); return;
                    }
                }

                // New Grammar
                bool addToCache = false;
                if (null == grammar)
                {
                    grammar    = new SpeechGrammar(name);
                    addToCache = true;
                }

                // Load XML
                string xml = File.ReadAllText(path, Encoding.UTF8);
                if (!LoadXML(grammar, xml))
                {
                    return;
                }

                // Add new grammar to cache
                if (addToCache)
                {
                    Cache.Add(name, grammar);
                }

                // Setup grammar
                grammar.Path         = path;
                grammar.LastModified = File.GetLastWriteTime(path);

                // Check lazy
                grammar.Enabled = true;
                if ((path.IndexOf("lazy") >= 0) || Regex.IsMatch(xml, "root=\"lazy\\w+\"", RegexOptions.IgnoreCase))
                {
                    grammar.Enabled = false;
                }

                // Check context
                var ctx = ConfigManager.GetInstance().Find("speech.grammar.context", "");
                if (!String.IsNullOrEmpty(ctx))
                {
                    var context = ctx.Split('|');
                    if (context.Length > 0 && !Array.Exists(context, delegate(object s) { return(s.Equals(name)); }))
                    {
                        grammar.Enabled = false;
                    }
                }

                // Log
                Log("Loading: " + name + " (" + grammar.Enabled + ")" + " (" + path + ")");

                // Store default context
                if (grammar.Enabled)
                {
                    ContextManager.GetInstance().Default.Add(name);
                    Log("Add to context list: " + name);
                }
            }
            catch (Exception ex) {
                Error(ex);
            }
        }
    public SpeechGrammar UpdateXML(SpeechGrammar grammar, String bodyXML) {
      if (grammar == null) { return null; }

      // Include BODY to XML
      var name = grammar.Name;
      var rule = "rule" + Char.ToUpper(name[0]) + name.Substring(1);
      var xml  = "\n<grammar version=\"1.0\" xml:lang=\"" + ConfigManager.GetInstance().Find("bot.language", "fr-FR") + "\" mode=\"voice\"  root=\"" + name + "\" xmlns=\"http://www.w3.org/2001/06/grammar\" tag-format=\"semantics/1.0\">";
      xml += "\n<rule id=\"" + name + "\" scope=\"public\">";
      xml += "\n<tag>out.action=new Object(); </tag>";
      xml += bodyXML;
      xml += "\n</rule>";
      xml += "\n</grammar>";

      // Load Grammar
      LoadXML(grammar, xml);
      grammar.LastModified = DateTime.Now;

      // Add to cache
      if (!Cache.ContainsKey(name)) {
        Cache[name] = grammar;
      }

      // Reload the XML of the Grammar
      foreach (SpeechEngine engine in SpeechManager.GetInstance().GetEngines()) {
        engine.Load(name, grammar.Build());
      }

      return grammar;
    }
    private bool LoadXML(SpeechGrammar grammar, string xml) {
      // Check Language
      var language = ConfigManager.GetInstance().Find("bot.language", "fr-FR");
      if (!Regex.IsMatch(xml, "xml:lang=\"" + language + "\"", RegexOptions.IgnoreCase)) {
        Log("Ignoring : " + grammar.Name + " (" + language + ")");
        return false;
      }

      // Clean XML
      var bot = ConfigManager.GetInstance().Find("bot.name", "SARAH").ToUpper();

      // Replace SaRaH by bot name
      xml = Regex.Replace(xml, "([^/])SARAH", "$1" + bot, RegexOptions.IgnoreCase);

      // Add optional SARAH
      var item = "<item>\\s*" + bot + "\\s*</item>";
      if (Regex.IsMatch(xml, item, RegexOptions.IgnoreCase)) {
        xml = Regex.Replace(xml, item, "<item repeat=\"0-1\">" + bot + "</item>", RegexOptions.IgnoreCase);
        grammar.HasName = true;
      }

      // Set XML
      grammar.SetXML(xml);
      return true;
    }
    public void LoadFile(string path) {
      try {
        var name = Path.GetFileNameWithoutExtension(path);

        // Check Grammar cache
        SpeechGrammar grammar = FindGrammar(name);
        if (null != grammar) {
          if (grammar.LastModified == File.GetLastWriteTime(path)) {
            Log("Ignoring: " + name + " (no changes)"); return;
          }
        }

        // New Grammar
        bool addToCache = false;
        if (null == grammar) {
          grammar = new SpeechGrammar(name);
          addToCache = true;
        }

        // Load XML
        string xml = File.ReadAllText(path, Encoding.UTF8);
        if (!LoadXML(grammar, xml)) { return; }

        // Add new grammar to cache
        if (addToCache) {
          Cache.Add(name, grammar);
        }

        // Setup grammar
        grammar.Path = path;
        grammar.LastModified = File.GetLastWriteTime(path);

        // Check lazy
        grammar.Enabled = true;
        if ((path.IndexOf("lazy") >= 0) || Regex.IsMatch(xml, "root=\"lazy\\w+\"", RegexOptions.IgnoreCase)) {
          grammar.Enabled = false;
        }

        // Check context
        var ctx = ConfigManager.GetInstance().Find("speech.grammar.context", "");
        if (!String.IsNullOrEmpty(ctx)) {
          var context = ctx.Split('|');
          if (context.Length > 0 && !Array.Exists(context, delegate(object s) { return s.Equals(name); })) { 
            grammar.Enabled = false;
          }
        }

        // Log
        Log("Loading: " + name + " (" + grammar.Enabled + ")" + " (" + path + ")");

        // Store default context
        if (grammar.Enabled) {
          ContextManager.GetInstance().Default.Add(name);
          Log("Add to context list: " + name);
        }
      }
      catch (Exception ex) {
        Error(ex);
      }
    }