阿里文本垃圾检测

<!--阿里文本垃圾检测-->
<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 "";
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值