From f4ead64177acbf396e4cd859557fcc2d0d2d1bc4 Mon Sep 17 00:00:00 2001 From: zhaoguojun Date: Fri, 19 Apr 2019 17:40:28 +0800 Subject: [PATCH] 日志修改 --- src/main/java/com/qiwu/chatbotbackend/processing/ActionMatcher.java | 25 +++++++++++++++++++++---- src/main/java/com/qiwu/chatbotbackend/task/utils/BasicBotUtil.java | 466 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 files changed, 261 insertions(+), 230 deletions(-) diff --git a/src/main/java/com/qiwu/chatbotbackend/processing/ActionMatcher.java b/src/main/java/com/qiwu/chatbotbackend/processing/ActionMatcher.java index 0c337be..766c39d 100644 --- a/src/main/java/com/qiwu/chatbotbackend/processing/ActionMatcher.java +++ b/src/main/java/com/qiwu/chatbotbackend/processing/ActionMatcher.java @@ -88,7 +88,9 @@ public class ActionMatcher { if (action.getStatus().equals("waiting")){ logWriter.record("-----------------------------------检索到配置条目", 0); - logWriter.record(userinfo.id2config.get(action.getId()), 1); + //logWriter.record(userinfo.id2config.get(action.getId()), 1); + Action action2 = userinfo.id2condition.get(action.getId()); + logWriter.record(action2.getConfigName() + " - " + action2.getId(), 1); //是否有激活操作,如果有激活操作且没有提交表单则判断是否满足条件,如果有激活有提交表单,则放在整个操作完成(getSolution)之后再激活 if (action.getFeatureMap().get("activate") != null && !action.getFeatureMap().containsKey("post")) { @@ -163,8 +165,9 @@ public class ActionMatcher { logWriter.record("-----------------------------------检索到配置条目", 0); - logWriter.record(userinfo.id2config.get(action.getId()), 1); - + //logWriter.record(userinfo.id2config.get(action.getId()), 1); + Action action2 = userinfo.id2condition.get(action.getId()); + logWriter.record(action2.getConfigName() + " - " + action2.getId(), 1); if(higherPriority(action, bestMatch)) replaceFlag = true; @@ -678,7 +681,21 @@ public class ActionMatcher { historyInfoPoint.put("currentAction", next); historyInfoPoint.put("activeList", historyInfoPoint.get("activeList") + "|" + next); - logWriter.record("处理完成后的信息点:
" + historyInfoPoint.toString(), 1); + // logWriter.record("处理完成后的信息点:
" + historyInfoPoint.toString(), 1); + StringBuilder sb = new StringBuilder("处理完成后的信息点:
"); + for(Map.Entry entry : historyInfoPoint.entrySet()) { + if(entry.getValue()==null || entry.getValue().equals("")) + continue; + switch (entry.getKey()) { + case "unknownText": + case "unknownTextList": + case "finalUnknownText": + break; + default: + sb.append(entry.getKey() + ":" + entry.getValue() + ", "); + } + } + logWriter.record(sb.toString(), 1); HashSet skipSet = new HashSet() { { diff --git a/src/main/java/com/qiwu/chatbotbackend/task/utils/BasicBotUtil.java b/src/main/java/com/qiwu/chatbotbackend/task/utils/BasicBotUtil.java index 0578adc..d0e097a 100644 --- a/src/main/java/com/qiwu/chatbotbackend/task/utils/BasicBotUtil.java +++ b/src/main/java/com/qiwu/chatbotbackend/task/utils/BasicBotUtil.java @@ -1,226 +1,240 @@ -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; - } -} +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); + Map _info = chatCache.getInfoPoint(); + StringBuilder sb = new StringBuilder("historyInfoPoint:"); + for(Map.Entry entry : _info.entrySet()) { + if(entry.getValue()==null || entry.getValue().equals("")) + continue; + switch (entry.getKey()) { + case "unknownText": + case "unknownTextList": + case "finalUnknownText": + break; + default: + sb.append(entry.getKey() + ":" + entry.getValue() + ", "); + } + } + logWriter.record(sb.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; + } +} -- libgit2 0.27.1