【语音识别处理纠正后的信息】

This commit is contained in:
weizhihong 2023-04-11 10:36:31 +08:00
parent 621cd8befa
commit d967a3c80f
4 changed files with 254 additions and 50 deletions

View File

@ -81,7 +81,6 @@ public class VoiceParseServiceImpl implements VoiceParseService {
private void matchCommand(Simulation simulation, VoiceDiscriminateResult result) {
// 加载本地图的规则信息列表从数据库中获取
List<VoiceDiscriminateRule> ruleList = simulation.getVoiceRuleList();
// BusinessExceptionAssertEnum.VOICE_COMMAND_CONFIG_NULL.assertTrue(!CollectionUtils.isEmpty(ruleList),"该线路没有语音配置的数据");
if(CollectionUtils.isEmpty(ruleList)){
result.setSuccess(false);
result.setMsg("该线路没有语音配置的数据");
@ -125,25 +124,53 @@ public class VoiceParseServiceImpl implements VoiceParseService {
private void paramExtract(Simulation simulation, VoiceDiscriminateResult result) {
List<ParamExtractRule> paramsRules = result.getRule().getParamsRules();
List<ParamExtractResult> paramExtractResults = new ArrayList<>(paramsRules.size());
ParamExtractResult extractResult = null;
String[] groupStrArr = null;
int groupSize = result.getGroupCount();
ParamExtractResult extractResult = null;
String[] originGroupArr = null, correctGroupArr = null;
for (ParamExtractRule rule : paramsRules) {
if (rule.getIndexArr() != null) { // 如果定位信息不为空
groupStrArr = new String[rule.getIndexArr().length];
for (int index = 0, len = groupStrArr.length; index < len; index ++) {
originGroupArr = new String[rule.getIndexArr().length];
correctGroupArr = new String[rule.getIndexArr().length];
for (int index = 0, len = originGroupArr.length; index < len; index ++) {
if (groupSize < rule.getIndexArr()[index]) {
result.setSuccess(false);
result.setMsg("提取参数出错");
return;
}
groupStrArr[index] = result.getGroup(rule.getIndexArr()[index]);
originGroupArr[index] = result.getGroup(rule.getIndexArr()[index]);
correctGroupArr[index] = result.getCorrectGroup(rule.getIndexArr()[index]);
}
} else { // 如果定位信息为空则将参数置空
originGroupArr = null;
correctGroupArr = null;
}
extractResult = new ParamExtractResult();
extractResult.setValue(rule.getParseRule().matchParam(simulation, groupStrArr));
extractResult = rule.getParseRule().matchParam(simulation, originGroupArr, correctGroupArr);
paramExtractResults.add(extractResult);
// 对已纠正过的数据进行记录 TODO
handleCorrectGroup(result, extractResult, originGroupArr, rule.getIndexArr());
}
result.setParamExtractResultList(paramExtractResults);
}
/**
* 处理纠正过的Group信息
* @param result 最终结果新
* @param extractResult 处理过的参数信息
* @param originArr 原始group数组
* @param indexArr group对应索引
*/
private void handleCorrectGroup(VoiceDiscriminateResult result, ParamExtractResult extractResult, String[] originArr, Integer[] indexArr) {
if (indexArr == null || extractResult.getCorrectGroupMap() == null) {
return;
}
String origin = null, correct = null;
for (int index = 0, len = indexArr.length; index < len; index++) {
origin = originArr[index];
correct = extractResult.getCorrectGroupMap().get(origin);
if (StringUtils.hasText(correct)) {
result.setCorrectGroup(indexArr[index], correct);
}
}
}
}

View File

@ -28,83 +28,172 @@ public enum ExtractRule {
STATION_NAME_EXTRACT("根据车站名称匹配车站") {
@Override
public Object matchParam(Simulation simulation, String[] sourceStr) {
if (sourceStr == null || sourceStr.length == 0) {
public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
if (originArr == null || originArr.length == 0) {
throw new IllegalArgumentException("定位参数不缺失");
}
ParamExtractResult result = new ParamExtractResult();
MapNamedElement mapNamedElement = null;
if (correctArr != null && correctArr[0] != null) { // 有正确值直接匹配
mapNamedElement = findDeviceByCorrect(simulation, MapElement.DeviceType.STATION, correctArr[0]);
} else {
// 这里可能需要做对入参做一些处理
String stationName = sourceStr[0];
return findDevice(simulation, MapElement.DeviceType.STATION,Station.class,stationName);
String stationName = originArr[0];
mapNamedElement = findDevice(simulation, MapElement.DeviceType.STATION, Station.class, stationName);
if (mapNamedElement != null) { // 不为空将信息放入纠错信息中
result.setCorrectGroupMap(stationName, mapNamedElement.getName());
}
}
if (mapNamedElement != null) {
result.setValue(mapNamedElement);
return result;
}
throw new IllegalArgumentException(String.format("未找到车站[%s]", originArr[0]));
}
},
ROUTE_NAME_EXTRACT("根据进路名称匹配进路") {
@Override
public Object matchParam(Simulation simulation, String[] sourceStr) {
if (sourceStr == null || sourceStr.length == 0) {
public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
if (originArr == null || originArr.length == 0) {
throw new IllegalArgumentException("定位参数不缺失");
}
ParamExtractResult result = new ParamExtractResult();
MapNamedElement mapNamedElement = null;
if (correctArr != null && correctArr[0] != null) { // 有正确值直接匹配
mapNamedElement = findDeviceByCorrect(simulation, MapElement.DeviceType.ROUTE, correctArr[0]);
} else {
// 这里可能对XS做处理
String routeName = sourceStr[0];
return findDevice(simulation, MapElement.DeviceType.ROUTE,Route.class,routeName);
String routeName = originArr[0];
mapNamedElement = findDevice(simulation, MapElement.DeviceType.ROUTE, Route.class, routeName);
if (mapNamedElement != null) { // 不为空将信息放入纠错信息中
result.setCorrectGroupMap(routeName, mapNamedElement.getName());
}
}
if (mapNamedElement != null) {
result.setValue(mapNamedElement);
return result;
}
throw new IllegalArgumentException(String.format("未找到进路[%s]", originArr[0]));
}
},
ROUTE_SIGNAL_EXTRACT("根据起始、终点信号机匹配进路") {
@Override
public Object matchParam(Simulation simulation, String[] sourceStr) {
if (sourceStr == null || sourceStr.length < 2) {
public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
if (originArr == null || originArr.length < 2) {
throw new IllegalArgumentException("定位参数缺失");
}
String startSignalName = sourceStr[0], endSignalName = sourceStr[1];
MapNamedElement startSignal = ExtractRule.findDevice(simulation, MapElement.DeviceType.SIGNAL,Signal.class,startSignalName);
MapNamedElement endSinal = ExtractRule.findDevice(simulation, MapElement.DeviceType.SIGNAL,Signal.class,endSignalName);
ParamExtractResult result = new ParamExtractResult();
// 查找起始信号机
MapNamedElement startSignal = null;
if (correctArr != null && correctArr[0] != null) { // 有正确值直接匹配
startSignal = findDeviceByCorrect(simulation, MapElement.DeviceType.SIGNAL, correctArr[0]);
} else {
String signalName = originArr[0];
startSignal = findDevice(simulation, MapElement.DeviceType.SIGNAL, Signal.class, signalName);
if (startSignal != null) { // 不为空将信息放入纠错信息中
result.setCorrectGroupMap(signalName, startSignal.getName());
}
}
// 查找终端信号机
MapNamedElement endSignal = null;
if (correctArr != null && correctArr[1] != null) { // 有正确值直接匹配
endSignal = findDeviceByCorrect(simulation, MapElement.DeviceType.SIGNAL, correctArr[1]);
} else {
String signalName = originArr[1];
endSignal = findDevice(simulation, MapElement.DeviceType.SIGNAL, Signal.class, signalName);
if (endSignal != null) { // 不为空将信息放入纠错信息中
result.setCorrectGroupMap(signalName, endSignal.getName());
}
}
if (startSignal == null || endSignal == null) {
return result;
}
String startCode = startSignal.getCode(), endCode = endSignal.getCode();
// 查找进路
Optional<Route> routeOptional = simulation.getRepository().getRouteList().stream()
.filter(route -> {
if (route.getStart() == null || route.getDestination() == null) {
return false;
}
return Objects.equals(startSignal.getCode(), route.getStart().getCode())
|| Objects.equals(endSinal, route.getDestination().getCode());
return Objects.equals(startCode, route.getStart().getCode()) || Objects.equals(endCode, route.getDestination().getCode());
}).findFirst();
if (routeOptional.isPresent()) {
return routeOptional.get();
result.setValue(routeOptional.get());
return result;
}
throw new IllegalArgumentException(String.format("未找到信号机始端[%s-%s],终端[%s-%s]",startSignalName, startSignal.getName(), endSignalName, endSinal.getName()));
throw new IllegalArgumentException(String.format("未找到信号机始端[%s],终端[%s]", originArr[0], originArr[1]));
}
},
STAND_STATION_UP_DOWN_EXTRACT("根据车站、上下行匹配站台") {
@Override
public Object matchParam(Simulation simulation, String[] sourceStr) {
if (sourceStr == null || sourceStr.length < 2) {
public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
if (originArr == null || originArr.length < 2) {
throw new IllegalArgumentException("定位参数缺失");
}
String stationName = sourceStr[0], upDown = sourceStr[1];
Station station = (Station) STATION_NAME_EXTRACT.matchParam(simulation, new String[] {stationName});
Boolean right = (Boolean) UP_DOWN_WAY.matchParam(simulation,new String[]{upDown});
List<Stand> stands = station.getStandOf(right);
ParamExtractResult result = new ParamExtractResult();
// 车站匹配结果
ParamExtractResult stationResult = STATION_NAME_EXTRACT.matchParam(simulation, new String[] { originArr[0] }, new String[] { correctArr[0] });
result.setCorrectGroupMap(stationResult.getCorrectGroupMap());
// 上行匹配结果
ParamExtractResult upDownResult = UP_DOWN_WAY.matchParam(simulation, new String[] { originArr[1] }, new String[] { correctArr[1] });
result.setCorrectGroupMap(upDownResult.getCorrectGroupMap());
// 获取站台列表
List<Stand> stands = ((Station) stationResult.getValue()).getStandOf((Boolean) upDownResult.getValue());
if(CollectionUtils.isEmpty(stands)){
throw new IllegalArgumentException(String.format("不能获取对应的站台 车站[%s-%s],上下行[%s-%s]",stationName,station.getCode(),upDown,right));
throw new IllegalArgumentException(String.format("不能获取对应的站台 车站[%s],上下行[%s]",originArr[0], originArr[1]));
}
return station.getStandOf(right).get(0);
result.setValue(stands.get(0));
return result;
}
},UP_DOWN_WAY("车辆上下行"){
},
UP_DOWN_WAY("车辆上下行"){
@Override
public Object matchParam(Simulation simulation, String[] sourceStr) {
String way = sourceStr[0];
if(StringUtils.containsIgnoreCase(way,"shang")){
return true;
public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
if (originArr == null || originArr.length == 0) {
throw new IllegalArgumentException("定位参数缺失");
}
ParamExtractResult result = new ParamExtractResult();
if (correctArr != null && correctArr[0] != null) { // 有正确值直接匹配
result.setValue(correctArr[0]);
} else {
String way = originArr[0];
if(StringUtils.containsIgnoreCase(way,"shang")) {
result.setValue(true);
result.setCorrectGroupMap(way, "");
}else if(StringUtils.containsIgnoreCase(way,"xia")){
return false;
result.setValue(false);
result.setCorrectGroupMap(way, "");
}
throw new IllegalArgumentException(String.format("不能解析上下行[%s]",way));
}
},SWITCH_NAME("道岔名称"){
throw new IllegalArgumentException(String.format("不能解析上下行[%s]", originArr[0]));
}
},
SWITCH_NAME("道岔名称"){
@Override
public Object matchParam(Simulation simulation, String[] sourceStr) {
String swtichName = sourceStr[0];
return ExtractRule.findDevice(simulation, MapElement.DeviceType.SWITCH,Switch.class,swtichName);
public ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr) {
if (originArr == null || originArr.length == 0) {
throw new IllegalArgumentException("定位参数缺失");
}
ParamExtractResult result = new ParamExtractResult();
// 查找起始信号机
MapNamedElement switchElement = null;
if (correctArr != null && correctArr[0] != null) { // 有正确值直接匹配
switchElement = findDeviceByCorrect(simulation, MapElement.DeviceType.SWITCH, correctArr[0]);
} else {
String switchName = originArr[0];
switchElement = findDevice(simulation, MapElement.DeviceType.SWITCH, Switch.class, switchName);
if (switchElement != null) { // 不为空将信息放入纠错信息中
result.setCorrectGroupMap(switchName, switchElement.getName());
}
}
if (switchElement != null) {
result.setValue(switchElement);
return result;
}
throw new IllegalArgumentException(String.format("未找到道岔[%s]", originArr[0]));
}
},
;
@ -118,7 +207,15 @@ public enum ExtractRule {
this.description = description;
}
public abstract Object matchParam(Simulation simulation, String[] sourceStr);
/**
* 解析参数
*
* @param simulation 仿真信息
* @param originArr 原始的group信息
* @param correctArr 已经纠正过的group信息
* @return 解析结果
*/
public abstract ParamExtractResult matchParam(Simulation simulation, String[] originArr, String[] correctArr);
private static MapNamedElement findDevice(Simulation simulation, MapElement.DeviceType dt, Class<? extends MapNamedElement> eleClass,String matchVal){
List<? extends MapNamedElement> eleList = simulation.getRepository().getListByType(dt,eleClass);
@ -135,4 +232,21 @@ public enum ExtractRule {
}
return nameEle;
}
/**
* 根据正确的名称信息获取设备信息
*
* @param simulation 仿真
* @param type 设备类型
* @param matchVal 匹配信息
* @return 设备信息
*/
private static MapNamedElement findDeviceByCorrect(Simulation simulation, MapElement.DeviceType type, String matchVal) {
return simulation.getRepository().getDeviceMap().values().stream()
.filter(mapElement -> Objects.equals(mapElement.getDeviceType(), type))
.map(mapElement -> (MapNamedElement) mapElement)
.filter(mapElement -> Objects.equals(mapElement.getName(), matchVal))
.findFirst().orElse(null);
}
}

View File

@ -1,6 +1,11 @@
package club.joylink.rtss.simulation.cbtc.discriminate;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
/**
* 参数提取结果
@ -8,9 +13,45 @@ import lombok.Data;
@Data
public class ParamExtractResult {
/**
* 矫正后的group信息
* key为传入信息
*/
private Map<String, String> correctGroupMap;
/**
* 参数解析出的相关数据
*/
private Object value;
/**
* 放入纠正结果
*
* @param origin 原始信息
* @param correct 纠错信息
*/
public void setCorrectGroupMap(String origin, String correct) {
if (StringUtils.isEmpty(origin) || StringUtils.isEmpty(correct)) {
return;
}
if (correctGroupMap == null) {
correctGroupMap = new HashMap<>();
}
correctGroupMap.put(origin, correct);
}
/**
* 放入纠正结果
*
* @param correctGroupMap 纠正过的map信息
*/
public void setCorrectGroupMap(Map<String, String> correctGroupMap) {
if (CollectionUtils.isEmpty(correctGroupMap)) {
return;
}
if (this.correctGroupMap == null) {
this.correctGroupMap = new HashMap<>();
}
this.correctGroupMap.putAll(correctGroupMap);
}
}

View File

@ -3,6 +3,7 @@ package club.joylink.rtss.simulation.cbtc.discriminate;
import club.joylink.rtss.simulation.cbtc.member.SimulationMember;
import lombok.Data;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
@ -81,4 +82,25 @@ public class VoiceDiscriminateResult {
}
return matcher.group(index);
}
/**
* 根据索引获取正确的group
* @return
*/
public String getCorrectGroup(int index) {
return this.correctGroupStr == null ? null : this.correctGroupStr.get(index);
}
/**
* 给正确MAP中赋值
*
* @param index GROUP 索引
* @param correct 正确值
*/
public void setCorrectGroup(Integer index, String correct) {
if (this.correctGroupStr == null) {
this.correctGroupStr = new HashMap<>();
}
this.correctGroupStr.put(index, correct);
}
}