public string Process(Areas.Chat.Models.UserSession userSession, string input, Extracting.ValidInfo info) { /* 分析: * 当手机型号被显式提出,则以提出的手机型号为临时目标 *--值未定 * --明确提出主语 * --没提出主语 * --有当前讨论的手机 * --没有当前讨论的手机 * --有候选手机 * --没有候选手机 * */ //用户希望系统根据当前上下文推荐一个符合其他要求的基础上的手机属性 eg:能推荐一种颜色给我吗? if (Regex.IsMatch(input, @"推荐")) { if (userSession.liveTable.Candidates.Count == 0) { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.NOTFOUND; return("不好意思,目前没有符合您要求的手机, 所以没有可以推荐的内容。我们会尽快收录。麻烦您改一下之前提出的条件吧。"); } else { // 目前推荐属性只支持单个属性的推荐 Models.CellPhone phone = userSession.liveTable.Candidates[0]; string retValue = ""; Type type = phone.GetType(); PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in props) { string value1 = Convert.ToString(pi.GetValue(phone, null)); string name = pi.Name; if (GlobalHash.SemanticToCN[name].Equals(info.items[0].name)) { retValue = value1; break; } } if (Regex.IsMatch(retValue, @"^\d+$") && !info.items[0].name.Equals("型号")) { List <Code.Extracting.InfoUnit> temp = new List <Extracting.InfoUnit>(); temp.Add(new Code.Extracting.InfoUnit(retValue, string.Format("{0},{1}", Convert.ToDouble(retValue) * 0.99, Convert.ToDouble(retValue) * 1.01))); Code.Extracting.InfoBlock oneblock = new Extracting.InfoBlock(info.items[0].name, "or", temp); userSession.liveTable.Parameter[info.items[0].name] = oneblock; } else { if (retValue.IndexOf("|") >= 0) { retValue = retValue.Split(new char[] { '|' })[0]; } List <Code.Extracting.InfoUnit> temp = new List <Extracting.InfoUnit>(); temp.Add(new Code.Extracting.InfoUnit(retValue, retValue)); Code.Extracting.InfoBlock oneblock = new Extracting.InfoBlock(info.items[0].name, "or", temp); userSession.liveTable.Parameter[info.items[0].name] = oneblock; } return(retValue + "是个不错的选择。"); } } bool isDirect = false; List <Models.CellPhone> list = new List <Models.CellPhone>(); string focusParameter = ""; foreach (var it in info.items) { if (String.IsNullOrEmpty(info.conflictFeedback) && it.name.Equals("型号")) { if (it.items.Count > 0) { isDirect = true; } } if (it.isFocus) { focusParameter = it.name; } } if (!isDirect) { // 已经推荐了一款手机,用户问“有什么颜色” if (userSession.liveTable.CurrentMode == Areas.Chat.Models.LiveTable.ModeType.RECOMMEND || userSession.liveTable.CurrentMode == Areas.Chat.Models.LiveTable.ModeType.DETAIL) { if (userSession.liveTable.TalkingAbout != null) { List <string> ps = new List <string>(); for (int i = 0; i < info.items.Count; i++) { ps.Add(info.items[i].name); } userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.DETAIL; string answer = Answering.Statement(userSession.liveTable.TalkingAbout, ps); if (input.IndexOf("有") != -1) { answer = answer.Replace("是", "有"); } return(answer); } } // 正在进行主动引导,用户问“有什么品牌” else if (userSession.liveTable.CurrentMode == Areas.Chat.Models.LiveTable.ModeType.ACTIVE || userSession.liveTable.CurrentMode == Areas.Chat.Models.LiveTable.ModeType.NOTFOUND || userSession.liveTable.CurrentMode == Areas.Chat.Models.LiveTable.ModeType.DAILYTALK) { List <Models.CellPhone> phonelist = Inquiry.Screening(userSession.liveTable.Parameter, info); Queue <String> queue = new Queue <String>(); // TODO if (focusParameter == "价格") { decimal?min = 99999, max = -1; for (int i = 0; i < phonelist.Count; i++) { if (phonelist[i].Price > max) { max = phonelist[i].Price; } if (phonelist[i].Price < min) { min = phonelist[i].Price; } } if (min != max) { return("价格有" + (int)min + "的和" + (int)max + "元的,不知您的预算如何?"); } } else if (focusParameter == "后置摄像头像素") { decimal?min = 99999, max = -1; for (int i = 0; i < phonelist.Count; i++) { if (phonelist[i].RearCameraPixel > max) { max = phonelist[i].RearCameraPixel; } if (phonelist[i].RearCameraPixel < min) { min = phonelist[i].RearCameraPixel; } } if (min != max) { return("从" + (int)min + "万和" + (int)max + "万像素" + ",您都可以选择。"); } } else if (focusParameter == "主屏尺寸") { decimal?min = 99999, max = -1; for (int i = 0; i < phonelist.Count; i++) { if (phonelist[i].ScreenSize > max) { max = phonelist[i].ScreenSize; } if (phonelist[i].ScreenSize < min) { min = phonelist[i].ScreenSize; } } if (min != max) { return("屏幕最小是" + ((double)min).ToString("0.0") + "寸" + ",而最大的是" + ((double)max).ToString("0.0") + "寸" + ",看您喜欢咯~"); } } else if (focusParameter == "CPU频率") { decimal?min = 99999, max = -1; for (int i = 0; i < phonelist.Count; i++) { if (phonelist[i].CpuFrequency > max) { max = phonelist[i].CpuFrequency; } if (phonelist[i].CpuFrequency < min) { min = phonelist[i].CpuFrequency; } } if (min != max) { return("频率最小是" + ((double)min).ToString("0.0") + "GHz" + ",而最大的是" + ((double)max).ToString("0.0") + "GHz。"); } } else if (focusParameter == "CPU核数") { decimal?min = 99999, max = -1; for (int i = 0; i < phonelist.Count; i++) { if (phonelist[i].CpuCoreNumber > max) { max = phonelist[i].CpuCoreNumber; } if (phonelist[i].CpuCoreNumber < min) { min = phonelist[i].CpuCoreNumber; } } if (min != max) { return("核数最小是" + ((double)min).ToString("0.0") + "核" + ",而最大的是" + ((double)max).ToString("0.0") + "核。"); } } else if (focusParameter == "品牌") { for (int i = 0; i < phonelist.Count; i++) { if (!queue.Contains(phonelist[i].Brand)) { queue.Enqueue(phonelist[i].Brand); } } if (queue.Count > 0) { String tmpStr = ""; if (queue.Count >= 5) { for (int i = 0; i < 5; i++) { tmpStr += queue.Dequeue() + "、"; } } else { for (int i = 0; i < queue.Count; i++) { tmpStr += queue.Dequeue() + "、"; } } if (tmpStr[tmpStr.Length - 1] == '|') { tmpStr = tmpStr.Substring(0, tmpStr.Length - 1); } return("有" + tmpStr + "等几个品牌可供选择。"); } } else if (focusParameter == "颜色") { for (int i = 0; i < phonelist.Count; i++) { if (!queue.Contains(phonelist[i].Color)) { queue.Enqueue(phonelist[i].Color); } } if (queue.Count > 0) { String tmpStr = ""; if (queue.Count >= 5) { for (int i = 0; i < 5; i++) { tmpStr += queue.Dequeue().Split(new char[] { '|' })[0] + "、"; } } else { for (int i = 0; i < queue.Count; i++) { tmpStr += queue.Dequeue().Split(new char[] { '|' })[0] + "、"; } } if (tmpStr[tmpStr.Length - 1] == '、') { tmpStr = tmpStr.Substring(0, tmpStr.Length - 1); } return("有" + tmpStr + "等这几种颜色供你选择。"); } } else if (focusParameter == "型号") { for (int i = 0; i < phonelist.Count; i++) { if (!queue.Contains(phonelist[i].Model)) { queue.Enqueue(phonelist[i].Brand); queue.Enqueue(phonelist[i].Model); } } //在向用户反馈型号信息的时候,用户看到只有一款准确型号的手机,有可能将其当作可以询问的当前手机 //例子 user:有什么型号的? // Ch2R:有 XXX 等几款手机。 //用户会将 XXX 手机当作 talkingabout的手机。 //所以在这里将反馈给用户的手机当作talkabout //并且将状态改为推荐状态 if (phonelist.Count == 1) { userSession.liveTable.TalkingAbout = phonelist[0]; userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; } if (queue.Count > 0) { String tmpStr = ""; if (queue.Count >= 10) { for (int i = 0; i < 5; i++) { tmpStr += queue.Dequeue() + queue.Dequeue() + "、"; } } else { for (int i = 0; i < queue.Count; i++) { tmpStr += queue.Dequeue() + queue.Dequeue() + "、"; } } if (tmpStr[tmpStr.Length - 1] == '|') { tmpStr = tmpStr.Substring(0, tmpStr.Length - 1); } return("有" + tmpStr + "等几种型号的手机供你选择。"); } } else { } } } else { string answer = ""; List <string> ps = new List <string>(); list = Inquiry.Screening(userSession.liveTable.Parameter, info); if (list.Count <= 0) { if (info.items.Count < 2) { answer = "亲,不好意思," + "没有" + info.items[0].items[0].rawData; return(answer); } else { answer = "亲,不好意思," + "我们还没收录您提到的这个型号,我们正在努力更新数据~~"; return(answer); } } else { for (int i = 0; i < info.items.Count; i++) { if (info.items[i].isFocus) { ps.Add(info.items[i].name); } } return(Answering.Statement(list[0], ps)); } } // 查询数据库,返回手机列表list list = Inquiry.Screening(userSession.liveTable.Parameter, info); userSession.liveTable.Candidates = list; if (list.Count > 3) { /* 如果筛选结果大于三款(且重要属性只有4个以下被确认),则进行主动引导 * 首先要先确定要引导的参数 * 然后还要记录当前状态变成主动引导 * 以及记录相关参数到live-table的RelativeParameter * */ int recordedImportantParameter = 0; //如果重要属性已有4个以上被确认,直接进行推荐,不继续引导 foreach (string s in Code.GlobalHash.ImportantParameter) { if (recordedImportantParameter >= 4) { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; userSession.liveTable.TalkingAbout = list[0]; return(Answering.Recommand(list[0])); } if (!userSession.liveTable.Parameter.ContainsKey(s)) { userSession.liveTable.RelatedParameter = s; userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.ACTIVE; return(Answering.ActiveLeading(s)); } else { recordedImportantParameter++; } } } else if (list.Count <= 0) { return(Answering.NotFound(userSession, info)); // 状态CurrentMode在该方法中变更 } else { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; userSession.liveTable.TalkingAbout = list[0]; return(Answering.Recommand(list[0])); } return(""); }
public string Process(Areas.Chat.Models.UserSession userSession, string input, Extracting.ValidInfo info) { //用户希望系统根据当前句的语义和之前的收集的信息立即推荐一款手机 eg:是不是能给我推荐一款三星手机? if (Regex.IsMatch(input, @"推荐")) { userSession.liveTable.Candidates = Inquiry.Screening(userSession.liveTable.Parameter, info); if (userSession.liveTable.Candidates.Count == 0) { return(Answering.NotFound(userSession, info)); } else { // 目前能对多个值已定的属性进行手机推荐 userSession.liveTable.TalkingAbout = userSession.liveTable.Candidates[0]; userSession.liveTable.Candidates.Remove(userSession.liveTable.TalkingAbout); userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; return(userSession.liveTable.TalkingAbout.Brand + userSession.liveTable.TalkingAbout.Model + "这款不错,你可以了解一下。"); } } bool isDirect = false; //是不是直接问一款准确的手机型号 bool isPropertyExist = false; //询问的属性是否已经存在于livetable中 List <Models.CellPhone> list = new List <Models.CellPhone>(); foreach (var it in info.items) { if (String.IsNullOrEmpty(info.conflictFeedback) && it.name.Equals("型号")) { if (it.items.Count > 0) { isDirect = true; } } if (userSession.liveTable.Parameter[it.name] != null) { isPropertyExist = true; } } if (!isDirect) { // 针对某一款手机 if (userSession.liveTable.CurrentMode != Areas.Chat.Models.LiveTable.ModeType.DEBUGGING && userSession.liveTable.CurrentMode != Areas.Chat.Models.LiveTable.ModeType.LEARNING && userSession.liveTable.CurrentMode != Areas.Chat.Models.LiveTable.ModeType.START && userSession.liveTable.CurrentMode != Areas.Chat.Models.LiveTable.ModeType.END) { if (userSession.liveTable.TalkingAbout != null) { if (!isPropertyExist) { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.DETAIL; string answer = ""; for (int i = 0; i < info.items.Count; i++) { string value = userSession.liveTable.TalkingAbout.GetType().GetProperty (Code.GlobalHash.SemanticToEN[info.items[i].name].ToString()).GetValue(userSession.liveTable.TalkingAbout, null).ToString(); if (Regex.IsMatch(info.items[i].items[0].normalData, @"\d+\.*\d+,\d+\.*\d+")) { string[] bounds = info.items[i].items[0].normalData.Split(new char[] { ',' }); if (Convert.ToDouble(bounds[0]) <= Convert.ToDouble(value) && Convert.ToDouble(value) <= Convert.ToDouble(bounds[1])) { answer += Code.Answering.getAnswerStatusFromSentence(userSession, true) + info.items[i].items[0].rawData; } else { answer += Code.Answering.getAnswerStatusFromSentence(userSession, false) + info.items[i].items[0].rawData; } } else { if (value.IndexOf(info.items[i].items[0].normalData) != -1) { answer += Code.Answering.getAnswerStatusFromSentence(userSession, true) + info.items[i].items[0].rawData; } else { answer += Code.Answering.getAnswerStatusFromSentence(userSession, false) + info.items[i].items[0].rawData; } } } if (answer[answer.Length - 1] == '|') { answer = answer.Substring(0, answer.Length - 1) + "。"; } else { answer = answer.Substring(0, answer.Length) + "。"; } return(answer); } else { userSession.liveTable.TalkingAbout = null; List <string> ps = new List <string>(); List <Models.CellPhone> phonelist = Inquiry.Screening(userSession.liveTable.Parameter, info); string answer = ""; if (phonelist.Count > 0) { userSession.liveTable.Candidates = phonelist; answer = ""; for (int i = 0; i < info.items.Count; i++) { ps.Add(info.items[i].name); } answer += Answering.Statement(phonelist[0], ps); if (input.IndexOf("有") != -1) { answer = answer.Replace("是", "有"); answer = answer.Replace("这款", ""); } else if (input.IndexOf("是") != -1) { answer = answer.Replace("有", "是"); answer = answer.Replace("这款", ""); } if (phonelist.Count <= 3) { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; userSession.liveTable.TalkingAbout = phonelist[0]; answer += Answering.Recommand(phonelist[0]); } else { foreach (string s in Code.GlobalHash.ImportantParameter) { if (!userSession.liveTable.Parameter.ContainsKey(s)) { userSession.liveTable.RelatedParameter = s; userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.ACTIVE; answer += Answering.ActiveLeading(s); break; } } } return(answer); } else { return(Answering.NotFound(userSession, info)); } } } else { List <string> ps = new List <string>(); List <Models.CellPhone> phonelist = Inquiry.Screening(userSession.liveTable.Parameter, info); string answer = ""; if (phonelist.Count > 0) { userSession.liveTable.Candidates = phonelist; answer = "是的,"; for (int i = 0; i < info.items.Count; i++) { ps.Add(info.items[i].name); } answer += Answering.Statement(phonelist[0], ps); if (input.IndexOf("有") != -1) { answer = answer.Replace("是", "有"); answer = answer.Replace("这款", ""); } else if (input.IndexOf("是") != -1) { answer = answer.Replace("有", "是"); answer = answer.Replace("这款", ""); } return(answer); } else { return(Answering.NotFound(userSession, info)); } } } } else { string answer = ""; List <string> ps = new List <string>(); list = Inquiry.Screening(userSession.liveTable.Parameter, info); if (list.Count <= 0) { if (info.items.Count < 2) { answer = "亲,不好意思," + "没有" + info.items[0].items[0].rawData; return(answer); } else { answer = "亲,不好意思," + "我们还没收录您提到的这个型号,我们正在努力更新数据~~"; return(answer); } } else { for (int i = 0; i < info.items.Count; i++) { ps.Add(info.items[i].name); } return(Answering.Statement(list[0], ps)); } } return(""); }
public string Process(Areas.Chat.Models.UserSession userSession, string input, Extracting.ValidInfo info) { //用户希望系统根据当前上下文推荐一个符合其他要求的基础上的手机属性 eg:能不能推荐一种颜色给我? if (Regex.IsMatch(input, @"推荐")) { if (userSession.liveTable.Candidates.Count == 0) { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.NOTFOUND; return("不好意思,目前没有符合您要求的手机, 所以没有可以推荐的内容。我们会尽快收录。麻烦您改一下之前提出的条件吧。"); } else { // 目前推荐属性只支持单个属性的推荐 Models.CellPhone phone = userSession.liveTable.Candidates[0]; string retValue = ""; Type type = phone.GetType(); PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in props) { string value1 = Convert.ToString(pi.GetValue(phone, null)); string name = pi.Name; if (GlobalHash.SemanticToCN[name].Equals(info.items[0].name)) { retValue = value1; break; } } if (Regex.IsMatch(retValue, @"^\d+$") && !info.items[0].name.Equals("型号")) { List <Code.Extracting.InfoUnit> temp = new List <Extracting.InfoUnit>(); temp.Add(new Code.Extracting.InfoUnit(retValue, string.Format("{0},{1}", Convert.ToDouble(retValue) * 0.99, Convert.ToDouble(retValue) * 1.01))); Code.Extracting.InfoBlock oneblock = new Extracting.InfoBlock(info.items[0].name, "or", temp); userSession.liveTable.Parameter[info.items[0].name] = oneblock; } else { if (retValue.IndexOf("|") >= 0) { retValue = retValue.Split(new char[] { '|' })[0]; } List <Code.Extracting.InfoUnit> temp = new List <Extracting.InfoUnit>(); temp.Add(new Code.Extracting.InfoUnit(retValue, retValue)); Code.Extracting.InfoBlock oneblock = new Extracting.InfoBlock(info.items[0].name, "or", temp); userSession.liveTable.Parameter[info.items[0].name] = oneblock; } return(retValue + "是个不错的选择。"); } } if (info.items.Count > 1) { // 诺基亚1200有什么颜色的? Areas.Chat.Models.LiveTable liveTableTemp = new Areas.Chat.Models.LiveTable(); List <string> ps = new List <string>(); List <Models.CellPhone> phonelist = Inquiry.Screening(liveTableTemp.Parameter, info); for (int i = 0; i < info.items.Count; i++) { ps.Add(info.items[i].name); } string answer = Answering.Statement(phonelist[0], ps); if (input.IndexOf("有") != -1) { answer = answer.Replace("是", "有"); } return(answer); } else { // 已经推荐了一款手机,用户问“有什么颜色” if (userSession.liveTable.CurrentMode == Areas.Chat.Models.LiveTable.ModeType.RECOMMEND || userSession.liveTable.CurrentMode == Areas.Chat.Models.LiveTable.ModeType.DETAIL) { if (userSession.liveTable.TalkingAbout != null) { List <string> ps = new List <string>(); for (int i = 0; i < info.items.Count; i++) { ps.Add(info.items[i].name); } userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.DETAIL; string answer = Answering.Statement(userSession.liveTable.TalkingAbout, ps); if (input.IndexOf("有") != -1) { answer = answer.Replace("是", "有"); } return(answer); } } else { // 查询数据库,返回手机列表list List <Models.CellPhone> list = Inquiry.Screening(userSession.liveTable.Parameter, info); userSession.liveTable.Candidates = list; if (list.Count > 3) { /* 如果筛选结果大于三款(且重要属性只有4个以下被确认),则进行主动引导 * 首先要先确定要引导的参数 * 然后还要记录当前状态变成主动引导 * 以及记录相关参数到live-table的RelativeParameter * */ string answear = "符合您要求的手机有以下几款:"; for (int i = 0; i < 2; ++i) { answear = answear + list[i].Brand + list[i].Model + "、"; } answear = answear + list[2].Brand + list[2].Model + "等。"; int recordedImportantParameter = 0; //如果重要属性已有4个以上被确认,直接进行推荐,不继续引导 foreach (string s in Code.GlobalHash.ImportantParameter) { if (recordedImportantParameter >= 4) { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; userSession.liveTable.TalkingAbout = list[0]; return(Answering.Recommand(list[0])); } if (!userSession.liveTable.Parameter.ContainsKey(s)) { userSession.liveTable.RelatedParameter = s; userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.ACTIVE; return(answear + Answering.ActiveLeading(s)); } else { recordedImportantParameter++; } } } else if (list.Count <= 0) { return(Answering.NotFound(userSession, info)); // 状态CurrentMode在该方法中变更 } else { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; userSession.liveTable.TalkingAbout = list[0]; return(Answering.Recommand(list[0])); } } } return(""); }
/// <summary> /// 特指问+值已定 /// </summary> /// <param name="userSession"></param> /// <param name="input">有什么三星手机是2000块以下的?</param> /// <param name="info">品牌:三星;价钱:[0,2000],块</param> /// <returns></returns> public string Process(Areas.Chat.Models.UserSession userSession, string input, Extracting.ValidInfo info) { /* 分析: * 当手机型号被显式提出的情况一般在这里不会出现 *--值已定 * --明确提出主语 * --没提出主语 * --有当前讨论的手机 * --没有当前讨论的手机 * --有候选手机 * --没有候选手机 * */ //用户希望系统根据当前句的语义和之前的收集的信息立即推荐一款手机 eg:能给我推荐一款三星手机吗? if (Regex.IsMatch(input, @"推荐")) { userSession.liveTable.Candidates = Inquiry.Screening(userSession.liveTable.Parameter, info); if (userSession.liveTable.Candidates.Count == 0) { return(Answering.NotFound(userSession, info)); } else { // 目前能对多个值已定的属性进行手机推荐 userSession.liveTable.TalkingAbout = userSession.liveTable.Candidates[0]; userSession.liveTable.Candidates.Remove(userSession.liveTable.TalkingAbout); userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; return(userSession.liveTable.TalkingAbout.Brand + userSession.liveTable.TalkingAbout.Model + "这款不错,你可以了解一下。"); } } bool isDirect = false; List <Models.CellPhone> list = new List <Models.CellPhone>(); foreach (var it in info.items) { if (String.IsNullOrEmpty(info.conflictFeedback) && it.name.Equals("型号")) { if (it.items.Count > 0) { isDirect = true; } break; } } if (!isDirect) { if (userSession.liveTable.Candidates.Count > 0) { list = Inquiry.MiniScreening(userSession.liveTable.Candidates, userSession.liveTable.Parameter, info); userSession.liveTable.Candidates = list; } if (list.Count == 0) { // 查询数据库,返回手机列表list //####################这里的候选手机列表替换方式不是很妥当, 需要改 list = Inquiry.Screening(userSession.liveTable.Parameter, info); userSession.liveTable.Candidates = list; } if (list.Count > 3) { /* 如果筛选结果大于三款(且重要属性只有4个以下被确认),则进行主动引导 * 首先要先确定要引导的参数 * 然后还要记录当前状态变成主动引导 * 以及记录相关参数到live-table的RelativeParameter * */ //string answear = "符合您要求的手机有以下几款:"; //for (int i = 0; i < 2; ++i) //{ // answear = answear + list[i].Model + "、"; //} //answear = answear + list[2].Model + "等。"; int recordedImportantParameter = 0; //如果重要属性已有4个以上被确认,直接进行推荐,不继续引导 foreach (string s in Code.GlobalHash.ImportantParameter) { if (recordedImportantParameter >= 4) { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; userSession.liveTable.TalkingAbout = list[0]; return(Answering.Recommand(list[0])); } if (!userSession.liveTable.Parameter.ContainsKey(s)) { userSession.liveTable.RelatedParameter = s; userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.ACTIVE; return(Answering.ActiveLeading(s)); } else { recordedImportantParameter++; } } } else if (list.Count <= 0) { return(Answering.NotFound(userSession, info)); // 状态CurrentMode在该方法中变更 } else { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; userSession.liveTable.TalkingAbout = list[0]; return(Answering.Recommand(list[0])); } } else { string answer = ""; List <string> ps = new List <string>(); list = Inquiry.Screening(userSession.liveTable.Parameter, info); if (list.Count <= 0) { if (info.items.Count < 2) { answer = "亲,不好意思," + "没有" + info.items[0].items[0].rawData; return(answer); } else { answer = "亲,不好意思," + "我们还没收录您提到的这个型号,我们正在努力更新数据~~"; return(answer); } } else { for (int i = 0; i < info.items.Count; i++) { ps.Add(info.items[i].name); } return(Answering.Statement(list[0], ps)); } } return(""); }
public string Process(Areas.Chat.Models.UserSession userSession, string input, Code.Extracting.ValidInfo info) { /* 分析: * 对于有参数的陈述句,其实有很多种情况: * 1、我要三星的手机 * 2、这款Lumia520不错 * 3、我觉得诺基亚比HTC好 * 4、。。。 * 不过我们从简处理,根据状态来进行处理 * 如果当前状态是:主动引导,筛选请求 * 则对于普通参数信息,比如2000块以下,大屏幕,直接更新到live-table * 对于比较类型参数信息,比如屏幕大一点,需要分析是普通意义上的大一点还是跟原来的比较 * 然后就根据live-table在数据库里筛选手机 * 接着根据筛选到的手机,如果大于3款,则进行主动提问,如果小于一款,则回答没有相应的手机,高级一点的话可以推荐一款最符合的 * 如果1-3款,则给出筛选到的手机 * 而如果当前状态为推荐、理解回答等的话,则另外处理 * */ if (userSession.liveTable.CurrentMode != Areas.Chat.Models.LiveTable.ModeType.DEBUGGING && userSession.liveTable.CurrentMode != Areas.Chat.Models.LiveTable.ModeType.LEARNING && userSession.liveTable.CurrentMode != Areas.Chat.Models.LiveTable.ModeType.START && userSession.liveTable.CurrentMode != Areas.Chat.Models.LiveTable.ModeType.END) { // 查询数据库,返回手机列表list List <Models.CellPhone> list = Inquiry.Screening(userSession.liveTable.Parameter, info); userSession.liveTable.Candidates = list; if (Regex.IsMatch(input, @"(不.*要)|(不要.*)")) { bool negFigureInfoFound = false; foreach (var di in info.items) { if (di.name == "价格" || di.name == "主屏尺寸") { foreach (var dii in di.items) { if (dii.isNegative) { negFigureInfoFound = true; } } } } //当用户表达抛弃属性时,将属性抛弃 if (!negFigureInfoFound && info.items.Count > 0 && info.items[0].items.Count > 0) { userSession.liveTable.Parameter[info.items[0].name] = null; return("好的,请问您还有什么要求?"); } } else if (Regex.IsMatch(input, @"推荐")) { //用户希望系统根据当前句的语义和之前的收集的信息立即推荐一款手机 if (!info.isInfoExist) { if (userSession.liveTable.Candidates.Count == 0) { Answering.NotFound(userSession, info); // 状态CurrentMode在该方法中变更 } else { userSession.liveTable.TalkingAbout = userSession.liveTable.Candidates[0]; userSession.liveTable.Candidates.Remove(userSession.liveTable.TalkingAbout); userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; return(userSession.liveTable.TalkingAbout.Brand + userSession.liveTable.TalkingAbout.Model + "这款不错,你可以了解一下。"); } } else { if (info.AttributeValue) { userSession.liveTable.TalkingAbout = userSession.liveTable.Candidates[0]; userSession.liveTable.Candidates.Remove(userSession.liveTable.TalkingAbout); userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; return(userSession.liveTable.TalkingAbout.Brand + userSession.liveTable.TalkingAbout.Model + "这款不错,你可以了解一下。"); } else { // 目前推荐属性只支持单个属性的推荐 Models.CellPhone phone = userSession.liveTable.Candidates[0]; string retValue = ""; Type type = phone.GetType(); PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in props) { string value1 = Convert.ToString(pi.GetValue(phone, null)); string name = pi.Name; if (GlobalHash.SemanticToCN[name].Equals(info.items[0].name)) { retValue = value1; break; } } if (Regex.IsMatch(retValue, @"^\d+$") && !info.items[0].name.Equals("型号")) { List <Code.Extracting.InfoUnit> temp = new List <Extracting.InfoUnit>(); temp.Add(new Code.Extracting.InfoUnit(retValue, string.Format("{0},{1}", Convert.ToDouble(retValue) * 0.99, Convert.ToDouble(retValue) * 1.01))); Code.Extracting.InfoBlock oneblock = new Extracting.InfoBlock(info.items[0].name, "or", temp); userSession.liveTable.Parameter[info.items[0].name] = oneblock; } else { if (retValue.IndexOf("|") >= 0) { retValue = retValue.Split(new char[] { '|' })[0]; } List <Code.Extracting.InfoUnit> temp = new List <Extracting.InfoUnit>(); temp.Add(new Code.Extracting.InfoUnit(retValue, retValue)); Code.Extracting.InfoBlock oneblock = new Extracting.InfoBlock(info.items[0].name, "or", temp); userSession.liveTable.Parameter[info.items[0].name] = oneblock; } return(retValue + "是个不错的选择。"); } } } else { // 用户直接用陈述词询问手机信息 如:价钱 这时如果有当前讨论手机机器人应该回答该手机的价钱 if (userSession.liveTable.TalkingAbout != null) { if (info.items[0].items.Count == 0) { Type type = userSession.liveTable.TalkingAbout.GetType(); PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo pi in props) { object value1 = pi.GetValue(userSession.liveTable.TalkingAbout, null); string name = pi.Name; if (info.items[0].name.Equals(Code.GlobalHash.SemanticToCN[name])) { return(Convert.ToString(value1)); } } } } } if (list.Count > 3) { /* 如果筛选结果大于三款(且重要属性只有4个以下被确认),则进行主动引导 * 首先要先确定要引导的参数 * 然后还要记录当前状态变成主动引导 * 以及记录相关参数到live-table的RelativeParameter * */ int recordedImportantParameter = 0; //如果重要属性已有4个以上被确认,直接进行推荐,不继续引导 foreach (string s in Code.GlobalHash.ImportantParameter) { if (recordedImportantParameter >= 4) { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; userSession.liveTable.TalkingAbout = list[0]; return(Answering.Recommand(list[0])); } if (!userSession.liveTable.Parameter.ContainsKey(s)) { userSession.liveTable.RelatedParameter = s; userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.ACTIVE; return(Answering.ActiveLeading(s)); } else { recordedImportantParameter++; } } } else if (list.Count <= 0) { return(Answering.NotFound(userSession, info)); // 状态CurrentMode在该方法中变更 } else { userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.RECOMMEND; userSession.liveTable.TalkingAbout = list[0]; return(Answering.Recommand(list[0])); } /* 这里会有什么情况呢? * 可能用户会说:我想了解一下它的优点 * 这可以从TalkingAbout找相应的描述返回 * 当然也可能还会说:我想要800万像素的 * 妈呀,这是重新筛选的节奏吗? * 好吧,那勉强理解为用户是问:这款是800万像素的吗? * 因为如果不是,则加上这个条件在数据库里也找不到,干脆就直接判断算了 * 其实不完全是这样的,因为候选机中可能有些满足要求,好吧,那就判断一下所有候选机吧=.= * * 感觉要区分这两种句型就很麻烦了,特判有意思吗? * 不过总比返回空串好,于是这里估计要写一堆了。。。 * */ if (Regex.IsMatch(input, "了解一下|介绍一下|说一下|介绍下|说下|了解下")) { if (userSession.liveTable.TalkingAbout != null) { List <string> ps = new List <string>(); for (int i = 0; i < info.items.Count; i++) { ps.Add(info.items[i].name); } userSession.liveTable.CurrentMode = Areas.Chat.Models.LiveTable.ModeType.DETAIL; return(Answering.Statement(userSession.liveTable.TalkingAbout, ps)); } } // 第二种情况略复杂暂时不处理了 } return(""); }