package com.qiwu.chatbotbackend.task.utils; import com.qiwu.chatbotbackend.entity.Action; import com.qiwu.chatbotbackend.entity.ChatCache; import com.qiwu.chatbotbackend.entity.Solution; import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.processing.LogWriter; import javax.servlet.ServletContext; import java.io.IOException; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class BasicBotUtil { Map curInfoPoint = null; UserInfo userinfo = null; LogWriter logWriter = null; ChatCache chatCache = null; public String botType = "basic"; public ChatCache getChatCache() { return chatCache; } public BasicBotUtil(){}; public BasicBotUtil(Map curInfoPoint, ChatCache chatCache, UserInfo userinfo, LogWriter logWriter){ this.curInfoPoint = curInfoPoint; this.userinfo = userinfo; this.logWriter = logWriter; this.chatCache = chatCache; } public boolean preprocessing(Map receiveParameters, ServletContext context) throws IOException { return infoPointPreprocessing(receiveParameters, context, true); } //在接收到请求后对数据做处理,转化为系统可以处理的数据 /** * * @param receiveParameters * @param context * @param setUnknown 如果什么都没识别到就将上一个激活的condition设为unknown * @return * @throws IOException */ public boolean infoPointPreprocessing(Map receiveParameters, ServletContext context, Boolean setUnknown) throws IOException { String return_str = "", k, botType = ""; String receiveParameterStr = ""; boolean debugFlag = false; HashSet skipSet = new HashSet() { { add("chat_key"); add("randKey"); add("userKey"); add("action_name"); add("bot_name"); add("emptySet"); add("query_text"); } }; //输出所有actionName的值 Iterator iit = receiveParameters.keySet().iterator(); receiveParameterStr+= "-----------------------------------------识别数据列表-------------------------------------------
"; while (iit.hasNext()) { k = iit.next(); // receiveParameterStr += k + ": "; curInfoPoint.put(k, receiveParameters.get(k)[0]); if (k.equals("debug_mode") || k.equals("debug_rule")) debugFlag = true; String values[] = receiveParameters.get(k); receiveParameterStr += values[0] + " " + "
"; if (values[0].equals(":")) { curInfoPoint.put(k, ""); } } logWriter.record(receiveParameterStr, 0); System.out.println(receiveParameterStr.toString()); //如果引擎什么信息都没识别到,那就将上一轮的nextStep(也就是当前轮的actionName)设为unknown iit = curInfoPoint.keySet().iterator(); boolean unknown_flag = setUnknown; while (iit.hasNext()) { k = iit.next(); String values = curInfoPoint.get(k); if (skipSet.contains(k)) continue; if (!values.equals("")) { unknown_flag = false; break; } } String currentAction = ""; if (chatCache.getInfoPoint().containsKey("currentAction")) currentAction = chatCache.getInfoPoint().get("currentAction"); else if (userinfo.extraInfo.containsKey("activeAction")) { currentAction = userinfo.extraInfo.get("activeAction"); chatCache.getInfoPoint().put("currentAction", currentAction); } else currentAction = " "; curInfoPoint.put("emptySet", " "); if (chatCache.getInfoPoint().get("activeList") == null) { chatCache.getInfoPoint().put("activeList", currentAction); } if (unknown_flag && !currentAction.equals("")) { String i = "unknown"; curInfoPoint.put(currentAction, i); } logWriter.record("historyInfoPoint:" + chatCache.getInfoPoint().toString(), 1); return debugFlag; } public boolean postprocessing(String chatKey, ServletContext context, Solution bestMatch){ if(chatCache.getInfoPoint().get("error_message") != null && chatCache.getInfoPoint().get("error_message") != ""){ bestMatch.setAnswer(bestMatch.getAnswer() + chatCache.getInfoPoint().get("error_message")); } return true; } /** * 从当前信息点或者已知信息点中提取关键词 * @param infoPoint * @param infoPoint2 * @param chooseLater * @param actionName * @return */ public String getKeyword(Map infoPoint, Map infoPoint2, Boolean chooseLater, String actionName){ String keyword = ""; String nameSeries[] = actionName.split("_"); /** * 如果chooseLater为真,则先提取infoPint2中的关键字,如果infoPoint2中不存在,再提取infoPoint中的关键字 */ if(chooseLater){ keyword = infoPoint2.get(actionName); if(keyword != null && !keyword.equals("")) return keyword; } keyword = infoPoint.get(actionName); return keyword; }; /** * 更新历史信息点中的某一个键值对 * @param variableName * @param updateValue */ public void putKeyword(String variableName, String updateValue){ chatCache.getInfoPoint().put(variableName, updateValue); } public String knowledgeProcess(String variableName, String updateValue){ return updateValue; } /** * 额外的更新操作 */ public void extraOperation(Action ansAction, Solution solution) throws IOException { } //将文本中存在的变量槽位{npc_xxx}替换成变量值 public String varSlotReplace(String text){ String VARIABLE_REPLACE_PATTERN = "(\\{.*?\\-[12]\\})"; Pattern replacePattern = Pattern.compile(VARIABLE_REPLACE_PATTERN); //用正则表达式将回答中的变量模板{conditionName-2}替换成后台存储的变量值 String keyword, variable,reply = text; Matcher m = replacePattern.matcher(reply); while (m.find()) { variable = m.group(1).substring(1, m.group(1).length() - 3); if (m.group(1).endsWith("-1}")) { if (curInfoPoint.get(variable) != null && !curInfoPoint.get(variable).equals("")) { keyword = curInfoPoint.get(variable); if(keyword.contains(":")) keyword = keyword.split(":")[1]; reply = reply.replace("{" + variable + "-1}", keyword); } } else if (m.group(1).endsWith("-2}")) { if (chatCache.getInfoPoint().get(variable) != null) if(chatCache.getInfoPoint().get(variable).equals("do_reset")){ reply = reply.replace("{" + variable + "-2}", ""); }else reply = reply.replace("{" + variable + "-2}", chatCache.getInfoPoint().get(variable)); } } return reply; } }