Ejemplo n.º 1
0
        public override void AfterSpeechRecognition(string device, string text, double confidence, XPathNavigator xnav, string grammar, Stream stream, IDictionary <string, string> options)
        {
            base.AfterSpeechRecognition(device, text, confidence, xnav, grammar, stream, options);

            // Reset context timeout
            ContextManager.GetInstance().ResetContextTimeout();

            // Update Context
            var context = xnav.SelectSingleNode("/SML/action/@context");

            if (context != null)
            {
                ContextManager.GetInstance().SetContext(context.Value);
                ContextManager.GetInstance().ApplyContextToEngines();
                ContextManager.GetInstance().StartContextTimeout();
            }

            // Update a Grammar with XML
            var asknext = xnav.SelectSingleNode("/SML/action/@asknext");

            if (asknext != null)
            {
                var example = GrammarManager.GetInstance().FindExample(asknext.Value);
                AddOnManager.GetInstance().BeforeHandleVoice(example, true);
            }
        }
Ejemplo n.º 2
0
        public void SetContext(String context)
        {
            if (context == null)
            {
                return;
            }
            Log("Context: " + context);
            if ("default".Equals(context))
            {
                SetContext(Default);
                Current = null;
                return;
            }
            bool all = "all".Equals(context);

            foreach (SpeechGrammar g in GrammarManager.GetInstance().Cache.Values)
            {
                if (g.Name == "dictation")
                {
                    continue;
                }
                g.Enabled = all || context.Equals(g.Name);
                Debug(g.Name + " = " + g.Enabled);
            }
        }
Ejemplo n.º 3
0
        public void SetContext(ICollection <string> context)
        {
            if (context == null)
            {
                SetContext("default"); return;
            }
            if (context.Count < 1)
            {
                return;
            }
            Current = context;
            if (context.Count == 1)
            {
                var e = context.GetEnumerator();
                e.MoveNext();
                SetContext(e.Current);
                return;
            }

            Log("Context: " + String.Join(", ", context));
            foreach (SpeechGrammar g in GrammarManager.GetInstance().Cache.Values)
            {
                if (g.Name == "dictation")
                {
                    continue;
                }
                g.Enabled = context.Contains(g.Name);
                Debug(g.Name + " = " + g.Enabled);
            }
        }
Ejemplo n.º 4
0
        public override void AfterHTTPRequest(string qs, NameValueCollection parameters, IDictionary files)
        {
            base.AfterHTTPRequest(qs, parameters, files);
            var querystring = System.Web.HttpUtility.ParseQueryString(qs);

            // Update Context
            var context = querystring.GetValues("context");

            if (context != null)
            {
                ContextManager.GetInstance().SetContext(context);
                ContextManager.GetInstance().ApplyContextToEngines();
                ContextManager.GetInstance().StartContextTimeout();
            }

            // Update a Grammar with XML
            var grammar = parameters.Get("grammar");

            if (grammar != null)
            {
                GrammarManager.GetInstance().UpdateXML(grammar, parameters.Get("xml"));
            }

            // AskMe: start a Dynamic Grammar
            String[] sentences   = querystring.GetValues("sentences");
            String[] tags        = querystring.GetValues("tags");
            String   callbackUrl = parameters.Get("callbackUrl");

            if (sentences != null && tags != null)
            {
                ContextManager.GetInstance().LoadDynamicGrammar(sentences, tags, callbackUrl);
                ContextManager.GetInstance().SetContext("Dyn");
                ContextManager.GetInstance().ApplyContextToEngines();
            }

            // AskNext: TTS Acording to ruleId
            var asknext = parameters.Get("asknext");

            if (asknext != null)
            {
                var example = GrammarManager.GetInstance().FindExample(asknext);
                if (example != null)
                {
                    var regexp  = new Regex("\\[([^\\]]+)\\]");
                    var matches = regexp.Matches(example);
                    foreach (Match match in matches)
                    {
                        var key   = match.Groups[1];
                        var value = parameters.Get(key.ToString());
                        if (value == null)
                        {
                            value = "";
                        }
                        example = example.Replace("[" + key + "]", value);
                    }
                    AddOnManager.GetInstance().BeforeHandleVoice(example, true);
                }
            }
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        // -------------------------------------------
        //  SPEECH
        // -------------------------------------------

        protected bool Confidence(String device, RecognitionResult rr, XPathNavigator xnav, double threashold)
        {
            double confidence = rr.Confidence;

            // Override Engine threashold by grammar
            XPathNavigator level = xnav.SelectSingleNode("/SML/action/@threashold");

            if (level != null)
            {
                Log("Override confidence: " + level.Value);
                threashold = level.ValueAsDouble;
            }

            // Search for bot name
            double match = 0;
            double bot   = ConfigManager.GetInstance().Find("bot.confidence", 0.0);
            string name  = ConfigManager.GetInstance().Find("bot.name", "SARAH").ToUpper();

            foreach (var word in rr.Words)
            {
                if (!name.Equals(word.Text.ToUpper()))
                {
                    continue;
                }
                if (word.Confidence < threashold + bot) // Check the bot name with threashold confidence
                {
                    Log("REJECTED " + name + ": " + word.Confidence + " < " + (threashold + bot) + " (" + rr.Text + ")");
                    return(false);
                }
                match = word.Confidence;
                break;
            }

            // Must have bot name in sentence
            var grammar = GrammarManager.GetInstance().FindGrammar(rr.Grammar.Name);

            if (match == 0 && grammar.HasName && !AddOnManager.GetInstance().IsEngaged(device))
            {
                return(false);
            }

            // Check full sentence
            if (confidence < threashold)
            {
                Log("REJECTED: " + confidence + " (" + match + ") " + " < " + threashold + " (" + rr.Text + ")");
                return(false);
            }

            // Speech recognized
            Log("RECOGNIZED: " + confidence + " (" + match + ") " + " > " + threashold + " (" + rr.Text + ")");
            return(true);
        }
Ejemplo n.º 7
0
        // ------------------------------------------
        //  AddOn life cycle
        // ------------------------------------------

        public override void Start()
        {
            base.Start();

            // Seek plugin folder
            var plugins = ConfigManager.GetInstance().Find("bot.plugins", ConfigManager.GetInstance().PATH_PLUGIN);

            // Load all Grammar
            GrammarManager.GetInstance().Load(plugins);

            // Watch grammar folder
            GrammarManager.GetInstance().Watch(plugins);
        }
Ejemplo n.º 8
0
        public void AddSpeechEngine(Stream stream, string format, String device, String language, double confidence)
        {
            language = (language == null) ? ConfigManager.GetInstance().Find("bot.language", "fr-FR") : language;

            var info = new SpeechAudioFormatInfo(16000, AudioBitsPerSample.Sixteen, AudioChannel.Stereo);

            if ("Kinect".Equals(format))
            {
                info = new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null);
            }

            SpeechEngine engine = new SpeechEngine(device, language, confidence);

            engine.Load(GrammarManager.GetInstance().Cache, false);
            engine.Init();
            engine.Engine.SetInputToAudioStream(stream, info);
            engine.Start();

            Engines.Add(device, engine);
        }
Ejemplo n.º 9
0
 public override void Dispose()
 {
     base.Dispose();
     GrammarManager.GetInstance().Dispose();
     SpeechManager.GetInstance().Dispose();
 }
Ejemplo n.º 10
0
 public void ApplyContextToEngines()
 {
     Log("Forward Context enable/disable grammar");
     GrammarManager.GetInstance().ApplyGrammarsToEngines();
 }