Ejemplo n.º 1
0
        private bool LoadXML(SpeechGrammar grammar, string xml)
        {
            // Check Language
            if (!Regex.IsMatch(xml, "xml:lang=\"" + language + "\"", RegexOptions.IgnoreCase))
            {
                Log("Ignoring : " + grammar.Name + " (" + language + ")");
                return(false);
            }

            // Clean XML
            var bot = hotword.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);
        }
Ejemplo n.º 2
0
        public SpeechGrammar UpdateXML(SpeechGrammar grammar, String bodyXML, bool ms)
        {
            if (grammar == null)
            {
                return(null);
            }
            var o = ms ? "$" : "out";

            // 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=\"" + language + "\" mode=\"voice\"  root=\"" + name + "\" xmlns=\"http://www.w3.org/2001/06/grammar\" tag-format=\"" + (ms ? "semantics-ms/1.0" : "semantics/1.0") + "\">";

            xml += "\n<rule id=\"" + name + "\" scope=\"public\">";
            xml += "\n<tag>" + o + ".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
            engine.Load(name, grammar.Build());

            return(grammar);
        }
Ejemplo n.º 3
0
        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) {
                Log("Exception:\n" + ex.StackTrace);
            }
        }