コード例 #1
0
        public void LoadXML(String file, String name)
        {
            WSRConfig       cfg     = WSRConfig.GetInstance();
            WSRSpeecGrammar grammar = null;

            if (Cache.TryGetValue(name, out grammar))
            {
                if (grammar.LastModified == File.GetLastWriteTime(file))
                {
                    logDebug("GRAMMAR", "Ignoring: " + name + " (no changes)");
                    return;
                }
            }

            try {
                // Load the XML
                logInfo("GRAMMAR", "Load file: " + name + " : " + file);
                String xml = File.ReadAllText(file, Encoding.UTF8);
                xml = Regex.Replace(xml, "([^/])SARAH", "$1" + cfg.Name, RegexOptions.IgnoreCase);

                // Check regexp language
                if (!Regex.IsMatch(xml, "xml:lang=\"" + cfg.language + "\"", RegexOptions.IgnoreCase))
                {
                    logInfo("GRAMMAR", "Ignoring : " + name + " (" + cfg.language + ")");
                    return;
                }

                // New grammar
                if (null == grammar)
                {
                    grammar = new WSRSpeecGrammar();
                    Cache.Add(name, grammar); // Add to cache
                }

                // Build grammar
                grammar.XML          = xml;
                grammar.Name         = name;
                grammar.Path         = file;
                grammar.LastModified = File.GetLastWriteTime(file);

                // Check contexte
                grammar.Enabled = true;

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

                // Add to context if there is no context
                if (!WSRConfig.GetInstance().HasContext() && grammar.Enabled && !WSRConfig.GetInstance().context.Contains(name))
                {
                    WSRConfig.GetInstance().context.Add(name);
                    logInfo("GRAMMAR", "Add to context list: " + name);
                }
            }
            catch (Exception ex) {
                cfg.logError("GRAMMAR", ex);
            }
        }
コード例 #2
0
 public void ForwardContext()
 {
     logInfo("CONTEXT", "Forward Context enable/disable grammar");
     foreach (WSRSpeechEngine engine in Engines.Values)
     {
         foreach (Grammar g in engine.GetEngine().Grammars)
         {
             WSRSpeecGrammar s = null;
             if (Cache.TryGetValue(g.Name, out s))
             {
                 g.Enabled = s.Enabled;
                 logDebug("CONTEXT", g.Name + " = " + s.Enabled);
             }
         }
     }
 }
コード例 #3
0
        public void LoadGrammar(String file, String name)
        {
            logInfo("GRAMMAR", "Load file: " + name + " : " + file);
              WSRConfig cfg = WSRConfig.GetInstance();
              // Load the XML
              String xml = File.ReadAllText(file, Encoding.UTF8);
              xml = Regex.Replace(xml, "([^/])SARAH", "$1" + cfg.name, RegexOptions.IgnoreCase);

              // Check regexp language
              if (!Regex.IsMatch(xml, "xml:lang=\"" + cfg.language + "\"", RegexOptions.IgnoreCase)) {
            logInfo("GRAMMAR", "Ignoring: " + name + " (" + cfg.language + ")");
            return;
              }

              // Build grammar
              WSRSpeecGrammar grammar = new WSRSpeecGrammar();
              grammar.XML = xml;
              grammar.Name = name;
              grammar.Path = file;

              // Check contexte
              grammar.Enabled = true;

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

              // Add to context if there is no context
              if (!WSRConfig.GetInstance().HasContext() && grammar.Enabled) {
            WSRConfig.GetInstance().context.Add(name);
            logInfo("GRAMMAR", "Add to context list: " + name);
              }

              // Cache XML
              cache.Add(name, grammar);
        }
コード例 #4
0
    protected bool LoadXML(String[] g, String[] tags) {
      WSRConfig cfg = WSRConfig.GetInstance();

      if (null == Engines)           { return false; }
      if (null == g || null == tags) { return false; }
      if (g.Length != tags.Length)   { return false; }

      bool rejected = false;
      bool garbage = false;
      var xml  = "\n<grammar version=\"1.0\" xml:lang=\""+ cfg.language +"\" mode=\"voice\"  root=\"ruleDyn\" xmlns=\"http://www.w3.org/2001/06/grammar\" tag-format=\"semantics/1.0\">";
          xml += "\n<rule id=\"ruleDyn\" scope=\"public\">";
          xml += "\n<tag>out.action=new Object(); </tag>";
          xml += "\n<one-of>";
          for (var i = 0; i < g.Length; i++) {
            logInfo("GRAMMAR", "Add to DynGrammar: " + g[i] + " => " + tags[i]);
            if (g[i].IndexOf('*') >= 0) { garbage = true; }
            if (g[i].Equals("*")) { rejected = true; continue; }
            xml += "\n<item>" + g[i].Replace("*","<ruleref special=\"GARBAGE\" />") + "<tag>out.action.tag=\"" + tags[i] + "\"</tag></item>";
          }
          xml += "\n</one-of>";
          xml += "\n<tag>out.action._attributes.uri=\"http://127.0.0.1:8080/askme\";</tag>";
          if (garbage) {
            xml += "\n<tag>out.action._attributes.dictation=\"true\";</tag>";
          }
          xml += "\n</rule>";
          xml += "\n</grammar>";

          xml = Regex.Replace(xml, "([^/])SARAH", "$1" + cfg.Name, RegexOptions.IgnoreCase);

          logInfo("GRAMMAR", "DynGrammar" + (rejected ? " (rejected)" : "") + ":" + xml);

      WSRSpeecGrammar grammar = null;
      if (Cache.ContainsKey("Dyn")){
        grammar = Cache["Dyn"];
      } else {
        grammar = new WSRSpeecGrammar();
        grammar.Name = "Dyn";
        Cache.Add("Dyn", grammar);
      }

      grammar.XML = xml;
      grammar.LastModified = DateTime.Now;
      grammar.Enabled = false;
      grammar.Rejected = rejected ? "http://127.0.0.1:8080/askme" : null;

      foreach (WSRSpeechEngine engine in Engines.Values) {
        grammar.LoadGrammar(engine);
      }

      return true;
    }
