From 621cd8befae5f079af919d47742b9fc1098b0fb3 Mon Sep 17 00:00:00 2001 From: weizhihong Date: Mon, 10 Apr 2023 17:13:16 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=AF=AD=E9=9F=B3=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../discriminate/VoiceParseServiceImpl.java | 20 +++++------ .../cbtc/discriminate/ExtractRule.java | 36 ++++++------------- .../discriminate/VoiceDiscriminateResult.java | 32 +++++++++++++++-- 3 files changed, 48 insertions(+), 40 deletions(-) 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 index a921eb89c..c264199fc 100644 --- a/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceParseServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/voice/discriminate/VoiceParseServiceImpl.java @@ -89,10 +89,10 @@ public class VoiceParseServiceImpl implements VoiceParseService { } // 匹配指令 for (VoiceDiscriminateRule rule : ruleList) { - List matchGroupList = keyWordsMatch(rule.getKeyWordRules(), result.getMatchOriginContent()); - if (!CollectionUtils.isEmpty(matchGroupList)) { + Matcher matcher = keyWordsMatch(rule.getKeyWordRules(), result.getMatchOriginContent()); + if (matcher != null) { result.setRule(rule); - result.setMatchGroupList(matchGroupList); + result.setMatcher(matcher); return; } } @@ -107,16 +107,13 @@ public class VoiceParseServiceImpl implements VoiceParseService { * @param content 语音内容 * @return groupList */ - private List keyWordsMatch(String patternStr, String content) { - List groupList = new ArrayList<>(); + private Matcher keyWordsMatch(String patternStr, String content) { Pattern pattern = Pattern.compile(translateToPinYin(patternStr));// 匹配的模式 Matcher matcher = pattern.matcher(content); if (matcher.find()) { - for (int index = 1, size = matcher.groupCount(); index <= size; index++) { - groupList.add(matcher.group(index)); - } + return matcher; } - return groupList; + return null; } /** @@ -126,12 +123,11 @@ public class VoiceParseServiceImpl implements VoiceParseService { * @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; String[] groupStrArr = null; - int groupSize = groupList.size(); + int groupSize = result.getGroupCount(); for (ParamExtractRule rule : paramsRules) { if (rule.getIndexArr() != null) { // 如果定位信息不为空 groupStrArr = new String[rule.getIndexArr().length]; @@ -141,7 +137,7 @@ public class VoiceParseServiceImpl implements VoiceParseService { result.setMsg("提取参数出错"); return; } - groupStrArr[index] = groupList.get(rule.getIndexArr()[index]); + groupStrArr[index] = result.getGroup(rule.getIndexArr()[index]); } } extractResult = new ParamExtractResult(); diff --git a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/ExtractRule.java b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/ExtractRule.java index 5e53e974f..2e24bf275 100644 --- a/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/ExtractRule.java +++ b/src/main/java/club/joylink/rtss/simulation/cbtc/discriminate/ExtractRule.java @@ -28,7 +28,7 @@ public enum ExtractRule { STATION_NAME_EXTRACT("根据车站名称匹配车站") { @Override - public Object matchParam(Simulation simulation, String... sourceStr) { + public Object matchParam(Simulation simulation, String[] sourceStr) { if (sourceStr == null || sourceStr.length == 0) { throw new IllegalArgumentException("定位参数不缺失"); } @@ -36,37 +36,22 @@ public enum ExtractRule { // 这里可能需要做对入参做一些处理 String stationName = sourceStr[0]; return findDevice(simulation, MapElement.DeviceType.STATION,Station.class,stationName); - - /* Optional stationOptional = simulation.getRepository().getStationList().stream() - .filter(station -> Objects.equals(stationName, PinYinUtil.toPinYin(station.getName()))) - .findFirst(); - if (stationOptional.isPresent()) { - return stationOptional.get(); - } - throw new IllegalArgumentException("未找到【" + stationName + "】车站");*/ } }, ROUTE_NAME_EXTRACT("根据进路名称匹配进路") { @Override - public Object matchParam(Simulation simulation, String... sourceStr) { + public Object matchParam(Simulation simulation, String[] sourceStr) { if (sourceStr == null || sourceStr.length == 0) { throw new IllegalArgumentException("定位参数不缺失"); } // 这里可能对X、S做处理 String routeName = sourceStr[0]; return findDevice(simulation, MapElement.DeviceType.ROUTE,Route.class,routeName); -/* Optional routeOptional = simulation.getRepository().getRouteList().stream() - .filter(route -> Objects.equals(route.getName(), routeName)) - .findFirst(); - if (routeOptional.isPresent()) { - return routeOptional.get(); - } - throw new IllegalArgumentException("未找到【" + routeName + "】进路");*/ } }, ROUTE_SIGNAL_EXTRACT("根据起始、终点信号机匹配进路") { @Override - public Object matchParam(Simulation simulation, String... sourceStr) { + public Object matchParam(Simulation simulation, String[] sourceStr) { if (sourceStr == null || sourceStr.length < 2) { throw new IllegalArgumentException("定位参数缺失"); } @@ -85,19 +70,18 @@ public enum ExtractRule { if (routeOptional.isPresent()) { return routeOptional.get(); } -// throw new IllegalArgumentException("未找到【" + startSignalName + "-" + endSignalName + "】进路"); - throw new IllegalArgumentException(String.format("未找到信号机始端[%s-%s],终端[%s-%s]",startSignalName,startSignal.getName(),endSignalName,endSinal.getName())); + throw new IllegalArgumentException(String.format("未找到信号机始端[%s-%s],终端[%s-%s]",startSignalName, startSignal.getName(), endSignalName, endSinal.getName())); } }, STAND_STATION_UP_DOWN_EXTRACT("根据车站、上下行匹配站台") { @Override - public Object matchParam(Simulation simulation, String... sourceStr) { + public Object matchParam(Simulation simulation, String[] sourceStr) { if (sourceStr == null || sourceStr.length < 2) { throw new IllegalArgumentException("定位参数缺失"); } String stationName = sourceStr[0], upDown = sourceStr[1]; - Station station = (Station) STATION_NAME_EXTRACT.matchParam(simulation, stationName); - Boolean right = (Boolean) UP_DOWN_WAY.matchParam(simulation,upDown); + Station station = (Station) STATION_NAME_EXTRACT.matchParam(simulation, new String[] {stationName}); + Boolean right = (Boolean) UP_DOWN_WAY.matchParam(simulation,new String[]{upDown}); List stands = station.getStandOf(right); if(CollectionUtils.isEmpty(stands)){ throw new IllegalArgumentException(String.format("不能获取对应的站台 车站[%s-%s],上下行[%s-%s]",stationName,station.getCode(),upDown,right)); @@ -107,7 +91,7 @@ public enum ExtractRule { } },UP_DOWN_WAY("车辆上下行"){ @Override - public Object matchParam(Simulation simulation, String... sourceStr) { + public Object matchParam(Simulation simulation, String[] sourceStr) { String way = sourceStr[0]; if(StringUtils.containsIgnoreCase(way,"shang")){ return true; @@ -118,7 +102,7 @@ public enum ExtractRule { } },SWITCH_NAME("道岔名称"){ @Override - public Object matchParam(Simulation simulation, String... sourceStr) { + public Object matchParam(Simulation simulation, String[] sourceStr) { String swtichName = sourceStr[0]; return ExtractRule.findDevice(simulation, MapElement.DeviceType.SWITCH,Switch.class,swtichName); } @@ -134,7 +118,7 @@ public enum ExtractRule { this.description = description; } - public abstract Object matchParam(Simulation simulation, String... sourceStr); + public abstract Object matchParam(Simulation simulation, String[] sourceStr); private static MapNamedElement findDevice(Simulation simulation, MapElement.DeviceType dt, Class eleClass,String matchVal){ List eleList = simulation.getRepository().getListByType(dt,eleClass); 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 c6de5f7a0..3a51922e3 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 @@ -4,6 +4,8 @@ import club.joylink.rtss.simulation.cbtc.member.SimulationMember; import lombok.Data; import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; /** * 语音解析结果 @@ -33,9 +35,14 @@ public class VoiceDiscriminateResult { private VoiceDiscriminateRule rule; /** - * 匹配指令结果 + * 原始匹配指令结果 */ - private List matchGroupList; + private Matcher matcher; + + /** + * 纠正过的group map + */ + private Map correctGroupStr; /** * 参数提取集合 @@ -53,4 +60,25 @@ public class VoiceDiscriminateResult { public String getMatchOriginContent() { return this.originPinYin; } + + /** + * 获取匹配到group数量 + * @return 数量 + */ + public int getGroupCount() { + return matcher == null ? 0 : matcher.groupCount(); + } + + /** + * 根据索引获取group 原始 + * + * @param index 位置 + * @return group + */ + public String getGroup(int index) { + if (index > getGroupCount()) { + throw new IllegalArgumentException("out of index"); + } + return matcher.group(index); + } } \ No newline at end of file