Ejemplo n.º 1
0
        /// <summary>
        /// 播放url内容,播放完成后自动唤醒
        /// </summary>
        /// <param name="msg">user:wali_Server type:other
        /// msg:wali_C40BCB80050A;3;3011;ac83f318064f(设备id);64(中文)1$/r$</param>
        public static string SoundMsg(string msg)
        {
            string devmac = msg.Split(';')[3];//给指定的音响发送消息
            string msg64j = EncryptionHelp.Decrypt(msg.Split(';')[4].Replace("$/r$", ""), false);
            //SoundPassiveRequest _SoundPassiveRequest = new SoundPassiveRequest()
            //{
            //    sessionId = Guid.NewGuid().ToString(),
            //    deviceId = devmac,
            //    actionId = "2011",
            //    req= msg64j,
            //    url= BaiduSDK.Tts(msg64j),
            //    blwakeup = "0"
            //};
            SoundBodyResult _SoundBodyRequest = new SoundBodyResult()
            {
                sessionId = Guid.NewGuid().ToString(),
                deviceId  = devmac,
                questions = "提醒",
                actionId  = "301",//与2011相似
                req       = msg64j,
                url       = BaiduSDK.Tts(msg64j),
                blwakeup  = "0"
            };

            if (_NlpControlerRequestMsg(_SoundBodyRequest))
            {
                return($"Remind OK");
            }
            else
            {
                return($"Remind Fail");
            }
        }
