增加拼音匹配

This commit is contained in:
wulin 2023-10-29 11:17:52 +08:00
parent d4b58636a8
commit 5978e32094
3 changed files with 37 additions and 0 deletions

View File

@ -80,6 +80,13 @@
<artifactId>spring-cloud-context</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
<build>

View File

@ -14,6 +14,9 @@ import com.qiuguo.iot.third.nlp.action.Action;
import com.qiuguo.iot.third.nlp.action.Actions;
import com.qiuguo.iot.third.nlp.entity.SystemTalkKeyAndDeviceName;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
@ -51,6 +54,8 @@ public class NlpService {
List<SystemTalkAnswerConfigEntity> systemIncluds = new ArrayList<>();
List<SystemTalkKeyAndDeviceName> systemTalkKeyAndDeviceNameList = new ArrayList<>();
int i = 0;
for(SystemTalkAnswerConfigEntity entity : systemTalkAnswerConfigService.getCommandList()){
SystemTalkKeyAndDeviceName systemTalkKeyAndDeviceName = new SystemTalkKeyAndDeviceName();
@ -82,6 +87,7 @@ public class NlpService {
String pText = text;
i = 0;
int j = 0;
Boolean isFinding = false;
for(SystemTalkKeyAndDeviceName sysTalkKeyAndDeviceName :systemTalkKeyAndDeviceNameList){
if(pText.indexOf(sysTalkKeyAndDeviceName.getKey()) >= 0){
//找到对应的名称
@ -89,10 +95,12 @@ public class NlpService {
//用户绑定设备名称
pText = pText.replace(sysTalkKeyAndDeviceName.getKey(), ">" + (i++) + ">");
includs.add(deviceUserBindEntityPagerResult.getData().get(sysTalkKeyAndDeviceName.getIndex()));
isFinding = true;
}else{// if(sysTalkKeyAndDeviceName.getType().equals(KeyTypeEnum.SYSTEM_COMMAND.getCode())){
//是系统自定义动词后的名称
pText = pText.replace(sysTalkKeyAndDeviceName.getKey(), "#" + (j++) + "#");
systemIncluds.add(systemTalkAnswerConfigService.getCommandList().get(sysTalkKeyAndDeviceName.getIndex()));
isFinding = true;
}/*else{
//秋果专有名称
pText = pText.replace(sysTalkKeyAndDeviceName.getKey(), "<" + (j++) + "<");
@ -100,6 +108,25 @@ public class NlpService {
}*/
}
}
if(!isFinding){
HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
try {
String pinyinArray = PinyinHelper.toHanYuPinyinString(text, hanyuPinyinOutputFormat, null, false).replaceAll("null", "");
log.info("拼音结果:{}", pinyinArray);
for(SystemTalkKeyAndDeviceName sysTalkKeyAndDeviceName :systemTalkKeyAndDeviceNameList){
if(pinyinArray.indexOf(sysTalkKeyAndDeviceName.getKey()) >= 0){
if(sysTalkKeyAndDeviceName.getType().equals(KeyTypeEnum.QIUGUO_NAME.getCode())){
pinyinArray = pinyinArray.replace(sysTalkKeyAndDeviceName.getKey(), "#" + (j++) + "#");
systemIncluds.add(systemTalkAnswerConfigService.getCommandList().get(sysTalkKeyAndDeviceName.getIndex()));
return getActions(pinyinArray, text, includs, systemIncluds, type);
}
}
}
}catch (Exception e) {
log.info("获取拼音异常");
}
}
return getActions(pText, text, includs, systemIncluds, type);
});
@ -110,6 +137,7 @@ public class NlpService {
List<DeviceUserBindEntity> includs,
List<SystemTalkAnswerConfigEntity> commands,
String type) {
return liguoNlpService.geSingletNlp(text, type).map(nlp -> {
Actions actions = new Actions();
actions.setActions(new ArrayList<>());

View File

@ -94,6 +94,8 @@
<version>4.6.4</version>
</dependency>
</dependencies>
<build>