コード例 #5
0
    public void LoadXML(String file, String name) {
      WSRConfig cfg = WSRConfig.GetInstance();
      WSRSpeecGrammar grammar = null;

      if (Cache.TryGetValue(name, out grammar)) {
        if (grammar.LastModified == File.GetLastWriteTime(file)) {
          logDebug("GRAMMAR", "Ignoring: " + name + " (no changes)");
          return;
        }
      }
      
      try {
        // Load the XML
        logInfo("GRAMMAR", "Load file: " + name + " : " + file);
        String xml = File.ReadAllText(file, Encoding.UTF8);
        xml = Regex.Replace(xml, "([^/])SARAH", "$1" + cfg.Name, RegexOptions.IgnoreCase);

        // Check regexp language
        if (!Regex.IsMatch(xml, "xml:lang=\"" + cfg.language + "\"", RegexOptions.IgnoreCase)) {
          logInfo("GRAMMAR", "Ignoring : " + name + " (" + cfg.language + ")");
          return;
        }

        // New grammar
        if (null == grammar) {
          grammar = new WSRSpeecGrammar();
          Cache.Add(name, grammar); // Add to cache
        }

        // Build grammar
        grammar.XML = xml;
        grammar.Name = name;
        grammar.Path = file;
        grammar.LastModified = File.GetLastWriteTime(file);

        // Check contexte
        grammar.Enabled = true;

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

        // Add to context if there is no context
        if (!WSRConfig.GetInstance().HasContext() && grammar.Enabled && !WSRConfig.GetInstance().context.Contains(name)) {
          WSRConfig.GetInstance().context.Add(name);
          logInfo("GRAMMAR", "Add to context list: " + name);
        }
      }
      catch (Exception ex) {
        cfg.logError("GRAMMAR", ex);
      }
    }
コード例 #6
0
        protected bool LoadXML(String[] g, String[] tags)
        {
            WSRConfig cfg = WSRConfig.GetInstance();

            if (null == Engines)
            {
                return(false);
            }
            if (null == g || null == tags)
            {
                return(false);
            }
            if (g.Length != tags.Length)
            {
                return(false);
            }

            bool rejected = false;
            bool garbage  = false;
            var  xml      = "\n<grammar version=\"1.0\" xml:lang=\"" + cfg.language + "\" mode=\"voice\"  root=\"ruleDyn\" xmlns=\"http://www.w3.org/2001/06/grammar\" tag-format=\"semantics/1.0\">";

            xml += "\n<rule id=\"ruleDyn\" scope=\"public\">";
            xml += "\n<tag>out.action=new Object(); </tag>";
            xml += "\n<one-of>";
            for (var i = 0; i < g.Length; i++)
            {
                logInfo("GRAMMAR", "Add to DynGrammar: " + g[i] + " => " + tags[i]);
                if (g[i].IndexOf('*') >= 0)
                {
                    garbage = true;
                }
                if (g[i].Equals("*"))
                {
                    rejected = true; continue;
                }
                xml += "\n<item>" + g[i].Replace("*", "<ruleref special=\"GARBAGE\" />") + "<tag>out.action.tag=\"" + tags[i] + "\"</tag></item>";
            }
            xml += "\n</one-of>";
            xml += "\n<tag>out.action._attributes.uri=\"http://127.0.0.1:8080/askme\";</tag>";
            if (garbage)
            {
                xml += "\n<tag>out.action._attributes.dictation=\"true\";</tag>";
            }
            xml += "\n</rule>";
            xml += "\n</grammar>";

            xml = Regex.Replace(xml, "([^/])SARAH", "$1" + cfg.Name, RegexOptions.IgnoreCase);

            logInfo("GRAMMAR", "DynGrammar" + (rejected ? " (rejected)" : "") + ":" + xml);

            WSRSpeecGrammar grammar = null;

            if (Cache.ContainsKey("Dyn"))
            {
                grammar = Cache["Dyn"];
            }
            else
            {
                grammar      = new WSRSpeecGrammar();
                grammar.Name = "Dyn";
                Cache.Add("Dyn", grammar);
            }

            grammar.XML          = xml;
            grammar.LastModified = DateTime.Now;
            grammar.Enabled      = false;
            grammar.Rejected     = rejected ? "http://127.0.0.1:8080/askme" : null;

            foreach (WSRSpeechEngine engine in Engines.Values)
            {
                grammar.LoadGrammar(engine);
            }

            return(true);
        }