| @@ -0,0 +1,21 @@ | |||
| package com.xueyi.nlt.api.nlt.feign; | |||
| import com.alibaba.fastjson2.JSONObject; | |||
| import com.xueyi.nlt.api.nlt.feign.factory.RemoteBaiduNLPFallbackFactory; | |||
| import org.springframework.cloud.openfeign.FeignClient; | |||
| import org.springframework.web.bind.annotation.PostMapping; | |||
| import org.springframework.web.bind.annotation.RequestBody; | |||
| import org.springframework.web.bind.annotation.RequestHeader; | |||
| import org.springframework.web.bind.annotation.RequestParam; | |||
| @FeignClient(url = "${notification.baidunlp.url}",name = "baidu-nlp", fallbackFactory = RemoteBaiduNLPFallbackFactory.class) | |||
| public interface RemoteBaiduNLPService { | |||
| @PostMapping(path = "/oauth/2.0/token", consumes = "application/x-www-form-urlencoded") | |||
| JSONObject getAccessToken(@RequestBody String body); | |||
| @PostMapping(path = "/rpc/2.0/nlp/v2/dnnlm_cn", consumes = "application/json") | |||
| JSONObject detected(@RequestParam(value = "charset", defaultValue = "UTF-8") String charset, | |||
| @RequestParam("access_token") String accessToken, | |||
| @RequestBody String body); | |||
| } | |||
| @@ -0,0 +1,15 @@ | |||
| package com.xueyi.nlt.api.nlt.feign.factory; | |||
| import com.xueyi.nlt.api.nlt.feign.RemoteBaiduNLPService; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| import org.springframework.cloud.openfeign.FallbackFactory; | |||
| import org.springframework.stereotype.Component; | |||
| @Slf4j | |||
| @Component | |||
| public class RemoteBaiduNLPFallbackFactory implements FallbackFactory<RemoteBaiduNLPService> { | |||
| @Override | |||
| public RemoteBaiduNLPService create(Throwable cause) { | |||
| return null; | |||
| } | |||
| } | |||
| @@ -5,6 +5,5 @@ import lombok.Data; | |||
| @Data | |||
| public class WordProcessVo{ | |||
| private String metadata; | |||
| private String method; | |||
| private String processedResult; | |||
| } | |||
| @@ -1,10 +0,0 @@ | |||
| package com.xueyi.nlt.nlt.manager; | |||
| import com.xueyi.nlt.nlt.domain.vo.WordProcessVo; | |||
| public interface IDmWordProcessManager { | |||
| public WordProcessVo semanticIntegrityDetection(WordProcessVo wordProcessVo); | |||
| public WordProcessVo split(WordProcessVo wordProcessVo); | |||
| } | |||
| @@ -1,99 +0,0 @@ | |||
| package com.xueyi.nlt.nlt.manager.impl; | |||
| import com.alibaba.fastjson2.JSONObject; | |||
| import com.baomidou.mybatisplus.core.toolkit.StringUtils; | |||
| import com.xueyi.nlt.nlt.config.BaiduConfig; | |||
| import com.xueyi.nlt.nlt.domain.vo.WordProcessVo; | |||
| import com.xueyi.nlt.nlt.manager.IDmWordProcessManager; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Component; | |||
| import okhttp3.*; | |||
| import java.io.*; | |||
| @Component | |||
| public class DmWordProcessManager implements IDmWordProcessManager { | |||
| @Autowired | |||
| private BaiduConfig baiduConfig; | |||
| static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build(); | |||
| private static final Logger log = LoggerFactory.getLogger(DmWordProcessManager.class); | |||
| @Override | |||
| public WordProcessVo semanticIntegrityDetection(WordProcessVo wordProcessVo) { | |||
| String result = detected(wordProcessVo.getMetadata()); | |||
| log.info("语义完整性检测结果:" + result); | |||
| if(result != null){ | |||
| JSONObject jsonObject = JSONObject.parseObject(result); | |||
| if(jsonObject.containsKey("error_code")){ | |||
| String errorMsg = String.format("语义完整性检测失败,错误码:%d,错误信息:%s", jsonObject.getInteger("error_code"),jsonObject.getString("error_msg")); | |||
| log.error(errorMsg); | |||
| }else{ | |||
| Double ppl = jsonObject.getDouble("ppl"); | |||
| if(ppl < 300) { | |||
| wordProcessVo.setProcessedResult("2"); | |||
| }else if(ppl > 1000){ | |||
| wordProcessVo.setProcessedResult("0"); | |||
| }else{ | |||
| wordProcessVo.setProcessedResult("1"); | |||
| } | |||
| log.info("语义完整性检测结果:" + wordProcessVo.getProcessedResult()); | |||
| } | |||
| }else{ | |||
| log.error("语义完整性检测失败,未获取到返回结果"); | |||
| } | |||
| return wordProcessVo; | |||
| } | |||
| private String detected(String question) { | |||
| MediaType mediaType = MediaType.parse("application/json"); | |||
| RequestBody body = RequestBody.create(mediaType, "{\"text\":\"" + question + "\"}"); | |||
| String accessToken = getAccessToken(); | |||
| if(StringUtils.isNotEmpty(accessToken)){ | |||
| Request request = new Request.Builder() | |||
| .url(baiduConfig.getDnnUrl() + accessToken) | |||
| .method("POST", body) | |||
| .addHeader("Content-Type", "application/json") | |||
| .addHeader("Accept", "application/json") | |||
| .build(); | |||
| try(Response response = HTTP_CLIENT.newCall(request).execute();) { | |||
| if(response.body() != null) { | |||
| return response.body().string(); | |||
| }else{ | |||
| return null; | |||
| } | |||
| }catch (IOException e){ | |||
| return null; | |||
| } | |||
| }else{ | |||
| return null; | |||
| } | |||
| } | |||
| private String getAccessToken(){ | |||
| MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); | |||
| RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + baiduConfig.getApiKey() | |||
| + "&client_secret=" + baiduConfig.getSecretKey()); | |||
| Request request = new Request.Builder() | |||
| .url(baiduConfig.getAuthUrl()) | |||
| .method("POST", body) | |||
| .addHeader("Content-Type", "application/x-www-form-urlencoded") | |||
| .build(); | |||
| try (Response response = HTTP_CLIENT.newCall(request).execute();){ | |||
| if(response.body() != null){ | |||
| String result = response.body().string(); | |||
| return JSONObject.parseObject(result).getString("access_token"); | |||
| }else{ | |||
| return null; | |||
| } | |||
| }catch (IOException e) { | |||
| return null; | |||
| } | |||
| } | |||
| @Override | |||
| public WordProcessVo split(WordProcessVo dmWordProcessVo){ | |||
| return null; | |||
| } | |||
| } | |||
| @@ -3,5 +3,5 @@ package com.xueyi.nlt.nlt.service; | |||
| import com.xueyi.nlt.nlt.domain.vo.WordProcessVo; | |||
| public interface IDmWordProcessService { | |||
| public WordProcessVo handle (WordProcessVo dmWordProcessVo); | |||
| public WordProcessVo semanticIntegrityDetection (WordProcessVo dmWordProcessVo); | |||
| } | |||
| @@ -1,8 +1,9 @@ | |||
| package com.xueyi.nlt.nlt.service.impl; | |||
| import com.alibaba.fastjson2.JSONObject; | |||
| import com.xueyi.nlt.api.nlt.feign.RemoteBaiduNLPService; | |||
| import com.xueyi.nlt.nlt.config.BaiduConfig; | |||
| import com.xueyi.nlt.nlt.domain.vo.WordProcessVo; | |||
| import com.xueyi.nlt.nlt.manager.IDmWordProcessManager; | |||
| import com.xueyi.nlt.nlt.manager.impl.DmWordProcessManager; | |||
| import com.xueyi.nlt.nlt.service.IDmWordProcessService; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| @@ -11,18 +12,40 @@ import org.springframework.stereotype.Service; | |||
| @Service | |||
| public class DmWordProcessServiceImpl implements IDmWordProcessService { | |||
| @Autowired | |||
| IDmWordProcessManager dmWordProcessManager; | |||
| RemoteBaiduNLPService remoteBaiduNLPService; | |||
| private static final Logger log = LoggerFactory.getLogger(DmWordProcessServiceImpl.class); | |||
| @Autowired | |||
| private BaiduConfig baiduConfig; | |||
| public WordProcessVo handle (WordProcessVo wordProcessVo){ | |||
| log.info("wordProcessVo.getMethod() = " + wordProcessVo.getMethod() + " wordProcessVo.getMetadata() = " + wordProcessVo.getMetadata()); | |||
| return switch (wordProcessVo.getMethod()) { | |||
| case "integrity" -> dmWordProcessManager.semanticIntegrityDetection(wordProcessVo); | |||
| case "split" -> dmWordProcessManager.split(wordProcessVo); | |||
| default -> null; | |||
| }; | |||
| private static final Logger log = LoggerFactory.getLogger(DmWordProcessServiceImpl.class); | |||
| public WordProcessVo semanticIntegrityDetection (WordProcessVo wordProcessVo){ | |||
| log.info("元数据:" + wordProcessVo.getMetadata()); | |||
| String body = "grant_type=client_credentials&client_id=" + baiduConfig.getApiKey() + "&client_secret=" + baiduConfig.getSecretKey(); | |||
| JSONObject accessJson = remoteBaiduNLPService.getAccessToken(body); | |||
| JSONObject result = remoteBaiduNLPService.detected("UTF-8", accessJson.getString("access_token"), "{\"text\":\"" + wordProcessVo.getMetadata() + "\"}"); | |||
| if(result.containsKey("ppl")){ | |||
| if(result != null){ | |||
| if(result.containsKey("error_code")){ | |||
| String errorMsg = String.format("语义完整性检测失败,错误码:%d,错误信息:%s", result.getInteger("error_code"),result.getString("error_msg")); | |||
| log.error(errorMsg); | |||
| }else{ | |||
| Double ppl = result.getDouble("ppl"); | |||
| if(ppl < 300) { | |||
| wordProcessVo.setProcessedResult("2"); | |||
| }else if(ppl > 1000){ | |||
| wordProcessVo.setProcessedResult("0"); | |||
| }else{ | |||
| wordProcessVo.setProcessedResult("1"); | |||
| } | |||
| log.info("语义完整性检测结果:" + wordProcessVo.getProcessedResult()); | |||
| } | |||
| }else{ | |||
| log.error("语义完整性检测失败,未获取到返回结果"); | |||
| } | |||
| } | |||
| return wordProcessVo; | |||
| } | |||
| } | |||