【语音识别拼音处理逻辑修改】

【根据语音识别Group索引获取原位置字符串】
This commit is contained in:
weizhihong 2023-04-11 13:24:21 +08:00
parent 2890997cdd
commit f750e8fd9d
3 changed files with 67 additions and 7 deletions

View File

@ -52,7 +52,8 @@ public class VoiceParseServiceImpl implements VoiceParseService {
}
result.setSuccess(true);
result.setOriginContent(content);
result.setOriginPinYin(translateToPinYin(content));
// 处理拼音信息
translateToPinYin(result, content);
// 匹配指令
matchCommand(simulation, result);
// 参数抽取
@ -68,8 +69,15 @@ public class VoiceParseServiceImpl implements VoiceParseService {
* @param content 语音文字
* @return 拼音信息
*/
private String translateToPinYin(String content) {
return PinYinUtil.toPinYin(content);
private void translateToPinYin(VoiceDiscriminateResult result, String content) {
char[] chars = content.toCharArray();
String[] pinYinArr = new String[chars.length];
String pinYin = null;
for (int index = 0, size = chars.length; index < size; index++) {
pinYin = PinYinUtil.toPinYin(chars[index]);
pinYinArr[index] = pinYin;
}
result.setContentPinYinArr(pinYinArr);
}
/**
@ -107,7 +115,7 @@ public class VoiceParseServiceImpl implements VoiceParseService {
* @return groupList
*/
private Matcher keyWordsMatch(String patternStr, String content) {
Pattern pattern = Pattern.compile(translateToPinYin(patternStr));// 匹配的模式
Pattern pattern = Pattern.compile(PinYinUtil.toPinYin(patternStr));// 匹配的模式
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
return matcher;

View File

@ -2,10 +2,12 @@ package club.joylink.rtss.simulation.cbtc.discriminate;
import club.joylink.rtss.simulation.cbtc.member.SimulationMember;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
/**
@ -26,9 +28,9 @@ public class VoiceDiscriminateResult {
private String originContent;
/**
* 原始语音拼音
* 原始语音拼音数组
*/
private String originPinYin;
private String[] contentPinYinArr;
/**
* 匹配到的指令规则
@ -59,7 +61,10 @@ public class VoiceDiscriminateResult {
* 获取匹配指令的语音内容
*/
public String getMatchOriginContent() {
return this.originPinYin;
if (this.contentPinYinArr != null) {
return String.join(StringUtils.EMPTY, this.contentPinYinArr);
}
return this.originContent;
}
/**
@ -103,4 +108,33 @@ public class VoiceDiscriminateResult {
}
this.correctGroupStr.put(index, correct);
}
/**
* 根据拼音Group的Index获取字符串在原内容中定位信息
*
* @param index GROUP索引
* @return 起始位置终止位置
*/
public int[] getPinYinGroupAtOriginStartAndEndIndex(int index) {
String group = getGroup(index); // 获取到group
int[] indexArr = new int[2]; // 索引数组
int readIndex = 0;
String pinYin = null;
for (int i = 0, len = contentPinYinArr.length; i < len; i ++) {
pinYin = contentPinYinArr[i];
if (Objects.equals(group.substring(readIndex, readIndex + pinYin.length()), pinYin)) {
if (readIndex == 0) {
indexArr[0] = i;
}
readIndex = readIndex + pinYin.length();
} else {
readIndex = 0;
}
if (readIndex == group.length()) { // 结束位置
indexArr[1] = i + 1;
break;
}
}
return indexArr;
}
}

View File

@ -53,4 +53,22 @@ public class PinYinUtil {
}
return sb.toString();
}
/**
*
* @param c
* @return
*/
public static String toPinYin(char c) {
if(Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
try {
String[] ss = PinyinHelper.toHanyuPinyinStringArray(c,PIN_YIN_FORMAT);
return ss[0];
} catch (BadHanyuPinyinOutputFormatCombination e) {
throw new IllegalArgumentException(e);
}
} else {
return String.valueOf(c);
}
}
}