Exemplo n.º 1
0
        protected void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            RecognitionResult rr = e.Result;

            if (!WSRSpeechManager.GetInstance().Listening)
            {
                cfg.logInfo("ENGINE - " + Name, "REJECTED not listening");
                return;
            }

            if (IsWorking)
            {
                cfg.logInfo("ENGINE - " + Name, "REJECTED Speech while working: " + rr.Confidence + " Text: " + rr.Text);
                return;
            }

            var start = DateTime.Now;

            IsWorking = true;

            try {
                SpeechRecognized(rr);
            }
            catch (Exception ex) {
                cfg.logError("ENGINE - " + Name, ex);
            }

            IsWorking = false;
            var stop = DateTime.Now;

            cfg.logInfo("ENGINE - " + Name, "SpeechRecognized: " + (stop - start).TotalMilliseconds + "ms Text: " + rr.Text);
        }
Exemplo n.º 2
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);
            }
        }