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.Action; import com.qiwu.chatbotbackend.entity.Solution; import com.qiwu.chatbotbackend.entity.UserInfo; import com.qiwu.chatbotbackend.task.utils.GameBotUtil; import org.apache.http.message.BasicNameValuePair; import org.springframework.beans.factory.annotation.Value; import java.io.UnsupportedEncodingException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class FormsPoster { LogWriter logWriter; public static String TALKER = "【(.*?)@.*?】"; @Value("${qa.url}") public String QA_URL; public void setQaUrl(String qaUrl) { QA_URL = qaUrl; } public FormsPoster(LogWriter logWriter) { this.logWriter = logWriter; } HashSet skipSet = new HashSet() { { add("activeList"); add("currentAction"); add("lastUpdateTime"); add("lastReply"); } }; /** * 提交类型为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))); } } } pairList.add(new BasicNameValuePair("reply", ans.getAnswer())); logWriter.record("提交表单的数据:
" + pairList.toString(), 2); JSONObject jobj = SimpleUtil.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 ansAction * @param URL * @throws UnsupportedEncodingException */ public void postTerm(Map curInfoPoint, Map historyInfoPoint, Solution ans, Action ansAction, 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 = ansAction.getCurrentPremise().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 = ansAction.getHistoryPremise().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 = SimpleUtil.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 postGameQA1(String gameStep, 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", gameStep)); pairList.add(new BasicNameValuePair("query_text", curInfoPoint.get("query_text"))); pairList.add(new BasicNameValuePair("qa_threshold", threshold)); //设置初始值 historyInfoPoint.put("common_answer", ""); historyInfoPoint.put("qa_score", "0"); historyInfoPoint.put("qa_status", "OK"); logWriter.record("提交表单的数据:
" + pairList.toString(), 2); JSONObject jobj = SimpleUtil.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")){ 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 ((GameBotUtil.sameLocation((String)textscore.get(0), curInfoPoint, historyInfoPoint, gameEnv, historyInfoPoint.get("site"), logWriter) == 1)) { historyInfoPoint.put("common_answer", (String)textscore.get(0)); historyInfoPoint.put("qa_score", String.valueOf(textscore.get(1))); break; } } } } } }else { historyInfoPoint.put("error_message", jobj.getString("error_message")); } } public void postGameQA(Map curInfoPoint, Map historyInfoPoint, Solution ans,GameEnv gameEnv, WorldController worldController, String URL, UserInfo userInfo) throws UnsupportedEncodingException { postGameQA1(historyInfoPoint.get("game_step"), curInfoPoint, historyInfoPoint,ans, gameEnv, worldController, URL, userInfo); if(historyInfoPoint.get("common_answer") != null && (historyInfoPoint.get("common_answer").equals("") || historyInfoPoint.get("common_answer").equals("no answer"))){ postGameQA1(curInfoPoint.get("bot_name"), curInfoPoint, historyInfoPoint, ans, gameEnv, worldController, URL, userInfo); } } }