Ejemplo n.º 2
0
        public static void AIFunction(SoundBodyRequest body)
        {
            //百度分词
            BaiduNlp nlpEntity = BaiduSDK.Nlp(body.questions);

            //添加句子表,分词表
            string talkDevice   = "Talk_" + body.deviceId;              //每个音响创建一个会话
            long   newTimestamp = DataHelper.GetTimeSpan(DateTime.Now); //句子时间戳
            string newTitleid   = nlpEntity.log_id.ToString();          //句子id
            string _Talkid      = "";
            string userid       = RedisHelper.GetHostId(body.deviceId); //音响对应用户

            body.timestamp   = newTimestamp;                            //	时间标签(Long型)
            body.baidu_Items = nlpEntity.items;                         //	分词列表(字符串数组)

            string field = AIControler.Getdomain(body);                 //获取领域

            body.field = field;                                         //领域
            int    sort       = 1;                                      //会话中句子的顺序
            string preTitleid = "";                                     //上一个句子id

            string questions = "";                                      //问题列表:(字符串数组) 音箱的会话记录
            string answers   = "";                                      //回答列表:(字符串数组)  AIcontrol会话记录

            //创建会话缓存
            using (RedisHashService service = new RedisHashService())
            {
                string _timestampStr = service.GetValueFromHash(talkDevice, "timestamp");//获取缓存时间戳
                if (!string.IsNullOrEmpty(_timestampStr))
                {
                    long oldTimestamp = Convert.ToInt64(_timestampStr);
                    long cha          = newTimestamp - oldTimestamp;
                    if (cha > 60)                                                                     //一分钟60秒
                    {
                        _Talkid = body.deviceId + "_" + DateTime.Now.ToString("yyyyMMdd-HHmmss-fff"); //超过一分钟,创建新的会话缓存
                        service.SetEntryInHash(talkDevice, "talkid", _Talkid);                        //缓存会话id
                    }
                    //一分钟内继续使用,不更新
                    else
                    {
                        _Talkid = service.GetValueFromHash(talkDevice, "talkid");
                        string sortStr = service.GetValueFromHash(talkDevice, "sort");//先获取当前顺序
                        if (!string.IsNullOrEmpty(sortStr))
                        {
                            sort = Convert.ToInt32(sortStr);
                        }
                        sort++;//一分钟内顺序+1
                    }
                }
                else
                {
                    _Talkid = body.deviceId + "_" + DateTime.Now.ToString("yyyyMMdd-HHmmss-fff"); //没有新建
                    service.SetEntryInHash(talkDevice, "talkid", _Talkid);                        //缓存会话id
                }
                preTitleid = service.GetValueFromHash(talkDevice, "titleid");                     //前个句子id
                //每次都更新句子时间戳
                service.SetEntryInHash(talkDevice, "titleid", newTitleid);                        //缓存新的句子id
                service.SetEntryInHash(talkDevice, "timestamp", newTimestamp.ToString());         //缓存新的时间戳
                service.SetEntryInHash(talkDevice, "field", field);                               //新的领域
                service.SetEntryInHash(talkDevice, "sort", sort.ToString());                      //顺序


                string talkstate = service.GetValueFromHash(talkDevice, "state"); //获取缓存会话模式,提前赋值
                body.talkstate = talkstate;                                       //会话模式
                talkstate      = "inquiry";
                if (talkstate == "inquiry" || talkstate == "await")               //询问,等待
                {
                    Task.Run(() =>
                    {
                        //不用请求任何Nlu服务器,直接丢给AIControl函数Setanswer处理,返回抛给音箱
                        AIControler.Setanswer(body);
                    });
                }
                //else if(_state== "train")//训练模式
                else
                {
                    Task.Run(() =>
                    {
                        //发送收到的语音到NLP管理器
                        NlpControler.ProcessingRequest(body);
                    });
                }

                //启动线程存数据库
                Task.Run(() =>
                {
                    using (HsfDBContext hsfDBContext = new HsfDBContext())
                    {
                        //句子表
                        sound_title sound_Title = new sound_title()
                        {
                            titleid   = newTitleid,
                            titletext = body.questions,
                            timestamp = newTimestamp,
                            preid     = preTitleid,
                            sort      = sort,
                            talkid    = _Talkid,
                            sender    = body.deviceId,
                            userid    = userid,
                            sendtype  = body.sourceId,
                            field     = field,
                            talkstate = talkstate
                        };
                        hsfDBContext.sound_title.Add(sound_Title);

                        int items_sort = 0;
                        //分词表
                        foreach (var item in nlpEntity.items)
                        {
                            items_sort++;
                            baidu_items baidu_Items = new baidu_items()
                            {
                                wordid      = Guid.NewGuid().ToString(),
                                titleid     = newTitleid,
                                byte_length = item.byte_length,
                                byte_offset = item.byte_offset,
                                uri         = item.uri,
                                pos         = item.pos,
                                ne          = item.ne,
                                item        = item.item,
                                basic_words = string.Join(",", item.basic_words),
                                formal      = item.formal,
                                sort        = items_sort,
                                timestamp   = newTimestamp
                            };
                            hsfDBContext.baidu_items.Add(baidu_Items);
                        }
                        hsfDBContext.SaveChanges();
                    }
                    log.Info($"保存到句子表和分词表");
                });
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 ///调用第三方语义排序API,对于语义进行排序
 /// </summary>
 /// <param name="_SemanticsDictionary">传递语义槽</param>
 /// <returns>排序结果</returns>
 public static void GetAIAnswers(SemanticsSlot _SemanticsSlot, string type)
 {
     try
     {
         string sessionId = _SemanticsSlot.SessionId;
         string deviceId  = _SemanticsSlot.DeviceId;
         string questions = _SemanticsSlot.Questions;
         string sourceid  = _SemanticsSlot.SourceId;
         log.Info($"{questions} 执行方式:{type} 返回次数:{_SemanticsSlot.Answertimes} 语义槽数量:{_SemanticsSlot.NlpAnswers.Count} ");
         //过滤掉所有空
         var Answers = _SemanticsSlot.NlpAnswers.OrderBy(t => t.Level);//.Where(t => !string.IsNullOrEmpty(t.Answers))
         if (Answers.Count() > 0)
         {
             foreach (var item in Answers)
             {
                 string code    = item.Code;
                 string answers = item.Answers;
                 //优先返回基础nlp
                 if (code == "BaseNlp")
                 {
                     NlpControler.BackAnswers(sessionId, deviceId, questions, "2011", answers, BaiduSDK.Tts(answers));// 2011播放url内容,播放完自动唤醒
                     break;
                 }
                 //返回智能家居nlp,不带的设备无法识别@
                 else if (code == "SmartHomeNlp" && !answers.Contains("2014@"))
                 {
                     if (answers.Split('@').Length == 2)
                     {
                         string action = answers.Split('@')[0];
                         string req    = answers.Split('@')[1];
                         NlpControler.BackAnswers(sessionId, deviceId, questions, action, answers, "");//根据@中的action返回
                         break;
                     }
                     else
                     {
                         NlpControler.BackAnswers(sessionId, deviceId, questions, "2020", answers, "");//2020播放响应效果音可持续交流
                         break;
                     }
                 }
                 else
                 {
                     //设备无法识别的时候,优先返回音响自带的结果(不为空的情况下)
                     var item2 = _SemanticsSlot.NlpAnswers.Where(t => t.Code == "SoundNlp").FirstOrDefault();
                     if (sourceid != "mengdou")
                     {
                         ///执行百度自己请求
                         NlpControler.BackAnswers(sessionId, deviceId, questions, "2010", "无法识别您说的意思", "");
                         break;
                     }
                     else
                     if (item2 != null)
                     {
                         if (answers.Contains("service\":\"musicX") ||
                             answers.Contains("service\":\"news") ||
                             answers.Contains("service\":\"story") ||
                             answers.Contains("service\":\"joke"))
                         {
                             NlpControler.BackAnswers(sessionId, deviceId, questions, "2025", item2.Answers, "");//2025主机不返回,超时,播放错误音,其实主机已经在播放音乐
                             break;
                         }
                         else
                         {
                             NlpControler.BackAnswers(sessionId, deviceId, questions, "2011", item2.Answers, BaiduSDK.Tts(item2.Answers));// 2011播放url内容,播放完自动唤醒
                             break;
                         }
                     }
                     else
                     {
                         NlpControler.BackAnswers(sessionId, deviceId, questions, "2014", "无法识别您说的意思", "");
                         //NlpControler.BackAnswers(sessionId, deviceId, questions, "2014", "无法识别您说的意思", BaiduSDK.mp3Fail);
                         break;
                     }
                 }
             }
         }
         else
         {
             if (sourceid != "mengdou")
             {
                 ///执行百度自己请求
                 NlpControler.BackAnswers(sessionId, deviceId, questions, "2010", "无法识别您说的意思", "");
             }
             else
             {
                 NlpControler.BackAnswers(sessionId, deviceId, questions, "2014", "无法识别您说的意思", "");
             }
             //NlpControler.BackAnswers(sessionId, deviceId, questions, "2014", "无法识别您说的意思", BaiduSDK.mp3Fail);
         }
     }
     catch (Exception ex)
     {
         log.Info($"{_SemanticsSlot.Questions} AI处理异常: {ex.Message}");
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 不用请求任何Nlu服务器,直接丢给AIControl函数Setanswer处理,返回抛给音箱
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        public static void Setanswer(SoundBodyRequest body)
        {
            string answers = "不用请求任何Nlu服务器,直接丢给AIControl函数Setanswer处理,返回抛给音箱";

            NlpControler.BackAnswers(body.sessionId, body.deviceId, body.questions, "2011", answers, BaiduSDK.Tts(answers));
        }