diff --git a/src/main/java/club/joylink/rtss/configuration/TaskExecutorConfiguration.java b/src/main/java/club/joylink/rtss/configuration/TaskExecutorConfiguration.java index 777a6a7a9..2cba694a5 100644 --- a/src/main/java/club/joylink/rtss/configuration/TaskExecutorConfiguration.java +++ b/src/main/java/club/joylink/rtss/configuration/TaskExecutorConfiguration.java @@ -75,4 +75,19 @@ public class TaskExecutorConfiguration { taskExecutor.initialize(); return taskExecutor; } + + /** + * 语音识别业务处理线程 + */ + @Bean("voiceDiscriminateExecutor") + public TaskExecutor voiceDiscriminateExecutor(Environment env) { + ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); + taskExecutor.setThreadNamePrefix("ns-voice-discriminate-executor-"); + taskExecutor.setCorePoolSize(2); + taskExecutor.setMaxPoolSize(2); + taskExecutor.setQueueCapacity(100); + taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + taskExecutor.initialize(); + return taskExecutor; + } } diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/CorrectSourceServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/CorrectSourceServiceImpl.java new file mode 100644 index 000000000..6de272ec9 --- /dev/null +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/CorrectSourceServiceImpl.java @@ -0,0 +1,18 @@ +package club.joylink.rtss.services.voice.discriminate; + +import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; +import club.joylink.rtss.simulation.cbtc.Simulation; +import org.springframework.stereotype.Service; + + +/** + * 纠正源信息问题 + */ +@Service("correctSourceServiceImpl") +public class CorrectSourceServiceImpl implements VoiceTransactionalService { + + @Override + public void doExec(VoiceDiscriminateResult result, Simulation simulation) { + + } +} diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/ExecCommandServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/ExecCommandServiceImpl.java deleted file mode 100644 index f043beca2..000000000 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/ExecCommandServiceImpl.java +++ /dev/null @@ -1,48 +0,0 @@ -package club.joylink.rtss.services.voice.discriminate; - -import club.joylink.rtss.simulation.cbtc.discriminate.OperateResult; -import club.joylink.rtss.simulation.cbtc.discriminate.ReplyResult; -import club.joylink.rtss.simulation.cbtc.discriminate.Stage; -import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; -import club.joylink.rtss.simulation.cbtc.ATS.operation.AtsOperationDispatcher; -import club.joylink.rtss.simulation.cbtc.Simulation; -import club.joylink.rtss.simulation.cbtc.conversation.ConversationManagerService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -/** - * 执行指令 - */ -@Service("execCommandServiceImpl") -public class ExecCommandServiceImpl implements VoiceDiscriminateStepService { - - @Autowired - private AtsOperationDispatcher atsOperationDispatcher; - - @Autowired - private ConversationManagerService conversationManagerService; - - @Override - public void doExec(VoiceDiscriminateResult result, Simulation simulation) { - result.setStage(Stage.EXEC_COMMAND); - // 执行操作 - try { - if (result.getOperateResult() != null) { - OperateResult operateResult = result.getOperateResult(); - atsOperationDispatcher.execute(simulation, operateResult.getMember(), operateResult.getType().name(), operateResult.getParams()); - } - } catch (Exception e) { - result.setSuccess(false); - result.setMsg(Stage.EXEC_COMMAND.getDesc() + "执行失败:" + e.getMessage()); - return; - } - - // 执行回复 - if (result.getReplyContentResult() != null) { - ReplyResult replyResult = result.getReplyContentResult(); - replyResult.getSimulationMemberList().forEach(member -> { - conversationManagerService.conversationChat(simulation, member, replyResult.getContent(), null); - }); - } - } -} diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/LoadVoiceFileServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/LoadVoiceFileServiceImpl.java deleted file mode 100644 index ca8cd834f..000000000 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/LoadVoiceFileServiceImpl.java +++ /dev/null @@ -1,50 +0,0 @@ -package club.joylink.rtss.services.voice.discriminate; - -import club.joylink.rtss.services.voice.VoiceService; -import club.joylink.rtss.simulation.cbtc.discriminate.Stage; -import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; -import club.joylink.rtss.simulation.cbtc.Simulation; -import club.joylink.rtss.vo.client.voice.VoiceRecognitionResult; -import club.joylink.rtss.vo.client.voice.VoiceRecognitionVO; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -/** - * 对原始语音文件进行解析 - */ -@Slf4j -@Service("loadVoiceFileServiceImpl") -public class LoadVoiceFileServiceImpl implements VoiceDiscriminateStepService { - /** - * 语音解析服务 - */ - @Autowired - private VoiceService voiceService; - - @Override - public void doExec(VoiceDiscriminateResult result, Simulation simulation) { - result.setStage(Stage.LOAD); - try { - VoiceRecognitionVO vo = VoiceRecognitionVO.load(result.getFile()); - VoiceRecognitionResult voiceRecognitionResult = voiceService.voiceRecognition(vo); - String originVoiceContent = voiceRecognitionResult.getResult(); - String originVoicePinYin = translateToPinYin(originVoiceContent); - result.setOriginContent(originVoiceContent); - result.setOriginPinYin(originVoicePinYin); - } catch (Exception e) { - result.setSuccess(false); - result.setMsg(e.getCause().getMessage()); - log.error(result.getStage().getDesc(), e); - } - } - - /** - * 翻译标准 - * @param result - * @return - */ - private String translateToPinYin(String result) { - return null; - } -} diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/MatchCommandServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/MatchCommandServiceImpl.java deleted file mode 100644 index 2d97a2a2b..000000000 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/MatchCommandServiceImpl.java +++ /dev/null @@ -1,55 +0,0 @@ -package club.joylink.rtss.services.voice.discriminate; - -import club.joylink.rtss.simulation.cbtc.discriminate.Stage; -import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; -import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateRule; -import club.joylink.rtss.simulation.cbtc.Simulation; -import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.BiFunction; - - -/** - * 匹配原始指令 - */ -@Service("matchCommandServiceImpl") -public class MatchCommandServiceImpl implements VoiceDiscriminateStepService { - - /** - * 关键字匹配方法 - */ - private final Map>> keyWordsMatchMethodMap = new HashMap<>(); - - /** - * 关键字匹配接口 - */ - private final BiFunction> defaultKeyWordsMatch = (originContent, ruleInfo) -> { - return null; - }; - - @Override - public void doExec(VoiceDiscriminateResult result, Simulation simulation) { - result.setStage(Stage.MATCH_COMMAND); - // 加载本地图的规则信息列表 - List ruleList = List.of(new VoiceDiscriminateRule()); - // 匹配指令 - BiFunction> matchMethod = null; - List matchGroupList = null; - for (VoiceDiscriminateRule rule : ruleList) { - matchMethod = keyWordsMatchMethodMap.getOrDefault(rule.getKwMatchMethod(), defaultKeyWordsMatch); - matchGroupList = matchMethod.apply(result.getMatchOriginContent(), rule); - if (!CollectionUtils.isEmpty(matchGroupList)) { - result.setRule(rule); - result.setMatchGroupList(matchGroupList); - return; - } - } - // 出错,未匹配到指令 - result.setSuccess(false); - result.setMsg(result.getStage().getDesc() + ",未匹配到对应指令"); - } -} diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java index 9b99d0a9f..6b099830c 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/OperateParseServiceImpl.java @@ -1,8 +1,11 @@ package club.joylink.rtss.services.voice.discriminate; +import club.joylink.rtss.simulation.cbtc.ATS.operation.AtsOperationDispatcher; import club.joylink.rtss.simulation.cbtc.Simulation; import club.joylink.rtss.simulation.cbtc.discriminate.*; import club.joylink.rtss.simulation.cbtc.member.SimulationMember; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -12,29 +15,35 @@ import java.util.List; * 操作解析 */ @Service("operateParseServiceImpl") -public class OperateParseServiceImpl implements VoiceDiscriminateStepService { +public class OperateParseServiceImpl implements VoiceTransactionalService { + + @Autowired + private AtsOperationDispatcher atsOperationDispatcher; + @Override + @Async("voiceDiscriminateExecutor") public void doExec(VoiceDiscriminateResult result, Simulation simulation) { - result.setStage(Stage.OPERATE_PARSE); + if (!result.isSuccess()) { + return; + } OperateRule operateRule = result.getRule().getExecOperateRule(); if (operateRule == null) { return; } + // 参数解析 List paramExtractResults = result.getParamExtractResultList(); OperateResult operateResult = new OperateResult(operateRule.getType()); - result.setOperateResult(operateResult); for (CommandParamRule paramRule : operateRule.getParamRules()) { if (paramRule.getIndex() == null) { operateResult.putParam(paramRule.getName(), paramRule.getParseRule().extractParam(simulation, null)); } else if (paramRule.getIndex() < paramExtractResults.size()) { operateResult.putParam(paramRule.getName(), paramRule.getParseRule().extractParam(simulation, paramExtractResults.get(paramRule.getIndex()))); } else { - result.setSuccess(false); - result.setMsg(Stage.OPERATE_PARSE.getDesc() + "解析参数出错"); return; } } + // 成员解析 MemberRule memberRule = operateRule.getMemberRule(); if (memberRule != null) { List groupList = result.getMatchGroupList(); @@ -44,16 +53,19 @@ public class OperateParseServiceImpl implements VoiceDiscriminateStepService { } else if (memberRule.getIndex() < paramExtractResults.size()) { simulationMemberList = memberRule.getParseRule().matchMember(simulation, groupList.get(memberRule.getIndex())); } else { - result.setSuccess(false); - result.setMsg(Stage.OPERATE_PARSE.getDesc() + "解析成员出错"); return; } if (CollectionUtils.isEmpty(simulationMemberList)) { - result.setSuccess(false); - result.setMsg(Stage.OPERATE_PARSE.getDesc() + "未找到匹配成员"); return; } operateResult.setMember(simulationMemberList.get(0)); } + + // 执行操作 + try { + atsOperationDispatcher.execute(simulation, operateResult.getMember(), operateResult.getType().name(), operateResult.getParams()); + } catch (Exception e) { + + } } } diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/ParamExtractServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/ParamExtractServiceImpl.java deleted file mode 100644 index 6f9198cb4..000000000 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/ParamExtractServiceImpl.java +++ /dev/null @@ -1,37 +0,0 @@ -package club.joylink.rtss.services.voice.discriminate; - -import club.joylink.rtss.simulation.cbtc.discriminate.ParamExtractResult; -import club.joylink.rtss.simulation.cbtc.discriminate.ParamExtractRule; -import club.joylink.rtss.simulation.cbtc.discriminate.Stage; -import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; -import club.joylink.rtss.simulation.cbtc.Simulation; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.List; - -/** - * 解析参数 - */ -@Service("paramExtractServiceImpl") -public class ParamExtractServiceImpl implements VoiceDiscriminateStepService { - - @Override - public void doExec(VoiceDiscriminateResult result, Simulation simulation) { - result.setStage(Stage.PARAM_EXTRACT); - List groupList = result.getMatchGroupList(); - List paramsRules = result.getRule().getParamsRules(); - List paramExtractResults = new ArrayList<>(paramsRules.size()); - ParamExtractResult extractResult = null; - for (ParamExtractRule rule : paramsRules) { - if (groupList.size() < rule.getIndex()) { - result.setSuccess(false); - result.setMsg("提取参数出错"); - return; - } - extractResult = rule.getParseRule().matchParam(simulation, groupList.get(rule.getIndex())); - paramExtractResults.add(extractResult); - } - result.setParamExtractResultList(paramExtractResults); - } -} diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java similarity index 77% rename from src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyServiceImpl.java rename to src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java index 920e00f66..3ffb81e13 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/ReplyParseServiceImpl.java @@ -1,8 +1,11 @@ package club.joylink.rtss.services.voice.discriminate; import club.joylink.rtss.simulation.cbtc.Simulation; +import club.joylink.rtss.simulation.cbtc.conversation.ConversationManagerService; import club.joylink.rtss.simulation.cbtc.discriminate.*; import club.joylink.rtss.simulation.cbtc.member.SimulationMember; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -11,19 +14,24 @@ import java.util.List; /** * 合成回复内容 */ -@Service("replyServiceImpl") -public class ReplyServiceImpl implements VoiceDiscriminateStepService { +@Service("replyParseServiceImpl") +public class ReplyParseServiceImpl implements VoiceTransactionalService { + + @Autowired + private ConversationManagerService conversationManagerService; @Override + @Async("voiceDiscriminateExecutor") public void doExec(VoiceDiscriminateResult result, Simulation simulation) { - result.setStage(Stage.REPLY_CONTENT); + if (!result.isSuccess()) { + return; + } ReplyRule replyRule = result.getRule().getReplyRule(); if (replyRule == null) { return; } List paramExtractResults = result.getParamExtractResultList(); ReplyResult replyResult = new ReplyResult(replyRule.getParamRules().size()); - result.setReplyContentResult(replyResult); try { for (CommandParamRule paramRule : replyRule.getParamRules()) { if (paramRule.getIndex() == null) { @@ -31,14 +39,11 @@ public class ReplyServiceImpl implements VoiceDiscriminateStepService { } else if (paramRule.getIndex() < paramExtractResults.size()) { replyResult.addParams(paramRule.getParseRule().extractParam(simulation, paramExtractResults.get(paramRule.getIndex()))); } else { - result.setSuccess(false); - result.setMsg(Stage.REPLY_CONTENT.getDesc() + "解析参数出错"); return; } } } catch (Exception e) { - result.setSuccess(false); - result.setMsg(Stage.REPLY_CONTENT.getDesc() + "解析参数出错"); + return; } // 格式化回复语句 Object[] objArr = new Object[replyResult.getParamList().size()]; @@ -54,16 +59,17 @@ public class ReplyServiceImpl implements VoiceDiscriminateStepService { } else if (memberRule.getIndex() < paramExtractResults.size()) { simulationMemberList = memberRule.getParseRule().matchMember(simulation, groupList.get(memberRule.getIndex())); } else { - result.setSuccess(false); - result.setMsg(Stage.REPLY_CONTENT.getDesc() + "解析成员出错"); return; } if (CollectionUtils.isEmpty(simulationMemberList)) { - result.setSuccess(false); - result.setMsg(Stage.REPLY_CONTENT.getDesc() + "未找到匹配成员"); return; } replyResult.setSimulationMemberList(simulationMemberList); } + + // 执行回复 + replyResult.getSimulationMemberList().forEach(member -> { + conversationManagerService.conversationChat(simulation, member, replyResult.getContent(), null); + }); } } diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java index 8e40c0581..7d3e6fba9 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateService.java @@ -1,6 +1,5 @@ package club.joylink.rtss.services.voice.discriminate; -import club.joylink.rtss.simulation.cbtc.discriminate.Stage; import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; import club.joylink.rtss.simulation.cbtc.Simulation; import lombok.extern.slf4j.Slf4j; @@ -9,9 +8,6 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import javax.annotation.PostConstruct; -import java.util.*; - /** * 语音识别服务类(多例) @@ -21,62 +17,27 @@ import java.util.*; public class VoiceDiscriminateService { @Autowired - @Qualifier(value = "loadVoiceFileServiceImpl") - private VoiceDiscriminateStepService LoadVoiceFileService; - - @Autowired - @Qualifier(value = "matchCommandServiceImpl") - private VoiceDiscriminateStepService MatchCommandService; - - @Autowired - @Qualifier(value = "paramExtractServiceImpl") - private VoiceDiscriminateStepService paramExtractService; + private VoiceParseService voiceParseService; @Autowired @Qualifier(value = "operateParseServiceImpl") - private VoiceDiscriminateStepService operateParseService; + private VoiceTransactionalService operateParseService; @Autowired - @Qualifier(value = "replyServiceImpl") - private VoiceDiscriminateStepService replyService; + @Qualifier(value = "replyParseServiceImpl") + private VoiceTransactionalService replyParseService; @Autowired - @Qualifier(value = "execCommandServiceImpl") - private VoiceDiscriminateStepService execCommandService; - - - /** - * 执行步骤 - */ - private List stepList = new LinkedList<>(); - - @PostConstruct - public void initStepList() { - stepList.add(LoadVoiceFileService); - stepList.add(MatchCommandService); - stepList.add(paramExtractService); - stepList.add(operateParseService); - stepList.add(replyService); - stepList.add(execCommandService); - } + @Qualifier(value = "correctSourceServiceImpl") + private VoiceTransactionalService correctSourceService; /** * 接受语音文件、资源等必要条件,解析开始 */ - public VoiceDiscriminateResult doAnalysis(Simulation simulation, MultipartFile file) { - VoiceDiscriminateResult result = new VoiceDiscriminateResult(); - result.setSuccess(true); - result.setFile(file); - for (VoiceDiscriminateStepService step : stepList) { - if (result.isSuccess()) { - step.doExec(result, simulation); - } else { - break; - } - } - if (result.isSuccess()) { - result.setStage(Stage.SUCCESS); - } - return result; + public void doAnalysis(Simulation simulation, MultipartFile file) { + VoiceDiscriminateResult result = voiceParseService.doParse(simulation, file); + operateParseService.doExec(result, simulation); // 指令执行 + replyParseService.doExec(result, simulation); // 回复执行 + correctSourceService.doExec(result, simulation);// 原信息纠错 } } diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceParseService.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceParseService.java new file mode 100644 index 000000000..6192ddab0 --- /dev/null +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceParseService.java @@ -0,0 +1,30 @@ +package club.joylink.rtss.services.voice.discriminate; + +import club.joylink.rtss.simulation.cbtc.Simulation; +import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; +import org.springframework.web.multipart.MultipartFile; + +/** + * 语音解析步骤服务 + */ +public interface VoiceParseService { + + /** + * 解析语音任务 + * + * @param simulation 仿真 + * @param file 文件 + * @return 解析结果 + */ + VoiceDiscriminateResult doParse(Simulation simulation, MultipartFile file); + + /** + * 对语音文字进行任务 + * + * @param simulation 仿真 + * @param content 语音文字 + * @return 解析结果 + */ + VoiceDiscriminateResult doParse(Simulation simulation, String content); + +} diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceParseServiceImpl.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceParseServiceImpl.java new file mode 100644 index 000000000..344bccef3 --- /dev/null +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceParseServiceImpl.java @@ -0,0 +1,128 @@ +package club.joylink.rtss.services.voice.discriminate; + +import club.joylink.rtss.services.voice.VoiceService; +import club.joylink.rtss.simulation.cbtc.Simulation; +import club.joylink.rtss.simulation.cbtc.discriminate.ParamExtractResult; +import club.joylink.rtss.simulation.cbtc.discriminate.ParamExtractRule; +import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; +import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateRule; +import club.joylink.rtss.vo.client.voice.VoiceRecognitionResult; +import club.joylink.rtss.vo.client.voice.VoiceRecognitionVO; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; +import org.springframework.web.multipart.MultipartFile; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.BiFunction; + +/** + * 语音文件解析服务 + */ +@Service +public class VoiceParseServiceImpl implements VoiceParseService { + + /** + * 语音解析服务 + */ + @Autowired + private VoiceService voiceService; + + /** + * 关键字匹配方法 + */ + private final Map>> keyWordsMatchMethodMap = new HashMap<>(); + + /** + * 关键字匹配接口 + */ + private final BiFunction> defaultKeyWordsMatch = (originContent, ruleInfo) -> null; + + @Override + public VoiceDiscriminateResult doParse(Simulation simulation, MultipartFile file) { + VoiceRecognitionVO vo = VoiceRecognitionVO.load(file); + VoiceRecognitionResult voiceRecognitionResult = voiceService.voiceRecognition(vo); + return doParse(simulation, voiceRecognitionResult.getResult()); + } + + @Override + public VoiceDiscriminateResult doParse(Simulation simulation, String content) { + VoiceDiscriminateResult result = new VoiceDiscriminateResult(); + if (StringUtils.isEmpty(content)) { + return result; + } + result.setSuccess(true); + result.setOriginContent(content); + result.setOriginPinYin(translateToPinYin(content)); + // 匹配指令 + matchCommand(simulation, result); + // 参数抽取 + if (result.isSuccess()) { + paramExtract(simulation, result); + } + return result; + } + + /** + * 翻译标准拼音 + * + * @param content 语音文字 + * @return 拼音信息 + */ + private String translateToPinYin(String content) { + return null; + } + + /** + * 匹配指令 + * + * @param simulation 仿真 + * @param result 结果信息 + */ + private void matchCommand(Simulation simulation, VoiceDiscriminateResult result) { + // 加载本地图的规则信息列表:从数据库中获取 + List ruleList = List.of(new VoiceDiscriminateRule()); + // 匹配指令 + BiFunction> matchMethod = null; + List matchGroupList = null; + for (VoiceDiscriminateRule rule : ruleList) { + matchMethod = keyWordsMatchMethodMap.getOrDefault(rule.getKwMatchMethod(), defaultKeyWordsMatch); + matchGroupList = matchMethod.apply(result.getMatchOriginContent(), rule); + if (!CollectionUtils.isEmpty(matchGroupList)) { + result.setRule(rule); + result.setMatchGroupList(matchGroupList); + return; + } + } + // 出错,未匹配到指令 + result.setSuccess(false); + result.setMsg("未匹配到对应指令"); + } + + /** + * 参数抽取 + * + * @param simulation 仿真 + * @param result 结果信息 + */ + private void paramExtract(Simulation simulation, VoiceDiscriminateResult result) { + List groupList = result.getMatchGroupList(); + List paramsRules = result.getRule().getParamsRules(); + List paramExtractResults = new ArrayList<>(paramsRules.size()); + ParamExtractResult extractResult = null; + for (ParamExtractRule rule : paramsRules) { + if (groupList.size() < rule.getIndex()) { + result.setSuccess(false); + result.setMsg("提取参数出错"); + return; + } + extractResult = rule.getParseRule().matchParam(simulation, groupList.get(rule.getIndex())); + paramExtractResults.add(extractResult); + } + result.setParamExtractResultList(paramExtractResults); + } +} diff --git a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateStepService.java b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceTransactionalService.java similarity index 67% rename from src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateStepService.java rename to src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceTransactionalService.java index 9b70c29c8..8dfdef91a 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceDiscriminateStepService.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceTransactionalService.java @@ -3,7 +3,10 @@ package club.joylink.rtss.services.voice.discriminate; import club.joylink.rtss.simulation.cbtc.discriminate.VoiceDiscriminateResult; import club.joylink.rtss.simulation.cbtc.Simulation; -public interface VoiceDiscriminateStepService { +/** + * 语音业务执行步骤,无返回值不影响其他业务步骤执行 + */ +public interface VoiceTransactionalService { void doExec(VoiceDiscriminateResult result, Simulation simulation); } diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/Stage.java b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/Stage.java deleted file mode 100644 index 30311f03b..000000000 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/Stage.java +++ /dev/null @@ -1,23 +0,0 @@ -package club.joylink.rtss.simulation.cbtc.discriminate; - -import lombok.Getter; - -/** - * 阶段 - */ -@Getter -public enum Stage { - LOAD("加载文件"), - MATCH_COMMAND("匹配语音指令"), - PARAM_EXTRACT("参数提取"), - OPERATE_PARSE("操作解析"), - REPLY_CONTENT("合成回复内容"), - EXEC_COMMAND("执行指令"), - SUCCESS("执行成功"); - - private String desc; - - Stage(String desc) { - this.desc = desc; - } -} diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/VoiceDiscriminateResult.java b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/VoiceDiscriminateResult.java index 640f5d863..ce1a720a7 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/VoiceDiscriminateResult.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/VoiceDiscriminateResult.java @@ -11,24 +11,13 @@ import java.util.List; */ @Data public class VoiceDiscriminateResult { - /** - * 当前所属阶段 - */ - private Stage stage; - /** * 状态是否正常 */ private boolean success; - /** - * 响应提示语 - */ private String msg; - private MultipartFile file; - - /************************* 最初语音识别的结果信息 start *************************/ /** * 语音原始内容 */ @@ -38,9 +27,7 @@ public class VoiceDiscriminateResult { * 原始语音拼音 */ private String originPinYin; - /************************* 最初语音识别的结果信息 end **************************/ - /************************* 根据语音识别的指令信息 start ************************/ /** * 匹配到的指令规则 */ @@ -50,9 +37,7 @@ public class VoiceDiscriminateResult { * 匹配指令结果 */ private List matchGroupList; - /************************* 根据语音识别的指令信息 end *************************/ - /************************* 参数提取结果 start *******************************/ /** * 参数提取集合 */ @@ -63,22 +48,6 @@ public class VoiceDiscriminateResult { */ private List simulationMemberList; - /************************* 参数提取结果 end *********************************/ - - /************************* 合成指令 start **********************************/ - /** - * 合成的指令结果 - */ - private OperateResult operateResult; - /************************* 合成指令 end *************************************/ - - /************************* 合成回复信息 start *******************************/ - /** - * 回复提取内容 - */ - private ReplyResult replyContentResult; - /************************* 合成回复信息 end ********************************/ - /** * 获取匹配指令的语音内容 */