package com.qiwu.chatbotbackend.processing; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.qiwu.chatbotbackend.GameEnvironment.GameEnv; import com.qiwu.chatbotbackend.entity.Condition; import com.qiwu.chatbotbackend.entity.Solution; import org.apache.http.message.BasicNameValuePair; import java.io.UnsupportedEncodingException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class FormsPoster { LogWriter logWriter = null; public FormsPoster(LogWriter logWriter) { this.logWriter = logWriter; } HashSet skipSet = new HashSet() { { add("activeList"); add("currentAction"); add("lastUpdateTime"); add("lastReply"); } }; static String TALKER = "^【(.*?)】"; /** * 提交类型为all时,会把当前信息点和历史信息点都作为post到另一个服务器 * @param curInfoPoint * @param historyInfoPoint * @param ans * @param URL * @throws UnsupportedEncodingException */ public void postAll(Map curInfoPoint, Map historyInfoPoint, Solution ans, String URL) throws UnsupportedEncodingException { String key, keyName; Iterator k_iter = null; List pairList = new ArrayList(); k_iter = curInfoPoint.keySet().iterator(); while (k_iter.hasNext()) { key = k_iter.next(); if (curInfoPoint.get(key) != "") //infoMap.put(key + "_cur", curInfoPoint.get(key)[0]); pairList.add(new BasicNameValuePair(key, curInfoPoint.get(key))); } k_iter = historyInfoPoint.keySet().iterator(); while (k_iter.hasNext()) { key = k_iter.next(); if (historyInfoPoint.get(key) != "") //infoMap.put(key + "_his", historyInfoPoint.get(key)); if (key.equals("game_step") || key.equals("qa_threshold")) pairList.add(new BasicNameValuePair(key, historyInfoPoint.get(key))); else if(skipSet.contains(key)){} else pairList.add(new BasicNameValuePair(key + "_his", historyInfoPoint.get(key))); } logWriter.record("提交表单的数据:
" + pairList.toString(), 2); JSONObject jobj = MyFunc.postFORM(URL, pairList); logWriter.record("提交表单接收到的数据:
" + jobj.toJSONString(), 2); if((Integer)jobj.get("post_status") == 1){ Iterator kit = jobj.keySet().iterator(); while (kit.hasNext()) { keyName = kit.next(); if (keyName.equals("process")) { ans.setNext((String) jobj.get(keyName)); historyInfoPoint.put("process", (String) jobj.get(keyName)); } else if (keyName.equals("solution")) { ans.setAnswer((String) jobj.get(keyName)); } else if (keyName.equals("status") || keyName.equals("qa_candidates")){ continue; } else{ historyInfoPoint.put(keyName, jobj.getString(keyName)); } } }else { if(URL.equals("http://simqa.sz.chewrobot.com:28081/game")) ans.setAnswer("qa_threshold, game_step, query_text 看看少了哪个"); else ans.setAnswer("提交表单出现异常"); } } /** * 提交类型为term时只会提交最佳匹配的配置条目中包含的条件和值 * @param curInfoPoint * @param historyInfoPoint * @param ans * @param ansCondition * @param URL * @throws UnsupportedEncodingException */ public void postTerm(Map curInfoPoint, Map historyInfoPoint, Solution ans, Condition ansCondition, String URL) throws UnsupportedEncodingException { String key, keyName; Iterator k_iter = null; List pairList = new ArrayList(); pairList.add(new BasicNameValuePair("chat_key", curInfoPoint.get("chat_key"))); pairList.add(new BasicNameValuePair("action_name", curInfoPoint.get("action_name"))); pairList.add(new BasicNameValuePair("bot_name", curInfoPoint.get("bot_name"))); k_iter = ansCondition.currentPremise.keySet().iterator(); while (k_iter.hasNext()) { key = k_iter.next().split("::")[0]; if (curInfoPoint.get(key) != "") pairList.add(new BasicNameValuePair(key, curInfoPoint.get(key))); } k_iter = ansCondition.historyPremise.keySet().iterator(); while (k_iter.hasNext()) { key = k_iter.next().split("::")[0]; if (historyInfoPoint.get(key) != "") pairList.add(new BasicNameValuePair(key + "_his", historyInfoPoint.get(key))); //infoMap.put(key + "_his", historyInfoPoint.get(key)); } logWriter.record("提交表单的数据:
" + pairList.toString(), 2); JSONObject jobj = MyFunc.postFORM(URL, pairList); logWriter.record("提交表单接收到的数据:
" + jobj.toJSONString(), 2); if((Integer)jobj.get("post_status") == 1){ Iterator kit = jobj.keySet().iterator(); while (kit.hasNext()) { keyName = kit.next(); if (keyName.equals("process")) { ans.setNext((String) jobj.get(keyName)); historyInfoPoint.put("process", (String) jobj.get(keyName)); } else if (keyName.equals("solution")) { ans.setAnswer((String) jobj.get(keyName)); } else if (keyName.equals("status")){ continue; } else{ historyInfoPoint.put(keyName, jobj.getString(keyName)); } } }else { ans.setAnswer("提交表单出现异常"); } } /** * 游戏专用提交表单 * @param curInfoPoint * @param historyInfoPoint * @param ans * @param gameEnv * @param worldController * @param URL * @throws UnsupportedEncodingException */ public void postGameQA(Map curInfoPoint, Map historyInfoPoint, Solution ans,GameEnv gameEnv, WorldController worldController, String URL) throws UnsupportedEncodingException { String key, keyName; Iterator k_iter = null; List pairList = new ArrayList(); String threshold = "50"; if (historyInfoPoint.get("qa_threshold") != null) threshold = historyInfoPoint.get("qa_threshold"); pairList.add(new BasicNameValuePair("game_step", historyInfoPoint.get("game_step"))); pairList.add(new BasicNameValuePair("query_text", curInfoPoint.get("query_text"))); pairList.add(new BasicNameValuePair("qa_threshold", threshold)); logWriter.record("提交表单的数据:
" + pairList.toString(), 2); JSONObject jobj = MyFunc.postFORM(URL, pairList); logWriter.record("提交表单接收到的数据:
" + jobj.toJSONString(), 2); String text,score, site = historyInfoPoint.get("site"), npc; if((Integer)jobj.get("post_status") == 1){ if(jobj.getJSONArray("qa_candidates") != null && jobj.get("qa_status").equals("OK")){ historyInfoPoint.put("common_answer", "未找到匹配"); historyInfoPoint.put("qa_score", "0"); List candidates = JSON.toJavaObject(jobj.getJSONArray("qa_candidates"), List.class); for(JSONArray pair:candidates){ List textscore = JSON.toJavaObject(pair, List.class); Pattern findTalker = Pattern.compile(TALKER); Matcher m = findTalker.matcher(textscore.get(0)); if(m.find()) { npc = m.group(1); if (worldController.worldOperation(gameEnv, "NPC_query", "queryLocationByNPCName:" + npc, curInfoPoint, historyInfoPoint).equals(site)){ historyInfoPoint.put("common_answer", textscore.get(0)); historyInfoPoint.put("qa_score", textscore.get(1)); break; } } } }else{ historyInfoPoint.put("common_answer", (String)jobj.get("common_answer")); historyInfoPoint.put("qa_score", "0"); historyInfoPoint.put("qa_status", (String)jobj.get("qa_status")); } }else { ans.setAnswer("提交表单出现异常"); } } }