【修改语音识别成员解析逻辑】

This commit is contained in:
weizhihong 2023-04-18 11:31:47 +08:00
parent 0dddb9b4e8
commit c02fa14d31
4 changed files with 91 additions and 55 deletions

View File

@ -44,11 +44,16 @@ public class OperateParseServiceImpl implements VoiceTransactionalService {
if (operateRule == null) {
return;
}
// 参数解析
List<ParamExtractResult> paramExtractResults = result.getParamExtractResultList();
Map<Integer,ParamExtractResult> paramExtractResultMap = IntStream.range(0,paramExtractResults.size()).boxed().collect(Collectors.toMap(i->++i,paramExtractResults::get));
OperateResult operateResult = new OperateResult(operateRule.getType());
int paramSize = paramExtractResults.size();
// 成员解析
List<SimulationMember> simulationMemberList = filterSimulationMember(simulation, result, paramExtractResultMap, operateRule.getMemberRule());
if (CollectionUtils.isEmpty(simulationMemberList)) {
return;
}
// 参数解析
for (CommandParamRule paramRule : operateRule.getParamRules()) {
ParamExtractResult[] paramArr = new ParamExtractResult[paramRule.getIndexArr().length];
for (int index = 0, len = paramArr.length; index < len; index ++) {
@ -67,33 +72,18 @@ public class OperateParseServiceImpl implements VoiceTransactionalService {
operateResult.putParam(getMethodParamName(paramRule.getParamIndex(), operateRule.getType().name()), val);
} else {
// 参数位置未指定
return;
}
}
// 成员解析
MemberRule memberRule = operateRule.getMemberRule();
if (memberRule != null) {
List<SimulationMember> simulationMemberList = null;
if (memberRule.getIndex() == null) {
simulationMemberList = memberRule.getParseRule().matchMember(simulation, null);
} else if (memberRule.getIndex() < paramExtractResults.size()) {
simulationMemberList = memberRule.getParseRule().matchMember(simulation, paramExtractResultMap.get(memberRule.getIndex()));
} else {
return;
}
if (CollectionUtils.isEmpty(simulationMemberList)) {
return;
}
operateResult.setMember(simulationMemberList.get(0));
}
// 执行操作
try {
atsOperationDispatcher.execute(simulation, operateResult.getMember(), operateResult.getType().name(), operateResult.getParams());
log.info("执行语音指令操作成功,操作[{}] 源指令[{}]",operateResult.getType().name(),result.getOriginContent());
} catch (Exception e) {
log.error("执行操作失败[{}] 输入源指令[{}] msg[{}]",operateResult.getType().name(),result.getOriginContent(),e.getMessage(),e);
}
simulationMemberList.forEach(member -> {
try {
atsOperationDispatcher.execute(simulation, member, operateResult.getType().name(), operateResult.getParams());
log.info("[{}]执行语音指令操作成功,操作[{}] 源指令[{}]", member.getName(), operateResult.getType().name(), result.getOriginContent());
} catch (Exception e) {
log.error("[{}]执行操作失败[{}] 输入源指令[{}] msg[{}]", member.getName(), operateResult.getType().name(), result.getOriginContent(),e.getMessage(),e);
}
});
}
/**
@ -115,7 +105,7 @@ public class OperateParseServiceImpl implements VoiceTransactionalService {
* @param operateName 操作名称
* @return 参数名称
*/
public String getMethodParamName(int index, String operateName) {
private String getMethodParamName(int index, String operateName) {
OperateMethod handlerMethod = this.atsManager.getHandlerMethod(operateName);
Parameter[] parameters = handlerMethod.getMethod().getParameters();
if (parameters.length < index) {
@ -123,4 +113,29 @@ public class OperateParseServiceImpl implements VoiceTransactionalService {
}
return parameters[index].getName();
}
/**
* 获取执行操作的人员
* @return 人员列表
*/
private List<SimulationMember> filterSimulationMember(Simulation simulation, VoiceDiscriminateResult result
, Map<Integer,ParamExtractResult> paramExtractResultMap, MemberRule memberRule) {
int paramSize = result.getParamExtractResultList().size();
List<SimulationMember> simulationMemberList = null;
if (memberRule != null) { // 如果有指定成员解析规则使用此规则解析
if (memberRule.getIndex() == null) {
simulationMemberList = memberRule.getParseRule().matchMember(simulation, null);
} else if (memberRule.getIndex() < paramSize) {
simulationMemberList = memberRule.getParseRule().matchMember(simulation, paramExtractResultMap.get(memberRule.getIndex()));
}
return simulationMemberList; // 返回解析结果
} else {
simulationMemberList = result.getSimulationMemberList(); // 获取群组成员对象
if (CollectionUtils.isEmpty(simulationMemberList) || simulationMemberList.size() > 1) {
return List.of();
} else {
return simulationMemberList;
}
}
}
}

View File

@ -30,10 +30,16 @@ public class ReplyParseServiceImpl implements VoiceTransactionalService {
if (replyRule == null) {
return;
}
// 回复成员
List<SimulationMember> simulationMemberList = filterSimulationMember(simulation, result, replyRule.getMemberRule());
if (CollectionUtils.isEmpty(simulationMemberList)) {
return;
}
// 格式化回复语句
List<ParamExtractResult> paramExtractResults = result.getParamExtractResultList();
ReplyResult replyResult = new ReplyResult(replyRule.getParamRules().size());
ParamExtractResult[] paramArr = null;
int paramSize = paramExtractResults.size();
ParamExtractResult[] paramArr = null;
for (CommandParamRule paramRule : replyRule.getParamRules()) {
paramArr = new ParamExtractResult[paramRule.getIndexArr().length];
for (int index = 0, len = paramArr.length; index < len; index ++) {
@ -45,31 +51,33 @@ public class ReplyParseServiceImpl implements VoiceTransactionalService {
}
replyResult.addParams(paramRule.getParseRule().extractParam(simulation, paramArr));
}
// 格式化回复语句
Object[] objArr = new Object[replyResult.getParamList().size()];
replyResult.getParamList().toArray(objArr);
replyResult.setContent(String.format(replyRule.getMessageFormat(), objArr));
// 匹配回复人员
MemberRule memberRule = replyRule.getMemberRule();
// 执行回复
simulationMemberList.forEach(member -> conversationManagerService.conversationChat(simulation, member, replyResult.getContent(), null));
}
/**
* 获取回复成员列表
*
* @param simulation 仿真
* @param result 封装的结果信息
* @param memberRule 成员解析规则
* @return 回复成员列表
*/
private List<SimulationMember> filterSimulationMember(Simulation simulation, VoiceDiscriminateResult result, MemberRule memberRule) {
List<ParamExtractResult> paramExtractResults = result.getParamExtractResultList();
List<SimulationMember> simulationMemberList = null;
if (memberRule != null) {
List<SimulationMember> simulationMemberList = null;
if (memberRule.getIndex() == null) {
simulationMemberList = memberRule.getParseRule().matchMember(simulation, null);
} else if (memberRule.getIndex() < paramExtractResults.size()) {
simulationMemberList = memberRule.getParseRule().matchMember(simulation, paramExtractResults.get(memberRule.getIndex() - 1));
} else {
return;
}
if (CollectionUtils.isEmpty(simulationMemberList)) {
return;
}
replyResult.setSimulationMemberList(simulationMemberList);
return simulationMemberList;
} else {
return result.getSimulationMemberList();
}
// 执行回复
replyResult.getSimulationMemberList().forEach(member -> {
conversationManagerService.conversationChat(simulation, member, replyResult.getContent(), null);
});
}
}

View File

@ -1,6 +1,7 @@
package club.joylink.rtss.services.voice.discriminate;
import club.joylink.rtss.simulation.cbtc.conversation.ConversationGroup;
import club.joylink.rtss.simulation.cbtc.conversation.ConversationMember;
import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult;
import club.joylink.rtss.simulation.cbtc.Simulation;
import club.joylink.rtss.simulation.cbtc.event.conversation.SimulationConversationGroupDissolveEvent;
@ -11,8 +12,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* 语音识别服务类
@ -56,6 +62,17 @@ public class VoiceDiscriminateService {
correctSourceService.doExec(result, simulation);// 原信息纠错
}
/**
* 接受语音文件资源等必要条件解析开始
*/
public void doAnalysis(Simulation simulation, String content, List<SimulationMember> memberList) {
VoiceDiscriminateResult result = voiceParseService.doParse(simulation, content);
result.setSimulationMemberList(memberList); // 匹配到人员集合
operateParseService.doExec(result, simulation); // 指令执行
replyParseService.doExec(result, simulation); // 回复执行
correctSourceService.doExec(result, simulation);// 原信息纠错
}
/**
* 处理聊天信息
* @param event 聊天事件信息
@ -63,8 +80,14 @@ public class VoiceDiscriminateService {
@EventListener
public void handleMessage(SimulationConversationGroupMessageEvent event) {
ConversationGroup conversationGroup = event.getConversationGroup();
SimulationMember member = event.getMember();
List<SimulationMember> memberList = conversationGroup.getMemberList().stream()
.filter(cm -> cm.isRobot() && !Objects.equals(cm.getMember(), member))
.map(ConversationMember::getMember).collect(Collectors.toList());
if (CollectionUtils.isEmpty(memberList)) { // 人员全部为真实人员时直接返回
return;
}
// 执行语音识别
doAnalysis(event.getSimulation(), event.getContent(), memberList);
}
}

View File

@ -1,7 +1,6 @@
package club.joylink.rtss.simulation.cbtc.discriminate;
import club.joylink.rtss.simulation.cbtc.ATS.operation.Operation;
import club.joylink.rtss.simulation.cbtc.member.SimulationMember;
import lombok.Data;
import java.util.HashMap;
@ -18,24 +17,15 @@ public class OperateResult {
*/
private Operation.Type type;
// private Boolean operationResult;
// private String errResultMsg;
/**
* 指令参数
*/
private Map<String, Object> params = new HashMap<>();
/**
* 角色ID
*/
private SimulationMember member;
public OperateResult(Operation.Type type) {
this.type = type;
}
/**
* 参数放入集合
* @param key 集合key