Commit 6321ac3c by hxw2303632

Merge branch 'master' of 192.168.1.81:hxw2303632/webapp

parents 947af96a f79bd39c
......@@ -846,18 +846,18 @@ public class ActionUtils {
conditionExtraKeywords.putIfAbsent(DEFAULT_CONDITION + "_head", new HashSet<String>());
conditionExtraKeywords.putIfAbsent(DEFAULT_CONDITION + "_tail", new HashSet<String>());
for(int i = 1;i < 10;i++){
for(int i = 1;i < 100;i++){
//给condition的名字添加后缀,因为游戏中可能用到condition的复制,但值域和名字不一样
if(i == 1)
postfix = "";
else
postfix = String.valueOf(i);
postfix = "_" + String.valueOf(i);
activeFlag = "0";
conditionJson = (JSONObject) patternConditionJson.clone();
conditionName = conditionJson.getString("conditionName") + postfix;
conditionName2 = conditionName + "_independent";
conditionName2 = conditionName + "_indep";
if(!conditionExtraKeywords.containsKey(conditionName + "_tail")){ //如果没有用到这个condition,就跳过
if(!conditionExtraKeywords.containsKey(conditionName2 + "_tail"))
......
package com.qiwu.chatbotbackend.actiongenerate.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.qiwu.chatbotbackend.actiongenerate.entity.ConfigTerm;
import com.qiwu.chatbotbackend.actiongenerate.entity.IntervalStatus;
import com.qiwu.chatbotbackend.db.SqlHelper;
import com.qiwu.chatbotbackend.entity.ConfigTermText;
import com.qiwu.chatbotbackend.entity.Premise;
import com.qiwu.chatbotbackend.entity.UserInfo;
import com.qiwu.chatbotbackend.processing.UserInfoController;
import javax.servlet.ServletContext;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import static com.qiwu.chatbotbackend.Config.Config.CONFIG_INSERT_ERROR;
public class ConfigTermUtils {
SqlHelper helper = null;
String botAccount = "";
ServletContext context = null;
UserInfo userInfo = null;
public IntervalStatus intervalStatus = new IntervalStatus(1, "");
public ConfigTermUtils(String botAccount, SqlHelper helper, ServletContext context) throws IOException, SQLException {
this.helper = helper;
this.context = context;
this.botAccount = botAccount;
UserInfoController uiController = new UserInfoController();
this.userInfo = uiController.userInfoQuery(botAccount, context);
}
public ConfigTermUtils(SqlHelper helper){
this.helper = helper;
}
public void insertConfigTerm(JSONObject parameters) throws IOException {
Boolean insertFlag = true;
List<JSONObject> configList = new ArrayList<JSONObject>();
List<Integer> idList = new ArrayList<Integer>();
int botId = parameters.getInteger("botId");
int categoryId = parameters.getInteger("categoryId");
//处理特殊字符
ConfigTermText configText = configPreprocessing(parameters.getJSONObject("configData"));
configList.add(parameters.getJSONObject("configData"));
insertFlag = helper.batchInsertConfigByBotId(botId, configList, idList, categoryId);
//generateSingleConfigText();
if(insertFlag){
UserInfoController.insertConfig(botAccount, String.valueOf(idList.get(0)), configText.toString() + idList.get(0), context);
UserInfoController.writeConfigText(botAccount, userInfo);
intervalStatus = new IntervalStatus(1,"");
}else{
intervalStatus = new IntervalStatus(0, CONFIG_INSERT_ERROR);
}
}
/**
* 数据预处理
* @param configJson
*/
public ConfigTermText configPreprocessing(JSONObject configJson){
int premiseNum = 0, knowledgeNum = 0, nextStepNum;
Premise premise = null;
String reply, auxiliaryPriority = "0", nextStepStatus, configName;
StringBuffer nextStepText = new StringBuffer(), featureText = new StringBuffer();
JSONObject premiseJson = null, knowledgeJson = null, nextStep;
ConfigTermText configTermText = null;
List<Premise> curPremiseList = new ArrayList<Premise>();
List<Premise> hisPremiseList = new ArrayList<Premise>();
Map<String, String> updateKnowledge = new HashMap<String, String>();
Map<String, String> punctReplaceMap = new LinkedHashMap<String, String>();
punctReplaceMap.put(",", ",");
punctReplaceMap.put("|", "");
punctReplaceMap.put("\r\n", "");
punctReplaceMap.put("\n", "");
punctReplaceMap.put("\t", "");
//解析当前信息的前置条件
premiseNum = configJson.getJSONArray("currentPremiseList").size();
for(int i = 0;i < premiseNum;i++){
premiseJson = configJson.getJSONArray("currentPremiseList").getJSONObject(i);
premise = new Premise(premiseJson.getString("actionName"), premiseJson.getString("cmpType"),
premiseJson.getString("operator"), premiseJson.getString("keywords"));
if(!premise.getConditionName().equals("") || !premise.getKeyword().equals("")){
premise.setKeyword(textReplace(premise.getKeyword(), punctReplaceMap));
curPremiseList.add(premise);
}
premiseJson.put("keywords", textReplace(premiseJson.getString("keywords"), punctReplaceMap));
}
//解析已知信息的前置条件
premiseNum = configJson.getJSONArray("historyPremiseList").size();
for(int i = 0;i < premiseNum;i++){
premiseJson = configJson.getJSONArray("historyPremiseList").getJSONObject(i);
premise = new Premise(premiseJson.getString("actionName"), premiseJson.getString("cmpType"),
premiseJson.getString("operator"), premiseJson.getString("keywords"));
if(!premise.getConditionName().equals("") || !premise.getKeyword().equals("")){
premise.setKeyword(textReplace(premise.getKeyword(), punctReplaceMap));
hisPremiseList.add(premise);
}
premiseJson.put("keywords", textReplace(premiseJson.getString("keywords"), punctReplaceMap));
}
//获得辅助优先级
if(configJson.get("auxiliaryPriority") != null){
auxiliaryPriority = String.valueOf(configJson.getInteger("auxiliaryPriority"));
}
//获得回答
reply = textReplace(configJson.getString("reply"), punctReplaceMap);
configJson.put("reply", textReplace(configJson.getString("reply"), punctReplaceMap));
//获得更新知识单元
if(configJson.getJSONArray("updateKnowledge") != null) {
knowledgeNum = configJson.getJSONArray("updateKnowledge").size();
for (int i = 0; i < knowledgeNum; i++) {
knowledgeJson = configJson.getJSONArray("updateKnowledge").getJSONObject(i);
updateKnowledge.put(knowledgeJson.getString("variableName"), textReplace(knowledgeJson.getString("keyword"), punctReplaceMap));
knowledgeJson.put("keyword", textReplace(knowledgeJson.getString("keyword"), punctReplaceMap));
}
}
//多个下一步激活问题的处理
if(configJson.get("nextStepList") != null){
nextStepNum = configJson.getJSONArray("nextStepList").size();
for(int i = 0;i < nextStepNum;i++){
if(i != 0)
nextStepText.append(",");
nextStep = configJson.getJSONArray("nextStepList").getJSONObject(i);
nextStepText.append(nextStep.getString("nextStep"));
if(nextStep.get("except") != null && nextStep.getJSONArray("except").size() != 0){
nextStepText.append(":").append(Joiner.on("-").join(nextStep.getJSONArray("except")));
}
}
}
if(curPremiseList.size() != 0 || hisPremiseList.size() != 0)
//match表示需要条件匹配
featureText.append("match");
//是否提交表单
if(configJson.getBoolean("doPost")){
featureText.append("post::").append(configJson.getString("postType"));
featureText.append("(").append(configJson.getString("postURL")).append(")");
}
//判断下一步是选择新的问题,还是留在原来的问题,或是返回上一个问题
if(!configJson.containsKey("nextStepStatus"))
featureText.append(",return-0");
else{
nextStepStatus = configJson.getString("nextStepStatus");
switch(nextStepStatus){
case "stay":
featureText.append(",return-0");
break;
case "back":
featureText.append(",return-1");
break;
case "return":
case "jump":
//存在多个下一步
if(configJson.get("nextStepList") != null && configJson.getJSONArray("nextStepList").size() > 1){
featureText.append(",newNext");
}
break;
case "activate":
//下一步激活,且激活不为空
if(configJson.get("nextStepList") != null && !configJson.getJSONArray("nextStepList").getJSONObject(0).getString("nextStep").equals("")){
featureText.append(",activate");
}
break;
}
}
configTermText = new ConfigTermText(curPremiseList, hisPremiseList, updateKnowledge, "", "", "", featureText.toString(), reply, nextStepText.toString(), auxiliaryPriority);
configName = configJson.getString("configName");
if(configName != null && !configName.equals("")) {
configTermText.setConfigName(configName, false);
}
return configTermText;
}
public void deleteConfig(JSONObject parameters) throws IOException {
Boolean deleteFlag = true;
int botId = parameters.getInteger("botId");
int configId = parameters.getInteger("configId");
deleteFlag = helper.deleteConfigByConfigId(botId, configId);
if(deleteFlag){
UserInfoController.deleteConfig(botAccount, String.valueOf(configId), "", context);
UserInfoController.writeConfigText(botAccount, userInfo);
}
}
public void updateConfig(JSONObject parameters) throws IOException {
Boolean updateFlag = true;
int botId = parameters.getInteger("botId");
int categoryId = parameters.getInteger("categoryId");
int configId = parameters.getInteger("configId");
ConfigTermText configText = configPreprocessing(parameters.getJSONObject("configData"));
ConfigTerm configTerm = helper.queryConfigByConfigId(botId, configId);
updateFlag = helper.updateConfig(new ConfigTerm(botId, configId, categoryId, parameters.getJSONObject("configData")));
if(!updateFlag){
if(parameters.getJSONObject("configData").toJSONString().equals(configTerm.getConfigJsonText())){
updateFlag = true;
}
}
if(updateFlag){
//更新配置条目
UserInfoController.deleteConfig(botAccount, String.valueOf(configId), "", context);
UserInfoController.insertConfig(botAccount, String.valueOf(configId), configText.toString() + configId, context);
UserInfoController.writeConfigText(botAccount, userInfo);
}
}
public void queryConfig(JSONObject parameters){
int botId = parameters.getInteger("botId");
int categoryId = parameters.getInteger("categoryId");
JSONArray configDataList = new JSONArray();
JSONObject configJson = null;
List<ConfigTerm> configTermList = helper.queryConfigByCategoryId(botId, categoryId);
for(ConfigTerm configTerm:configTermList){
configJson = JSONObject.parseObject(configTerm.getConfigJsonText());
configJson.put("configId", configTerm.getConfigId());
configJson.put("categoryId", configTerm.getCategoryId());
configDataList.add(configJson);
}
intervalStatus = new IntervalStatus(1, "", configDataList);
}
public void querySleepingConfig(JSONObject parameters){
int botId = parameters.getInteger("botId");
JSONArray sleepingConfigs = new JSONArray();
JSONObject configJson = null;
List<ConfigTerm> configTermList = helper.querySleepingConfig(botId);
for(ConfigTerm configTerm:configTermList){
sleepingConfigs.add(new JSONArray(){
{
add(configTerm.getConfigId());
add(configTerm.getConfigName());
}
});
}
intervalStatus = new IntervalStatus(1, "", sleepingConfigs);
}
/**
* 将文本中的一些特殊字符过滤掉
* @param text
* @param replaceMap
* @return
*/
public static String textReplace(String text, Map<String, String> replaceMap){
for(String key:replaceMap.keySet()){
text = text.replace(key, replaceMap.get(key));
}
return text;
}
public void generateSingleConfigText(JSONObject configJson){
}
}
package com.qiwu.chatbotbackend.actiongenerate.utils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Joiner;
import com.qiwu.chatbotbackend.actiongenerate.entity.ConfigTerm;
import com.qiwu.chatbotbackend.actiongenerate.entity.IntervalStatus;
import com.qiwu.chatbotbackend.db.SqlHelper;
import com.qiwu.chatbotbackend.entity.ConfigTermText;
import com.qiwu.chatbotbackend.entity.Premise;
import com.qiwu.chatbotbackend.entity.UserInfo;
import com.qiwu.chatbotbackend.processing.UserInfoController;
import javax.servlet.ServletContext;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import static com.qiwu.chatbotbackend.Config.Config.CONFIG_INSERT_ERROR;
public class ConfigTermUtils {
SqlHelper helper = null;
String botAccount = "";
ServletContext context = null;
UserInfo userInfo = null;
public IntervalStatus intervalStatus = new IntervalStatus(1, "");
public ConfigTermUtils(String botAccount, SqlHelper helper, ServletContext context) throws IOException, SQLException {
this.helper = helper;
this.context = context;
this.botAccount = botAccount;
UserInfoController uiController = new UserInfoController();
this.userInfo = uiController.userInfoQuery(botAccount, context);
}
public ConfigTermUtils(SqlHelper helper){
this.helper = helper;
}
public void insertConfigTerm(JSONObject parameters) throws IOException {
Boolean insertFlag = true;
List<JSONObject> configList = new ArrayList<JSONObject>();
List<Integer> idList = new ArrayList<Integer>();
int botId = parameters.getInteger("botId");
int categoryId = parameters.getInteger("categoryId");
//处理特殊字符
ConfigTermText configText = configPreprocessing(parameters.getJSONObject("configData"));
configList.add(parameters.getJSONObject("configData"));
insertFlag = helper.batchInsertConfigByBotId(botId, configList, idList, categoryId);
//generateSingleConfigText();
if(insertFlag){
UserInfoController.insertConfig(botAccount, String.valueOf(idList.get(0)), configText.toString() + idList.get(0), context);
UserInfoController.writeConfigText(botAccount, userInfo);
intervalStatus = new IntervalStatus(1,"");
}else{
intervalStatus = new IntervalStatus(0, CONFIG_INSERT_ERROR);
}
}
/**
* 数据预处理
* @param configJson
*/
public ConfigTermText configPreprocessing(JSONObject configJson){
int premiseNum = 0, knowledgeNum = 0, nextStepNum;
Premise premise = null;
String reply, auxiliaryPriority = "0", nextStepStatus, configName;
StringBuffer nextStepText = new StringBuffer(), featureText = new StringBuffer();
JSONObject premiseJson = null, knowledgeJson = null, nextStep;
ConfigTermText configTermText = null;
List<Premise> curPremiseList = new ArrayList<Premise>();
List<Premise> hisPremiseList = new ArrayList<Premise>();
Map<String, String> updateKnowledge = new HashMap<String, String>();
Map<String, String> punctReplaceMap = new LinkedHashMap<String, String>();
punctReplaceMap.put(",", ",");
punctReplaceMap.put("|", "");
punctReplaceMap.put("\r\n", "");
punctReplaceMap.put("\n", "");
punctReplaceMap.put("\t", "");
//解析当前信息的前置条件
premiseNum = configJson.getJSONArray("currentPremiseList").size();
for(int i = 0;i < premiseNum;i++){
premiseJson = configJson.getJSONArray("currentPremiseList").getJSONObject(i);
premise = new Premise(premiseJson.getString("actionName"), premiseJson.getString("cmpType"),
premiseJson.getString("operator"), premiseJson.getString("keywords"));
if(!premise.getConditionName().equals("") || !premise.getKeyword().equals("")){
premise.setKeyword(textReplace(premise.getKeyword(), punctReplaceMap));
curPremiseList.add(premise);
}
premiseJson.put("keywords", textReplace(premiseJson.getString("keywords"), punctReplaceMap));
}
//解析已知信息的前置条件
premiseNum = configJson.getJSONArray("historyPremiseList").size();
for(int i = 0;i < premiseNum;i++){
premiseJson = configJson.getJSONArray("historyPremiseList").getJSONObject(i);
premise = new Premise(premiseJson.getString("actionName"), premiseJson.getString("cmpType"),
premiseJson.getString("operator"), premiseJson.getString("keywords"));
if(!premise.getConditionName().equals("") || !premise.getKeyword().equals("")){
premise.setKeyword(textReplace(premise.getKeyword(), punctReplaceMap));
hisPremiseList.add(premise);
}
premiseJson.put("keywords", textReplace(premiseJson.getString("keywords"), punctReplaceMap));
}
//获得辅助优先级
if(configJson.get("auxiliaryPriority") != null){
auxiliaryPriority = String.valueOf(configJson.getInteger("auxiliaryPriority"));
}
//获得回答
reply = textReplace(configJson.getString("reply"), punctReplaceMap);
configJson.put("reply", textReplace(configJson.getString("reply"), punctReplaceMap));
//获得更新知识单元
if(configJson.getJSONArray("updateKnowledge") != null) {
knowledgeNum = configJson.getJSONArray("updateKnowledge").size();
for (int i = 0; i < knowledgeNum; i++) {
knowledgeJson = configJson.getJSONArray("updateKnowledge").getJSONObject(i);
updateKnowledge.put(knowledgeJson.getString("variableName"), textReplace(knowledgeJson.getString("keyword"), punctReplaceMap));
knowledgeJson.put("keyword", textReplace(knowledgeJson.getString("keyword"), punctReplaceMap));
}
}
//多个下一步激活问题的处理
if(configJson.get("nextStepList") != null){
nextStepNum = configJson.getJSONArray("nextStepList").size();
for(int i = 0;i < nextStepNum;i++){
if(i != 0)
nextStepText.append(",");
nextStep = configJson.getJSONArray("nextStepList").getJSONObject(i);
nextStepText.append(nextStep.getString("nextStep"));
if(nextStep.get("except") != null && nextStep.getJSONArray("except").size() != 0){
nextStepText.append(":").append(Joiner.on("-").join(nextStep.getJSONArray("except")));
}
}
}
if(curPremiseList.size() != 0 || hisPremiseList.size() != 0)
//match表示需要条件匹配
featureText.append("match");
//是否提交表单
if(configJson.getBoolean("doPost")){
featureText.append("post::").append(configJson.getString("postType"));
featureText.append("(").append(configJson.getString("postURL")).append(")");
}
//判断下一步是选择新的问题,还是留在原来的问题,或是返回上一个问题
if(!configJson.containsKey("nextStepStatus"))
featureText.append(",return-0");
else{
nextStepStatus = configJson.getString("nextStepStatus");
switch(nextStepStatus){
case "stay":
featureText.append(",return-0");
break;
case "back":
featureText.append(",return-1");
break;
case "return":
case "jump":
//存在多个下一步
if(configJson.get("nextStepList") != null && configJson.getJSONArray("nextStepList").size() > 1){
featureText.append(",newNext");
}
break;
case "activate":
//下一步激活,且激活不为空
if(configJson.get("nextStepList") != null && !configJson.getJSONArray("nextStepList").getJSONObject(0).getString("nextStep").equals("")){
featureText.append(",activate");
}
break;
}
}
Map<String ,Integer> gNameCount = new HashMap<String , Integer>();
configTermText = new ConfigTermText(curPremiseList, hisPremiseList, updateKnowledge, "", "", "", featureText.toString(), reply, nextStepText.toString(), auxiliaryPriority, gNameCount);
configName = configJson.getString("configName");
if(configName != null && !configName.equals("")) {
configTermText.setConfigName(configName, false);
}
return configTermText;
}
public void deleteConfig(JSONObject parameters) throws IOException {
Boolean deleteFlag = true;
int botId = parameters.getInteger("botId");
int configId = parameters.getInteger("configId");
deleteFlag = helper.deleteConfigByConfigId(botId, configId);
if(deleteFlag){
UserInfoController.deleteConfig(botAccount, String.valueOf(configId), "", context);
UserInfoController.writeConfigText(botAccount, userInfo);
}
}
public void updateConfig(JSONObject parameters) throws IOException {
Boolean updateFlag = true;
int botId = parameters.getInteger("botId");
int categoryId = parameters.getInteger("categoryId");
int configId = parameters.getInteger("configId");
ConfigTermText configText = configPreprocessing(parameters.getJSONObject("configData"));
ConfigTerm configTerm = helper.queryConfigByConfigId(botId, configId);
updateFlag = helper.updateConfig(new ConfigTerm(botId, configId, categoryId, parameters.getJSONObject("configData")));
if(!updateFlag){
if(parameters.getJSONObject("configData").toJSONString().equals(configTerm.getConfigJsonText())){
updateFlag = true;
}
}
if(updateFlag){
//更新配置条目
UserInfoController.deleteConfig(botAccount, String.valueOf(configId), "", context);
UserInfoController.insertConfig(botAccount, String.valueOf(configId), configText.toString() + configId, context);
UserInfoController.writeConfigText(botAccount, userInfo);
}
}
public void queryConfig(JSONObject parameters){
int botId = parameters.getInteger("botId");
int categoryId = parameters.getInteger("categoryId");
JSONArray configDataList = new JSONArray();
JSONObject configJson = null;
List<ConfigTerm> configTermList = helper.queryConfigByCategoryId(botId, categoryId);
for(ConfigTerm configTerm:configTermList){
configJson = JSONObject.parseObject(configTerm.getConfigJsonText());
configJson.put("configId", configTerm.getConfigId());
configJson.put("categoryId", configTerm.getCategoryId());
configDataList.add(configJson);
}
intervalStatus = new IntervalStatus(1, "", configDataList);
}
public void querySleepingConfig(JSONObject parameters){
int botId = parameters.getInteger("botId");
JSONArray sleepingConfigs = new JSONArray();
JSONObject configJson = null;
List<ConfigTerm> configTermList = helper.querySleepingConfig(botId);
for(ConfigTerm configTerm:configTermList){
sleepingConfigs.add(new JSONArray(){
{
add(configTerm.getConfigId());
add(configTerm.getConfigName());
}
});
}
intervalStatus = new IntervalStatus(1, "", sleepingConfigs);
}
/**
* 将文本中的一些特殊字符过滤掉
* @param text
* @param replaceMap
* @return
*/
public static String textReplace(String text, Map<String, String> replaceMap){
for(String key:replaceMap.keySet()){
text = text.replace(key, replaceMap.get(key));
}
return text;
}
public void generateSingleConfigText(JSONObject configJson){
}
}
......@@ -167,9 +167,9 @@ public class ConfigTermText implements Cloneable{
}
public ConfigTermText(List<Premise> curPremise, List<Premise> hisPremise, Map<String, String> updateKnowledge, String extraCP, String extraHP, String extraUK, String feature, String reply, String nextStep, String auxiliaryPriority) {
this.curPremise = appendPremiseStr(curPremise, extraCP);
this.hisPremise = appendPremiseStr(hisPremise, extraHP);
public ConfigTermText(List<Premise> curPremise, List<Premise> hisPremise, Map<String, String> updateKnowledge, String extraCP, String extraHP, String extraUK, String feature, String reply, String nextStep, String auxiliaryPriority, Map<String ,Integer> gNameCount) {
this.curPremise = appendPremiseStr(curPremise, extraCP, gNameCount);
this.hisPremise = appendPremiseStr(hisPremise, extraHP, gNameCount);
this.updateKnowledge = appendKnowledgeStr(updateKnowledge, extraUK);
this.feature = feature;
this.reply = reply;
......@@ -177,26 +177,28 @@ public class ConfigTermText implements Cloneable{
this.auxiliaryPriority = auxiliaryPriority;
}
public String appendPremiseStr(List<Premise> premises, String extraPremise) {
public String appendPremiseStr(List<Premise> premises, String extraPremise, Map<String ,Integer> gNameCount) {
StringBuffer sb = new StringBuffer();
String premiseStr = "", conditionName;
Map<String ,Integer> nameCount = new HashMap<String , Integer>();
for (Premise premise : premises){
conditionName = premise.getConditionName();
if(conditionName.startsWith("npc_") ||
conditionName.startsWith("goods_") ||
conditionName.startsWith("globalVariable_") ||
conditionName.startsWith("map_"))
conditionName.startsWith("map_") ||
conditionName.startsWith("start_options") ||
conditionName.startsWith("load_game_by_site")
)
{}
else {
nameCount.putIfAbsent(conditionName, 0);
//nameCount加一
nameCount.put(conditionName, nameCount.get(conditionName) + 1);
//在conditionName后添加count
if(nameCount.get(conditionName) > 1) {
premise.setConditionName(conditionName + nameCount.get(conditionName) + "_independent");
}
gNameCount.putIfAbsent(conditionName, 0);
gNameCount.put(conditionName, gNameCount.get(conditionName) + 1);
if(gNameCount.get(conditionName) > 1 && premise.getAccurateMatch()) {
premise.setConditionName(conditionName + "_" + gNameCount.get(conditionName));
premise.setAccurateName();
}
}
sb.append(",");
......
package com.qiwu.chatbotbackend.entity;
public class Premise {
private String conditionName = "";
private String cmpType = "";
private String operator = "";
private String keyword = "";
public Premise(String conditionName, String cmpType, String operator, String keyword) {
this.conditionName = conditionName;
this.cmpType = cmpType;
this.operator = operator;
this.keyword = keyword;
if(cmpType == null){
this.cmpType = "match";
}else{
this.cmpType = cmpType;
}
if(operator == null){
this.operator = "Include";
}else{
this.operator = operator;
}
if(cmpType.equals("any")){
this.operator = "Include";
}
}
public String getConditionName() {
return conditionName;
}
public void setConditionName(String conditionName) {
this.conditionName = conditionName;
}
public String getCmpType() {
return cmpType;
}
public void setCmpType(String cmpType) {
this.cmpType = cmpType;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
}
package com.qiwu.chatbotbackend.entity;
public class Premise {
private String conditionName = "";
private String cmpType = "";
private String operator = "";
private String keyword = "";
private Boolean accurateMatch = false;
private String plot = "";
private int branch = 0;
public String getPlot() {
return plot;
}
public void setPlot(String plot) {
this.plot = plot;
}
public int getBranch() {
return branch;
}
public void setBranch(int branch) {
this.branch = branch;
}
public Boolean getAccurateMatch() {
return accurateMatch;
}
public void setAccurateMatch(Boolean accurateMatch) {
this.accurateMatch = accurateMatch;
}
public Premise(String conditionName, String cmpType, String operator, String keyword, Boolean accurateMatch) {
this.conditionName = conditionName;
this.cmpType = cmpType;
this.operator = operator;
this.keyword = keyword;
this.accurateMatch = accurateMatch;
if(cmpType == null){
this.cmpType = "match";
}else{
this.cmpType = cmpType;
}
if(operator == null){
this.operator = "Include";
}else{
this.operator = operator;
}
if(cmpType.equals("any")){
this.operator = "Include";
}
if(accurateMatch == null)
{
this.accurateMatch = false;
}
}
public Premise(String conditionName, String cmpType, String operator, String keyword) {
this.conditionName = conditionName;
this.cmpType = cmpType;
this.operator = operator;
this.keyword = keyword;
this.accurateMatch = false;
if(cmpType == null){
this.cmpType = "match";
}else{
this.cmpType = cmpType;
}
if(operator == null){
this.operator = "Include";
}else{
this.operator = operator;
}
if(cmpType.equals("any")){
this.operator = "Include";
}
}
public String getConditionName() {
return conditionName;
}
public void setConditionName(String conditionName) {
this.conditionName = conditionName;
}
public String getCmpType() {
return cmpType;
}
public void setCmpType(String cmpType) {
this.cmpType = cmpType;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public void setAccurateName() {
this.conditionName = this.conditionName + "_indep";
}
}
package com.qiwu.chatbotbackend.gameDesign;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiwu.chatbotbackend.db.SqlHelper;
import com.qiwu.chatbotbackend.entity.ConfigTermText;
import com.qiwu.chatbotbackend.entity.Premise;
import com.qiwu.chatbotbackend.entity.UserInfo;
import java.util.*;
import static com.qiwu.chatbotbackend.Config.Config.*;
public class ListeningConstructor extends ElementConstructor{
public ListeningConstructor(SqlHelper helper, int botId, String botAccount, Map<String, StringBuffer> keywordMap, HashSet<String> exceptRelation, StringBuffer POS) {
super();
this.helper = helper;
this.botId = botId;
this.botAccount = botAccount;
this.exceptRelation = exceptRelation;
this.keywordMap = keywordMap;
this.POS = POS;
}
public void construct(JSONObject jsonObject, UserInfo userinfo) {
}
public static void cc(String botAccount, JSONObject gameELments, HashMap<String, JSONObject> plotIndex, List<ConfigTermText> configTermTextList){
int size = 0;
String feature, nextPlotId, reply = "", nextCondition = "", nextLocation = "", priority;
String nTaskLineName, lastTaskCondition, lastTaskReply, nextGameStep;
Boolean autoJump = false;
List<Premise> curPremiseList = new ArrayList<Premise>();
List<Premise> hisPremiseList = new ArrayList<Premise>();
JSONArray listeningActions = gameELments.getJSONArray("listeningActions");
JSONObject plotNode = null;
if(listeningActions == null)
return;
size = listeningActions.size();
JSONObject config = null;
for(int i = 0; i < size;i++){
plotNode = null;
autoJump = false;
priority = "2";
feature = "match";
config = listeningActions.getJSONObject(i);
curPremiseList = JSONArray.parseArray(config.getJSONArray("curPremise").toJSONString(), Premise.class);
hisPremiseList = JSONArray.parseArray(config.getJSONArray("hisPremise").toJSONString(), Premise.class);
Map<String, String> updateKnowledge = JSONObject.parseObject(config.getJSONObject("updateKnowledge").toJSONString(), Map.class);
nextPlotId = config.getString("nextPlotId");
if(nextPlotId != null){
plotNode = plotIndex.get(nextPlotId);
}
if(plotNode != null){
reply = plotNode.getString("plotText");
nextCondition = plotNode.getString("conditionName");
nextLocation = plotNode.getString("place");
autoJump = plotNode.getBoolean("autoJump");
if(nextCondition == null || nextCondition.equals("")){
nextCondition = DEFAULT_CONDITION;
}
if(autoJump){
feature = "match,activate:after";
updateKnowledge.put("plot_jump_text", "str+" + reply);
nextCondition = "plot_" + nextPlotId;
reply = "";
}else{
feature = "match";
}
if(config.getString("priority") != null)
priority = config.getString("priority");
updateKnowledge.put(nextLocation + "_activate_by", "");
updateKnowledge.put(nextLocation + "_activate_ready", "0");
updateKnowledge.put("site", nextLocation);
nTaskLineName = String.format(TASK_LINE, nextLocation);
//任务线最后一个condition
lastTaskCondition = String.format(TASK_CONDITION, nextLocation);
lastTaskReply = String.format(TASK_REPLY, nextLocation);
nextGameStep = String.format(GAME_STEP_FORMAT, botAccount,nextPlotId);
updateKnowledge.put(lastTaskCondition, nextCondition);
updateKnowledge.put(lastTaskReply, reply);
updateKnowledge.put(nextPlotId, "1"); //下一步设为1
updateKnowledge.put(nTaskLineName, nextGameStep);
updateKnowledge.put("game_step", nextGameStep);
}else{
feature = "match,return-0";
}
ConfigTermText configTermText = new ConfigTermText(
curPremiseList,
hisPremiseList,
updateKnowledge,
"", "", "", feature, reply, nextCondition, priority
);
// System.out.println("生成:" + curPremiseList.get(0));
configTermTextList.add(configTermText);
}
}
}
package com.qiwu.chatbotbackend.gameDesign;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qiwu.chatbotbackend.db.SqlHelper;
import com.qiwu.chatbotbackend.entity.ConfigTermText;
import com.qiwu.chatbotbackend.entity.Premise;
import com.qiwu.chatbotbackend.entity.UserInfo;
import java.util.*;
import static com.qiwu.chatbotbackend.Config.Config.*;
public class ListeningConstructor extends ElementConstructor{
public ListeningConstructor(SqlHelper helper, int botId, String botAccount, Map<String, StringBuffer> keywordMap, HashSet<String> exceptRelation, StringBuffer POS) {
super();
this.helper = helper;
this.botId = botId;
this.botAccount = botAccount;
this.exceptRelation = exceptRelation;
this.keywordMap = keywordMap;
this.POS = POS;
}
public void construct(JSONObject jsonObject, UserInfo userinfo) {
}
public static void cc(String botAccount, JSONObject gameELments, HashMap<String, JSONObject> plotIndex, List<ConfigTermText> configTermTextList){
int size = 0;
String feature, nextPlotId, reply = "", nextCondition = "", nextLocation = "", priority;
String nTaskLineName, lastTaskCondition, lastTaskReply, nextGameStep;
Boolean autoJump = false;
List<Premise> curPremiseList = new ArrayList<Premise>();
List<Premise> hisPremiseList = new ArrayList<Premise>();
JSONArray listeningActions = gameELments.getJSONArray("listeningActions");
JSONObject plotNode = null;
if(listeningActions == null)
return;
size = listeningActions.size();
JSONObject config = null;
for(int i = 0; i < size;i++){
plotNode = null;
autoJump = false;
priority = "2";
feature = "match";
config = listeningActions.getJSONObject(i);
curPremiseList = JSONArray.parseArray(config.getJSONArray("curPremise").toJSONString(), Premise.class);
hisPremiseList = JSONArray.parseArray(config.getJSONArray("hisPremise").toJSONString(), Premise.class);
Map<String, String> updateKnowledge = JSONObject.parseObject(config.getJSONObject("updateKnowledge").toJSONString(), Map.class);
nextPlotId = config.getString("nextPlotId");
if(nextPlotId != null){
plotNode = plotIndex.get(nextPlotId);
}
if(plotNode != null){
reply = plotNode.getString("plotText");
nextCondition = plotNode.getString("conditionName");
nextLocation = plotNode.getString("place");
autoJump = plotNode.getBoolean("autoJump");
if(nextCondition == null || nextCondition.equals("")){
nextCondition = DEFAULT_CONDITION;
}
if(autoJump){
feature = "match,activate:after";
updateKnowledge.put("plot_jump_text", "str+" + reply);
nextCondition = "plot_" + nextPlotId;
reply = "";
}else{
feature = "match";
}
if(config.getString("priority") != null)
priority = config.getString("priority");
updateKnowledge.put(nextLocation + "_activate_by", "");
updateKnowledge.put(nextLocation + "_activate_ready", "0");
updateKnowledge.put("site", nextLocation);
nTaskLineName = String.format(TASK_LINE, nextLocation);
//任务线最后一个condition
lastTaskCondition = String.format(TASK_CONDITION, nextLocation);
lastTaskReply = String.format(TASK_REPLY, nextLocation);
nextGameStep = String.format(GAME_STEP_FORMAT, botAccount,nextPlotId);
updateKnowledge.put(lastTaskCondition, nextCondition);
updateKnowledge.put(lastTaskReply, reply);
updateKnowledge.put(nextPlotId, "1"); //下一步设为1
updateKnowledge.put(nTaskLineName, nextGameStep);
updateKnowledge.put("game_step", nextGameStep);
}else{
feature = "match,return-0";
}
Map<String ,Integer> gNameCount = new HashMap<String , Integer>();
ConfigTermText configTermText = new ConfigTermText(
curPremiseList,
hisPremiseList,
updateKnowledge,
"", "", "", feature, reply, nextCondition, priority, gNameCount
);
// System.out.println("生成:" + curPremiseList.get(0));
configTermTextList.add(configTermText);
}
}
}
......@@ -220,6 +220,9 @@ public class PlotConstructor2 extends ElementConstructor {
int branchSize;
List<Premise> curPremiseList, historyPremiseList;
//condition name 计数
Map<String ,Integer> gNameCount = new HashMap<String , Integer>();
//设置初始化unknown文本
......@@ -487,16 +490,12 @@ public class PlotConstructor2 extends ElementConstructor {
}
//下一部剧情在不同地点时,为了防止到配置文件重复执行,需要加一个标志表示下一步剧情是否已经发生过
// activate_by 下一个地点的剧情是由当前剧情激活的
//_activate_ready 下一个地点的剧情等待被激活
appendKnowledge(extraUK, nextLocation + "_activate_by", id);
appendKnowledge(extraUK, nextLocation + "_activate_ready", "1");
// 如果不能直接到达所在地点,说明剧情1-1和1-2直接存在一个中间结点,这些更新操作会放在中间结点执行(动态生成,方法名:arriveActivatePlot2 这个中间结点会被当做一个监听剧情来处理)
arriveActivatePlot2(jsonObject, id, nextId, nextLocation);
......@@ -506,13 +505,12 @@ public class PlotConstructor2 extends ElementConstructor {
}
}
//System.out.println(branchNode);
//取出作者设定的剧情条件
curPremiseList = JSONArray.parseArray(branchNode.getJSONArray("curPremise").toJSONString(), Premise.class);
historyPremiseList = JSONArray.parseArray(branchNode.getJSONArray("hisPremise").toJSONString(), Premise.class);
addContext(curPremiseList, node.getString("id"), i);
addContext(historyPremiseList, node.getString("id"), i);
//是否将当前识别到的数据加入到已知数据列表
if((Boolean)branchNode.get("includeCurInfo")){
......@@ -525,7 +523,6 @@ public class PlotConstructor2 extends ElementConstructor {
appendFeature(extraFT, branchNode.getString("extraFeature"));
}
//如果符合激活下一个剧情配置文件的操作
if(doActivatePlot(nextAutoJump, location, nextLocation, moveWithPlot) ){
//在配置文件中添加激活的操作
......@@ -540,7 +537,7 @@ public class PlotConstructor2 extends ElementConstructor {
historyPremiseList,
updateKnowledge,
"", extraHP.toString(), extraUK.toString(),
extraFT.toString(), reply, nextCondition, "1"
extraFT.toString(), reply, nextCondition, "1", gNameCount
);
configTermText.setConfigName("plot_" + id, false);
......@@ -569,8 +566,6 @@ public class PlotConstructor2 extends ElementConstructor {
configTermText.setAuxiliaryPriority("3");
configTermTextList.add(configTermText);
//将子结点加入队列
adjId = nextId;
plotEnQueue(queue, id, adjId, nextUnknownText, curAutoJump);
......@@ -585,19 +580,8 @@ public class PlotConstructor2 extends ElementConstructor {
extraUK.setLength(0);
skipText.setLength(0);
arriveUK.setLength(0);
}
}
/**
* ----------------------------------------------------------------------------------------------------
* 剧情不设条件时的处理
......@@ -923,10 +907,10 @@ public class PlotConstructor2 extends ElementConstructor {
//激活下一步剧情的几个条件:1. condition 到沙滩 2. 沙滩_activate_by 沙滩的剧情是有当前剧情激活的 3. 沙滩的剧情处于等待激活的状态 4. 当前地点和沙滩地点相邻或可以直接到达
// 2.3 两个条件是特意加的
curPremiseList.add(new Premise(GOTO_CONDITION, "match", "Include", nextLocation));
hisPremiseList.add(new Premise(nextLocation + "_activate_by", "match", "Include", plotId));
hisPremiseList.add(new Premise(nextLocation + "_activate_ready", "match", "Include", "1"));
hisPremiseList.add(new Premise("isNeighbour:" + GOTO_CONDITION, "match", "Include", "1+3+4"));
curPremiseList.add(new Premise(GOTO_CONDITION, "match", "Include", nextLocation, false));
hisPremiseList.add(new Premise(nextLocation + "_activate_by", "match", "Include", plotId, false));
hisPremiseList.add(new Premise(nextLocation + "_activate_ready", "match", "Include", "1", false));
hisPremiseList.add(new Premise("isNeighbour:" + GOTO_CONDITION, "match", "Include", "1+3+4", false));
//无匹配次数清零
......@@ -1003,20 +987,26 @@ public class PlotConstructor2 extends ElementConstructor {
}
private ConfigTermText createTermExtra2(ConfigTermText term) {
String hPremise = "qa_score::number-Smaller=98";
String featureStr = "match";
String nextStr = " ";
// String featureStr = ",match,includeCurInfo,activate:after";
String configName = term.getConfigName()+"_qa2";
if(term.getFeature().indexOf("activate") != -1) {
nextStr = term.getNextStep() + "," + nextStr;
}
String nextStr = term.getNextStep();
ConfigTermText newTerm = (ConfigTermText)term.clone();
newTerm.setHisPremise(hPremise);
newTerm.setConfigName(configName, false);
newTerm.setFeature(featureStr);
// newTerm.setFeature(featureStr);
newTerm.setNextStep(nextStr);
newTerm.setStatus("sleeping");
//newTerm.setReply("");
return newTerm;
}
private void addContext(List<Premise> premises, String plot, int branch) {
for(Premise pre : premises) {
pre.setPlot(plot);
pre.setBranch(branch);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment