|
|
|
@ -16,6 +16,11 @@ import com.supervision.util.TtsUtil;
|
|
|
|
|
import jakarta.annotation.Resource;
|
|
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import net.sourceforge.pinyin4j.PinyinHelper;
|
|
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
|
|
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
|
|
|
|
|
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
|
|
|
|
|
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.util.StopWatch;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
@ -24,6 +29,8 @@ import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Service
|
|
|
|
@ -48,7 +55,7 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
DIFYChatReqInputVO inputs = new DIFYChatReqInputVO();
|
|
|
|
|
stopWatch.start("stt");
|
|
|
|
|
stopWatch.stop();
|
|
|
|
|
difyChatReqVO.setQuery(AsrUtil.asrTransformByBytes(bytes));
|
|
|
|
|
difyChatReqVO.setQuery(replaceTown(AsrUtil.asrTransformByBytes(bytes)));
|
|
|
|
|
difyChatReqVO.setConversation_id(robotTalkReq.getSessionId());
|
|
|
|
|
stopWatch.start("dify");
|
|
|
|
|
ChatResDTO chatResDTO = difyApiUtil.chat(difyChatReqVO);
|
|
|
|
@ -77,4 +84,60 @@ public class ChatServiceImpl implements IChatService {
|
|
|
|
|
log.info("audioId:{}", audioId);
|
|
|
|
|
Base64.decodeToStream(voiceCache.get(audioId), response.getOutputStream(), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检索字符串中的“小镇”,并判断“小镇”前的两个汉字的拼音是否为“menghe”,
|
|
|
|
|
* 如果为true,则替换这四个汉字为“勐赫小镇”,返回更新后的字符串。
|
|
|
|
|
*
|
|
|
|
|
* @param text 输入字符串
|
|
|
|
|
* @return 更新后的字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String replaceTown(String text) {
|
|
|
|
|
// 正则模式:匹配两个汉字紧跟“小镇”
|
|
|
|
|
Pattern pattern = Pattern.compile("([\u4e00-\u9fa5]{2})小镇");
|
|
|
|
|
Matcher matcher = pattern.matcher(text);
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
|
|
|
|
|
// 配置拼音输出格式:小写、无声调
|
|
|
|
|
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
|
|
|
|
|
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
|
|
|
|
|
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
|
|
|
|
|
|
|
|
|
while (matcher.find()) {
|
|
|
|
|
String twoChars = matcher.group(1);
|
|
|
|
|
StringBuilder pinyinStr = new StringBuilder();
|
|
|
|
|
boolean valid = true;
|
|
|
|
|
// 遍历两个汉字,转换为拼音并拼接
|
|
|
|
|
for (int i = 0; i < twoChars.length(); i++) {
|
|
|
|
|
char ch = twoChars.charAt(i);
|
|
|
|
|
try {
|
|
|
|
|
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(ch, format);
|
|
|
|
|
if (pinyinArray != null && pinyinArray.length > 0) {
|
|
|
|
|
pinyinStr.append(pinyinArray[0]);
|
|
|
|
|
} else {
|
|
|
|
|
valid = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} catch (BadHanyuPinyinOutputFormatCombination e) {
|
|
|
|
|
valid = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 如果转换后的拼音为 "menghe",则替换为 "勐赫小镇"
|
|
|
|
|
if (valid && "menghe".contentEquals(pinyinStr)) {
|
|
|
|
|
matcher.appendReplacement(sb, "勐赫小镇");
|
|
|
|
|
} else {
|
|
|
|
|
matcher.appendReplacement(sb, matcher.group(0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
matcher.appendTail(sb);
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 示例测试
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String sampleText = "欢迎来到孟河小镇,体验独特的小镇风情;另外,还有梦和小镇等待你探访。";
|
|
|
|
|
System.out.println(replaceTown(sampleText));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|