Browse Source

ScanContentUtils新增voiceResults方法

dev_shibei_match
yinzuomei 5 years ago
parent
commit
e828d1d51f
  1. 50
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/scan/result/VoiceResultDTO.java
  2. 47
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ScanContentUtils.java
  3. 6
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/common/constant/SysConstant.java
  4. 66
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/common/enu/LabelEnum.java
  5. 46
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanServiceImpl.java
  6. 52
      epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResult.java

50
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/scan/result/VoiceResultDTO.java

@ -0,0 +1,50 @@
package com.epmet.commons.tools.scan.result;
import lombok.Data;
import java.io.Serializable;
/**
* 检测成功检测成功+检测失败的的任务详情
* 处理中的该接口不返回继续轮询即可
* @author yinzuomei@elink-cn.com
* @date 2020/12/18 13:10
*/
@Data
public class VoiceResultDTO implements Serializable {
private static final long serialVersionUID = 8006827312407034344L;
/**
* 检测对象对应的数据ID
*/
private String dataId;
/**
* 检测任务的ID
*/
private String taskId;
/**
* 建议您执行的后续操作取值
* pass结果正常无需进行其余操作
* review结果不确定需要进行人工审核
* block结果违规建议直接删除或者限制公开
*/
private String suggestion;
/**
* 检测结果的分类取值
* normal正常文本
* spam含垃圾信息
* ad广告
* politics涉政
* terrorism暴恐
* abuse辱骂
* porn色情
* flood灌水
* contraband违禁
* meaningless无意义
* customized自定义例如命中自定义关键词
*/
private String label;
private String labelDesc;
}

47
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/ScanContentUtils.java

@ -2,13 +2,16 @@ package com.epmet.commons.tools.utils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.scan.param.*; import com.epmet.commons.tools.scan.param.*;
import com.epmet.commons.tools.scan.result.AsyncScanResult; import com.epmet.commons.tools.scan.result.AsyncScanResult;
import com.epmet.commons.tools.scan.result.SyncScanResult; import com.epmet.commons.tools.scan.result.SyncScanResult;
import com.epmet.commons.tools.scan.result.VoiceResultDTO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -77,7 +80,7 @@ public class ScanContentUtils {
/** /**
* desc:语音异步检测任务提交 * desc:语音异步检测任务提交
* *
* @return * @return 返回检测对象对应的任务id
*/ */
public static Result<AsyncScanResult> voiceAsyncScan(String url, VoiceScanParamDTO param){ public static Result<AsyncScanResult> voiceAsyncScan(String url, VoiceScanParamDTO param){
log.debug("voiceAsyncScan param:{}", JSON.toJSONString(param)); log.debug("voiceAsyncScan param:{}", JSON.toJSONString(param));
@ -103,12 +106,44 @@ public class ScanContentUtils {
} }
} }
/**
* desc:语音异步检测结果查询
*
* @param taskIds 数组中的元素个数不超过100个
* @return 注意该接口只返回检测任务完成的即检测成功或失败的任务正在处理中的不返回继续轮询即可
*/
public static Result<List<VoiceResultDTO>> voiceResults(String url, List<String> taskIds) {
if (StringUtils.isBlank(url) || CollectionUtils.isEmpty(taskIds)) {
throw new RenException("参数错误");
}
if (taskIds.size() > NumConstant.ONE_HUNDRED) {
throw new RenException("参数错误,查询检测任务最大不能超过100");
}
try {
Result<String> result = HttpClientManager.getInstance().sendPostByJSON(url, JSON.toJSONString(taskIds));
log.debug("voiceResults result:{}", JSON.toJSONString(result));
if (result.success()) {
return JSON.parseObject(result.getData(), new TypeReference<Result<List<VoiceResultDTO>>>() {
});
}
Result<List<VoiceResultDTO>> resultResult = new Result<>();
resultResult.error(result.getCode(), result.getMsg());
resultResult.setInternalMsg(result.getInternalMsg());
return resultResult;
} catch (Exception e) {
log.error("voiceResults exception:", e);
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), e.getMessage());
}
}
public static void main(String[] args) { public static void main(String[] args) {
//测试文本检测 //测试文本检测
// testTextSyncScan(); // testTextSyncScan();
//测试语音检测 //测试语音检测
testVoiceAsyncScan(); // testVoiceAsyncScan();
//语音检测结果
testVoiceResults();
} }
public static void testTextSyncScan(){ public static void testTextSyncScan(){
@ -157,4 +192,12 @@ public class ScanContentUtils {
} }
} }
} }
public static void testVoiceResults(){
String url = "http://localhost:8107/epmetscan/api/voiceResults";
List<String> taskIds=new ArrayList<>();
taskIds.add("vc_f_6CXRk1VcAwM6u0FMA@CfoW-1tDgIp");
Result<List<VoiceResultDTO>> asyncScanResultResult = ScanContentUtils.voiceResults(url, taskIds);
System.out.println("================" + JSON.toJSONString(asyncScanResultResult));
}
} }

