Example #1
0
        /// <summary>
        /// 图灵机器人识别接口
        /// </summary>
        /// <param name="ans">与图灵对话的字符串</param>
        /// <returns>图灵回复</returns>
        public static TuLingResult ConnectTu(string ans)
        {
            TuLingResult tuLingResult = new TuLingResult();
            string       res          = string.Empty;
            string       APIKEY       = "32b17e3aa352423dbb6dfbc93b90f657";

            if (!string.IsNullOrEmpty(ans))
            {
                WebRequest  wq = WebRequest.Create("http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + ans);
                WebResponse wp = wq.GetResponse();
                res          = new StreamReader(wp.GetResponseStream()).ReadToEnd();
                tuLingResult = JsonConvert.DeserializeObject <TuLingResult>(res);
            }
            return(tuLingResult);
        }
Example #2
0
        public ActionResult GetAnswer(BizRule bizRule, string text)
        {
            RuleResult     ruleResult   = new RuleResult();
            TuLingResult   tuLingResult = new TuLingResult();
            BizRecord      bizRecord    = new BizRecord();
            List <BizRule> bizRules     = new List <BizRule>();

            AspectF.Define
            .Log(log, "RuleController-QueryRule[post]开始", "RuleController-QueryRule[post]结束")
            .HowLong(log)
            .Retry(log)
            .Do(() =>
            {
                text     = RegularRemoveSign(text);
                bizRules = ruleService.List(bizRule).ToList();
                Dictionary <string, string> keyValues = new Dictionary <string, string>();
                foreach (var item in bizRules)
                {
                    //关键字一为Null
                    if (item.KeyWordOne == null)
                    {
                        if (text.Contains(item.KeyWordTwo))
                        {
                            text = text.Substring(0, text.IndexOf(item.KeyWordTwo) + item.KeyWordTwo.Length);
                            string keyWordTwo = item.KeyWordTwo;
                            text    = text.Replace(keyWordTwo, "");
                            bizRule = ruleService.SingleTwo(keyWordTwo);
                        }
                    }
                    //关键字二为Null
                    else if (item.KeyWordTwo == null)
                    {
                        if (text.Contains(item.KeyWordOne))
                        {
                            text    = text.Substring(text.IndexOf(item.KeyWordOne));
                            text    = text.Replace(item.KeyWordOne, "");
                            bizRule = ruleService.SingleOne(item.KeyWordOne);
                        }
                    }
                    //关键字均不为空
                    else if (text.Contains(item.KeyWordOne))
                    {
                        if (text.Contains(item.KeyWordTwo))
                        {
                            text = text.Substring(text.IndexOf(item.KeyWordOne));
                            text = text.Substring(0, text.IndexOf(item.KeyWordTwo) + item.KeyWordTwo.Length);
                            text = text.Replace(item.KeyWordOne, "");
                            text = text.Replace(item.KeyWordTwo, "");
                            keyValues.Add(item.KeyWordOne, item.KeyWordTwo);
                            bizRule = ruleService.Single(keyValues.Keys.SingleOrDefault(), keyValues.Values.SingleOrDefault());
                        }
                    }
                }
            });
            if (bizRule.RuleID == null)
            {
                //图灵机器人回复
                ruleResult.text           = ConnectTu(text).text;
                bizRecord.QuestionContent = text;
                recordService.Add(bizRecord);
            }
            else
            {
                ruleResult.text    = bizRule.Answer;
                ruleResult.url     = bizRule.Website;
                ruleResult.type    = bizRule.AnswerTypes;
                ruleResult.keyword = text;
            }
            return(new JsonResultPro(ruleResult, JsonRequestBehavior.AllowGet, "yyyy-MM-dd"));
        }