예제 #1
0
        private static Dictionary <string, IPlugin> LoadPlugins(QueryClassification qc)
        {
            Dictionary <string, IPlugin> _Plugins = new Dictionary <string, IPlugin>();
            string path = @"C:\Program Files\EchoServer\Plugins";
            ICollection <IPlugin> plugins = GenericPluginLoader <IPlugin> .LoadPlugins(path);

            if (plugins != null && plugins.Count() > 0)
            {
                foreach (var item in plugins)
                {
                    _Plugins.Add(item.Name, item);

                    foreach (string action in item.Actions)
                    {
                        qc.AddPhraseToAction(action, item.Name);
                    }
                }
            }
            return(_Plugins);
        }
예제 #2
0
        public static void Main(string[] args)
        {
            ResponseTime                 responseTime = new ResponseTime();
            QueryClassification          qc           = new QueryClassification();
            Dictionary <string, IPlugin> _Plugins     = LoadPlugins(qc);

            while (true)
            {
                Message message = SQL.GetNextMessage();

                if (message == null || message.messageID == -1)
                {
                    Thread.Sleep(10);
                    continue;
                }
                Task.Factory.StartNew(() =>
                {
                    if (string.IsNullOrEmpty(message.textRequest))
                    {
                        message.textRequest = SpeechToText.Process(message.audioRequest);
                    }


                    KeyValuePair <string, string> query = qc.Classify(message.textRequest);

                    int responseTimeID = responseTime.Start(query.Key);
                    string delaymsg    = responseTime.GetDelayMessage(query.Key);
                    if (!string.IsNullOrWhiteSpace(delaymsg))
                    {
                        message.textResponse  = delaymsg;
                        message.audioResponse = GetAudio(delaymsg);
                        message.status        = Message.Status.delayed;
                        SQL.UpdateMessage(message);
                    }

                    if (query.Key == "help")
                    {
                        message.textResponse  = query.Value;
                        message.audioResponse = GetAudio(query.Value);
                        message.status        = Message.Status.ready;
                    }
                    else if (query.Key == "unknown")
                    {
                        message.textResponse  = query.Value;
                        message.audioResponse = GetAudio(query.Value);
                        message.status        = Message.Status.ready;
                    }
                    else if (query.Key == "blank")
                    {
                        message.status = Message.Status.error;
                    }
                    else if (_Plugins.ContainsKey(query.Key))
                    {
                        string request        = message.textRequest.Replace(query.Value, "").CleanText();
                        request               = request.Length > 0 ? request : message.textRequest;
                        IPlugin plugin        = _Plugins[query.Key];
                        string response       = plugin.Go(request);
                        message.textResponse  = response;
                        message.audioResponse = GetAudio(response);
                        message.status        = Message.Status.ready;
                    }
                    else
                    {
                        message.textResponse  = query.Value;
                        message.audioResponse = GetAudio("A loaded plugin has failed to produce a valid key."); // very unlikely.
                        message.status        = Message.Status.ready;
                    }

                    // post the message back to SQL.
                    message.audioRequest = new byte[0]; // remove request audio.
                    SQL.UpdateMessage(message);
                    responseTime.Stop(query.Key, responseTimeID);
                });
            }
        }