6
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/common/constant/SysConstant.java

@ -21,4 +21,10 @@ public class SysConstant {
public static final String CODE = "code"; public static final String CODE = "code";
public static final String DATA = "data"; public static final String DATA = "data";
/**
* 任务正在执行中建议您等待一段时间例如5s后再查询结果
*/
public static final int PROCESSING=280;
} }

66
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/common/enu/LabelEnum.java

@ -0,0 +1,66 @@
package com.epmet.openapi.scan.common.enu;
import java.util.ArrayList;
import java.util.List;
/**
* 阿里检测结果的分类字典
*
* @author yinzuomei@elink-cn.com
* @date 2020/12/18 14:04
*/
public enum LabelEnum {
NORMAL("normal", "正常文本"),
SPAM("spam", "含垃圾信息"),
AD("ad", "广告"),
POLITICS("politics","涉政"),
TERRORISM("terrorism","暴恐"),
ABUSE("abuse","辱骂"),
PORN("porn","色情"),
FLOOD("flood","灌水"),
CONTRABAND("contraband","违禁"),
MEANINGLESS("meaningless","无意义"),
CUSTOMIZED("customized","自定义");
private String code;
private String desc;
LabelEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}
public static List<String> getLabelEnumList() {
List<String> result = new ArrayList<>();
LabelEnum[] values = LabelEnum.values();
for (LabelEnum v : values) {
result.add(v.getCode());
}
return result;
}
public static String getDesc(String code) {
LabelEnum[] businessModeEnums = values();
for (LabelEnum labelEnum : businessModeEnums) {
if (labelEnum.getCode().equals(code)) {
return labelEnum.getDesc();
}
}
return "";
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}

46
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/service/impl/ScanServiceImpl.java

