<!--阿里文本垃圾检测--> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-green</artifactId> <version>3.5.1</version> </dependency>
/** * @author zbf * @version 1.0 * @date 2019/12/17 17:02 * @ClassIntroduction */ public enum QdwyTxtEnum { //违规数据 BLOCK("block"), //疑似违规数据 REVIEW("review"), //正确数据 PASS("pass"); private String value; QdwyTxtEnum(String value) { this.value = value; } public String getValue() { return value; }}
/** * @author zbf * @version 1.0 * @date 2019/12/17 16:37 * @ClassIntroduction */ @Slf4j public class QdwyFindTxt { /** * * @param strTxt 长度不超过10000个字符 * @param txt_area 阿里文本检测area * @param txt_key 阿里文本检测key * @param txt_key_secret 阿里文本检测keySecret * @return */ public static String getTxt(String strTxt,String txt_area,String txt_key,String txt_key_secret) { try { IClientProfile profile = DefaultProfile.getProfile(txt_area, txt_key, txt_key_secret); IAcsClient client = new DefaultAcsClient(profile); TextScanRequest textScanRequest = new TextScanRequest(); textScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式 textScanRequest.setHttpContentType(FormatType.JSON); textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法 textScanRequest.setEncoding("UTF-8"); textScanRequest.setRegionId(txt_area); List<Map<String, Object>> tasks = new ArrayList<>(); Map<String, Object> task1 = new LinkedHashMap<>(); task1.put("dataId", UUID.randomUUID().toString()); /** * 待检测的文本,长度不超过10000个字符 */ task1.put("content", strTxt); tasks.add(task1); JSONObject data = new JSONObject(); /** * 检测场景,文本垃圾检测传递:antispam **/ data.put("scenes", Arrays.asList("antispam")); data.put("tasks", tasks); textScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON); // 请务必设置超时时间 textScanRequest.setConnectTimeout(30000); textScanRequest.setReadTimeout(60000); HttpResponse httpResponse = client.doAction(textScanRequest); if (httpResponse.isSuccess()) { JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8")); System.out.println(JSON.toJSONString(scrResponse, true)); if (200 == scrResponse.getInteger("code")) { JSONArray taskResults = scrResponse.getJSONArray("data"); for (Object taskResult : taskResults) { if (200 == ((JSONObject) taskResult).getInteger("code")) { JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results"); for (Object sceneResult : sceneResults) { String scene = ((JSONObject) sceneResult).getString("scene"); String suggestion = ((JSONObject) sceneResult).getString("suggestion"); //根据scene和suggetion做相关处理 //suggestion == pass 未命中垃圾 suggestion == block 命中了垃圾,可以通过label字段查看命中的垃圾分类 log.info("args = [" + scene + "]"); log.info("args = [" + suggestion + "]"); if (StringUtils.isNotBlank(suggestion)){ return suggestion; } } } else { log.info("task process fail:" + ((JSONObject) taskResult).getInteger("code")); } } } else { log.info("detect not success. code:" + scrResponse.getInteger("code")); } } else { log.info("response not success. status:" + httpResponse.getStatus()); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } return ""; } }