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 com.qiwu.chatbotbackend.entity.UserInfo; 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 static String QA_URL = "http://qcloud-gz1.chewrobot.com:58000/game"; 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) != null && !curInfoPoint.get(key).equals("")) //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) != null && !historyInfoPoint.get(key).equals("")) { if (skipSet.contains(key)) continue; if (curInfoPoint.get(key) == null || curInfoPoint.equals("")) { pairList.add(new BasicNameValuePair(key, historyInfoPoint.get(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(QA_URL)) 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) != null && !curInfoPoint.equals("")) 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).equals("")) { if (curInfoPoint.get(key) == null || curInfoPoint.equals("")) { pairList.add(new BasicNameValuePair(key, historyInfoPoint.get(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")){ 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, UserInfo userInfo) 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; System.out.println(jobj.toJSONString()); 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"); historyInfoPoint.put("qa_status", "OK"); 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); if((Integer)textscore.get(1) > 90) { Matcher m = findTalker.matcher((String)textscore.get(0)); if (m.find()) { npc = m.group(1); if ((ResponseFilter.sameLocation((String)textscore.get(0), curInfoPoint, historyInfoPoint, gameEnv, worldController, historyInfoPoint.get("site"), logWriter) == 1)) { historyInfoPoint.put("common_answer", (String)textscore.get(0)); historyInfoPoint.put("qa_score", String.valueOf(textscore.get(1))); break; } } else { logWriter.record(textscore + "没找到npc名字", 1); historyInfoPoint.put("common_answer", ResponseFilter.appendTalker((String)textscore.get(0), curInfoPoint, historyInfoPoint, gameEnv, worldController)); historyInfoPoint.put("qa_score", String.valueOf(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("提交表单出现异常"); } } }