@ -10,6 +10,8 @@ import com.aliyuncs.green.model.v20180509.VoiceAsyncScanRequest;
import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest; import com.aliyuncs.green.model.v20180509.VoiceAsyncScanResultsRequest;
import com.aliyuncs.http.FormatType; import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse; import com.aliyuncs.http.HttpResponse;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.utils.Result;
import com.epmet.openapi.scan.common.constant.SysConstant; import com.epmet.openapi.scan.common.constant.SysConstant;
import com.epmet.openapi.scan.common.enu.*; import com.epmet.openapi.scan.common.enu.*;
@ -412,11 +414,11 @@ public class ScanServiceImpl implements ScanService {
try { try {
request.setHttpContent(JSON.toJSONString(taskIds).getBytes(SysConstant.UTF8), SysConstant.UTF8, FormatType.JSON); request.setHttpContent(JSON.toJSONString(taskIds).getBytes(SysConstant.UTF8), SysConstant.UTF8, FormatType.JSON);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
log.error("getVoiceAsyncScanResult parse param exception", e); log.error("voiceResults parse param exception", e);
return new Result<List<VoiceAsyncScanResult>>().error(SysResponseEnum.SCAN_PARAM_ERROR.getCode(), SysResponseEnum.SCAN_PARAM_ERROR.getMsg()); return new Result<List<VoiceAsyncScanResult>>().error(SysResponseEnum.SCAN_PARAM_ERROR.getCode(), SysResponseEnum.SCAN_PARAM_ERROR.getMsg());
} }
log.info("语音异步检测结果查询入参:"+JSON.toJSONString(taskIds,true)); // log.info("语音异步检测结果查询入参:"+JSON.toJSONString(taskIds,true));
try { try {
HttpResponse httpResponse = IAcsClientUtil.getIAcsClient().doAction(request); HttpResponse httpResponse = IAcsClientUtil.getIAcsClient().doAction(request);
@ -429,12 +431,13 @@ public class ScanServiceImpl implements ScanService {
//获取data列表 //获取data列表
JSONArray dataResults = scrResponse.getJSONArray(SysConstant.DATA); JSONArray dataResults = scrResponse.getJSONArray(SysConstant.DATA);
List<VoiceAsyncScanResult> resultList = dataResults.toJavaList(VoiceAsyncScanResult.class); List<VoiceAsyncScanResult> resultList = dataResults.toJavaList(VoiceAsyncScanResult.class);
List<VoiceAsyncScanResult> resultData=processVoiceAsyncScanResult(resultList);
//成功返回 //成功返回
return new Result<List<VoiceAsyncScanResult>>().ok(resultList); return new Result<List<VoiceAsyncScanResult>>().ok(resultData);
}else{ }else{
log.warn("getVoiceAsyncScanResult detect not success. code:{}", scrResponse.getInteger(SysConstant.CODE)); log.warn("voiceResults detect not success. code:{}", scrResponse.getInteger(SysConstant.CODE));
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getCode(), throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getCode(),
SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getMsg() + ",status:" + httpResponse.getStatus()); SysResponseEnum.THIRD_PLATFORM_RESP_CODE_ERROR.getMsg() + ",status:" + httpResponse.getStatus());
} }
@ -445,12 +448,45 @@ public class ScanServiceImpl implements ScanService {
SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getMsg() + ",status:" + httpResponse.getStatus()); SysResponseEnum.THIRD_PLATFORM_RESP_STATUS_ERROR.getMsg() + ",status:" + httpResponse.getStatus());
} }
} catch (Exception e) { } catch (Exception e) {
log.error("executeSyncVoice exception IAcsClientUtil do action exception", e); log.error("voiceResults exception IAcsClientUtil do action exception", e);
throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getCode(), SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getMsg()); throw new ExecuteHttpException(SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getCode(), SysResponseEnum.THIRD_PLATFORM_SERVER_ERROR.getMsg());
} }
} }
private List<VoiceAsyncScanResult> processVoiceAsyncScanResult(List<VoiceAsyncScanResult> resultList) {
List<VoiceAsyncScanResult> list = new ArrayList<>();
for (VoiceAsyncScanResult voiceAsyncScanResult : resultList) {
if (SysConstant.PROCESSING == voiceAsyncScanResult.getCode()) {
//280:表示处理中,需要继续轮询
continue;
}
VoiceAsyncScanResult dto = ConvertUtils.sourceToTarget(voiceAsyncScanResult, VoiceAsyncScanResult.class);
if (HttpStatus.SC_OK == voiceAsyncScanResult.getCode()) {
if (!CollectionUtils.isEmpty(voiceAsyncScanResult.getResults()) && voiceAsyncScanResult.getResults().size() > NumConstant.ZERO) {
//目前只有一个检测场景,所以只判断返回来的第一个场景
VoiceAsyncScanResultDTO voiceAsyncScanResultDTO = voiceAsyncScanResult.getResults().get(NumConstant.ZERO);
if (null != voiceAsyncScanResultDTO) {
dto.setLabel(voiceAsyncScanResultDTO.getLabel());
dto.setLabelDesc(LabelEnum.getDesc(voiceAsyncScanResultDTO.getLabel()));
dto.setSuggestion(voiceAsyncScanResultDTO.getSuggestion());
}
}
} else if(HttpStatus.SC_NOT_FOUND == voiceAsyncScanResult.getCode()) {
dto.setSuggestion(SuggestionEnum.REVIEW.getCode());
dto.setLabel(NumConstant.EMPTY_STR);
dto.setLabelDesc("智能检测任务失败,结果已失效," + SuggestionEnum.REVIEW.getDesc());
}else{
//其他:表示任务失败
dto.setSuggestion(SuggestionEnum.REVIEW.getCode());
dto.setLabel(NumConstant.EMPTY_STR);
dto.setLabelDesc("智能检测任务失败," + SuggestionEnum.REVIEW.getDesc());
}
list.add(dto);
}
return list;
}
private VoiceAsyncScanResultsRequest getVoiceAsyncScanResultsRequest(){ private VoiceAsyncScanResultsRequest getVoiceAsyncScanResultsRequest(){
VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest(); VoiceAsyncScanResultsRequest getResultsRequest = new VoiceAsyncScanResultsRequest();
// 指定API返回格式。 // 指定API返回格式。

52
epmet-openapi/epmet-openapi-scan/src/main/java/com/epmet/openapi/scan/support/result/VoiceAsyncScanResult.java

@ -15,6 +15,43 @@ import java.util.List;
@Data @Data
public class VoiceAsyncScanResult implements Serializable { public class VoiceAsyncScanResult implements Serializable {
private static final long serialVersionUID = 8702129292884498023L; private static final long serialVersionUID = 8702129292884498023L;
/**
* 检测对象对应的数据ID
*/
private String dataId;
/**
* 检测任务的ID
*/
private String taskId;
/**
* 检测结果的分类取值
* normal正常文本
* spam含垃圾信息
* ad广告
* politics涉政
* terrorism暴恐
* abuse辱骂
* porn色情
* flood灌水
* contraband违禁
* meaningless无意义
* customized自定义例如命中自定义关键词
*/
private String label;
private String labelDesc;
/**
* 建议您执行的后续操作取值
* pass结果正常无需进行其余操作
* review结果不确定需要进行人工审核
* block结果违规建议直接删除或者限制公开
*/
private String suggestion;
/** /**
* 错误码和HTTP状态码一致 * 错误码和HTTP状态码一致
* 200表示检测成功 * 200表示检测成功
@ -22,20 +59,13 @@ public class VoiceAsyncScanResult implements Serializable {
* 其他表示任务失败 * 其他表示任务失败
* 更多信息请参见公共错误码 * 更多信息请参见公共错误码
*/ */
private String code; @JsonIgnore
private Integer code;
/** /**
* 错误描述信息 * 错误描述信息
*/ */
@JsonIgnore
private String msg; private String msg;
/**
* 检测对象对应的数据ID
*/
private String dataId;
/**
* 检测任务的ID
*/
private String taskId;
/** /**
* 暂时没用所以返回忽略 * 暂时没用所以返回忽略
@ -45,7 +75,9 @@ public class VoiceAsyncScanResult implements Serializable {
/** /**
* 检测成功code=200返回的检测结果该结果包含一个或多个元素每个元素是个结构体对应一个场景关于每个元素的结构描述请参见result * 检测成功code=200返回的检测结果该结果包含一个或多个元素每个元素是个结构体对应一个场景关于每个元素的结构描述请参见result
* 暂时不展示审核结果明细
*/ */
@JsonIgnore
private List<VoiceAsyncScanResultDTO> results; private List<VoiceAsyncScanResultDTO> results;
} }

Loading…
Cancel
Save