|
|
@@ -0,0 +1,99 @@ |
|
|
|
|
|
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; |
|
|
|
|
|
} |
|
|
|
|
|
} |